From ad9038cc39cc14dc420c9ca9aead30f43195102a Mon Sep 17 00:00:00 2001 From: "wtc@google.com" Date: Thu, 20 Nov 2008 18:17:05 +0000 Subject: Cancel any pending IO and wait for it to be aborted before returning from the Disconnect method. This allows the caller to destroy the read or write buffer safely when the destructor (which calls Disconnect) returns. R=eroman,rvargas BUG=4449 Review URL: http://codereview.chromium.org/11299 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@5775 0039d316-1c4b-4281-b951-d872f2087c98 --- net/base/tcp_client_socket_win.cc | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'net/base/tcp_client_socket_win.cc') diff --git a/net/base/tcp_client_socket_win.cc b/net/base/tcp_client_socket_win.cc index bb25e34..11a6de3 100644 --- a/net/base/tcp_client_socket_win.cc +++ b/net/base/tcp_client_socket_win.cc @@ -79,6 +79,7 @@ int TCPClientSocket::Connect(CompletionCallback* callback) { if (rv != OK) return rv; + // WSACreateEvent creates a manual-reset event object. overlapped_.hEvent = WSACreateEvent(); // WSAEventSelect sets the socket to non-blocking mode as a side effect. // Our connect() and recv() calls require that the socket be non-blocking. @@ -116,13 +117,19 @@ void TCPClientSocket::Disconnect() { // Make sure the message loop is not watching this object anymore. watcher_.StopWatching(); + // Cancel any pending IO and wait for it to be aborted. + if (wait_state_ == WAITING_READ || wait_state_ == WAITING_WRITE) { + CancelIo(reinterpret_cast(socket_)); + WaitForSingleObject(overlapped_.hEvent, INFINITE); + wait_state_ = NOT_WAITING; + } + // In most socket implementations, closing a socket results in a graceful // connection shutdown, but in Winsock we have to call shutdown explicitly. // See the MSDN page "Graceful Shutdown, Linger Options, and Socket Closure" // at http://msdn.microsoft.com/en-us/library/ms738547.aspx shutdown(socket_, SD_SEND); - // This cancels any pending IO. closesocket(socket_); socket_ = INVALID_SOCKET; -- cgit v1.1