From 8a29f958a186af1a20237dfbb92cfd15fb8bd317 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 15 Aug 2017 13:53:15 +0200 Subject: [PATCH] Detect any attempt to use non-blocking socket from worker threads This doesn't work, as non-blocking sockets implementation requires dispatching events generated by them, which is only possible from the main thread event loop, and it's better to be upfront about it rather than failing mysteriously later. --- interface/wx/socket.h | 6 ++++++ src/common/socket.cpp | 10 +++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/interface/wx/socket.h b/interface/wx/socket.h index 0de0c81e43..36ad0f87d2 100644 --- a/interface/wx/socket.h +++ b/interface/wx/socket.h @@ -289,6 +289,12 @@ public: /** Constructor. + Notice that if the object is created from a worker thread or if it is + created from the main thread but the event loop is not running, @a + flags parameter @em must include ::wxSOCKET_BLOCK as non-blocking + sockets require dispatching events, which can only be done in the main + thread. + @param flags Socket flags (See wxSocketBase::SetFlags()) */ diff --git a/src/common/socket.cpp b/src/common/socket.cpp index 37cd53783a..5fb8c291ae 100644 --- a/src/common/socket.cpp +++ b/src/common/socket.cpp @@ -25,7 +25,6 @@ #include "wx/socket.h" #ifndef WX_PRECOMP - #include "wx/app.h" #include "wx/object.h" #include "wx/string.h" #include "wx/intl.h" @@ -1975,6 +1974,15 @@ bool wxSocketBase::SetLocal(const wxIPV4address& local) wxSocketClient::wxSocketClient(wxSocketFlags flags) : wxSocketBase(flags, wxSOCKET_CLIENT) { + // Notice that we don't check for a running event loop here, unlike in + // GetBlockingFlagIfNeeded() because it is common to create the sockets + // before the event loop is entered and we shouldn't break existing code + // doing this as it can still work correctly if it only uses non-blocking + // sockets once the event loop is running. + wxASSERT_MSG( (flags & wxSOCKET_BLOCK) || wxIsMainThread(), + wxS("Non-blocking sockets may only be created ") + wxS("in the main thread") ); + m_initialRecvBufferSize = m_initialSendBufferSize = -1; }