diff --git a/include/wx/msw/private/sockmsw.h b/include/wx/msw/private/sockmsw.h index 5c4e565d20..bbcad77bca 100644 --- a/include/wx/msw/private/sockmsw.h +++ b/include/wx/msw/private/sockmsw.h @@ -51,9 +51,25 @@ private: virtual void UnblockAndRegisterWithEventLoop() wxOVERRIDE { - // no need to make the socket non-blocking, Install_Callback() will do - // it - wxSocketManager::Get()->Install_Callback(this); + if ( GetSocketFlags() & wxSOCKET_BLOCK ) + { + // Counter-intuitively, we make the socket non-blocking even in + // this case as it is necessary e.g. for Read() to return + // immediately if there is no data available. However we must not + // install a callback for it as blocking sockets don't use any + // events and generating them would actually be harmful (and not + // just useless) as they would be dispatched by the main thread + // while this blocking socket can be used from a worker one, so it + // would result in data races and other unpleasantness. + unsigned long trueArg = 1; + ioctlsocket(m_fd, FIONBIO, &trueArg); + } + else + { + // No need to make the socket non-blocking, Install_Callback() will + // do it as a side effect of calling WSAAsyncSelect(). + wxSocketManager::Get()->Install_Callback(this); + } } int m_msgnumber;