Fix crash with blocking accepting sockets in threads under Unix
Sockets returned by wxSocket::Accept() are non-blocking by default and the only way to use them safely in worker threads is by switching them to the blocking mode by calling SetFlags(wxSOCKET_BLOCK). However this didn't work correctly since at least 2.8 days, as turning wxSOCKET_BLOCK on didn't unregister the socket from the event loop, with which it had been registered on creation. Fix this by doing this now, which ensures that the main thread doesn't get any notifications about the socket if it's used, in a blocking way, in a worker thread. Note that making the new socket blocking after accpeting is still pretty inefficient and pre-creating the socket as blocking and using AcceptWith() is still preferable, but at least it does work now. Closes #12886.
This commit is contained in:
@@ -281,6 +281,12 @@ public:
|
||||
// notifications
|
||||
// -------------
|
||||
|
||||
// Update the socket depending on the presence or absence of wxSOCKET_BLOCK
|
||||
// in GetSocketFlags(): if it's present, make the socket blocking and
|
||||
// ensure that we don't get any asynchronous event for it, otherwise put
|
||||
// it into non-blocking mode and enable monitoring it in the event loop.
|
||||
virtual void UpdateBlockingState() = 0;
|
||||
|
||||
// notify m_wxsocket about the given socket event by calling its (inaptly
|
||||
// named) OnRequest() method
|
||||
void NotifyOnStateChange(wxSocketNotify event);
|
||||
@@ -323,12 +329,6 @@ private:
|
||||
// called by Close() if we have a valid m_fd
|
||||
virtual void DoClose() = 0;
|
||||
|
||||
// Update the socket depending on the presence or absence of wxSOCKET_BLOCK
|
||||
// in GetSocketFlags(): if it's present, make the socket blocking and
|
||||
// ensure that we don't get any asynchronous event for it, otherwise put
|
||||
// it into non-blocking mode and enable monitoring it in the event loop.
|
||||
virtual void UpdateBlockingState() = 0;
|
||||
|
||||
// check that the socket wasn't created yet and that the given address
|
||||
// (either m_local or m_peer depending on the socket kind) is valid and
|
||||
// set m_error and return false if this is not the case
|
||||
|
Reference in New Issue
Block a user