Skip to content

Commit

Permalink
network: fix double close event (#236)
Browse files Browse the repository at this point in the history
It is theoretically possible that we complete the SSL handshake in
the write ready event. In this case, we may raise a connected event
and have the connection closed. We need to handle this case and not
double raise the event.
  • Loading branch information
mattklein123 authored Nov 20, 2016
1 parent 4090ba2 commit 8d53667
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions source/common/network/connection_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ void ConnectionImpl::readDisable(bool disable) {

void ConnectionImpl::raiseEvents(uint32_t events) {
for (ConnectionCallbacks* callback : callbacks_) {
// TODO: If we close while raising a connected event we should not raise further connected
// events.
callback->onEvent(events);
}
}
Expand Down Expand Up @@ -331,8 +333,13 @@ void ConnectionImpl::onWriteReady() {
}

if (doWriteToSocket() == PostIoAction::Close) {
closeSocket();
raiseEvents(ConnectionEvent::RemoteClose);
// It is possible (though unlikely) for the connection to have already been closed during the
// write callback. This can happen if we manage to complete the SSL handshake in the write
// callback, raise a connected event, and close the connection.
if (fd_ != -1) {
closeSocket();
raiseEvents(ConnectionEvent::RemoteClose);
}
} else if ((state_ & InternalState::CloseWithFlush) && write_buffer_.length() == 0) {
conn_log_debug("write flush complete", *this);
doLocalClose();
Expand Down

0 comments on commit 8d53667

Please sign in to comment.