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:
Vadim Zeitlin
2019-11-20 19:00:30 +01:00
parent 51ea713826
commit 73f0c9dff8
4 changed files with 31 additions and 18 deletions

View File

@@ -57,9 +57,6 @@ public:
// anything here // anything here
} }
private:
virtual void DoClose() wxOVERRIDE;
virtual void UpdateBlockingState() wxOVERRIDE virtual void UpdateBlockingState() wxOVERRIDE
{ {
if ( GetSocketFlags() & wxSOCKET_BLOCK ) if ( GetSocketFlags() & wxSOCKET_BLOCK )
@@ -86,6 +83,9 @@ private:
} }
} }
private:
virtual void DoClose() wxOVERRIDE;
int m_msgnumber; int m_msgnumber;
friend class wxSocketMSWManager; friend class wxSocketMSWManager;

View File

@@ -281,6 +281,12 @@ public:
// notifications // 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 // notify m_wxsocket about the given socket event by calling its (inaptly
// named) OnRequest() method // named) OnRequest() method
void NotifyOnStateChange(wxSocketNotify event); void NotifyOnStateChange(wxSocketNotify event);
@@ -323,12 +329,6 @@ private:
// called by Close() if we have a valid m_fd // called by Close() if we have a valid m_fd
virtual void DoClose() = 0; 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 // 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 // (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 // set m_error and return false if this is not the case

View File

@@ -59,6 +59,15 @@ public:
EnableEvents(flags); EnableEvents(flags);
} }
virtual void UpdateBlockingState() wxOVERRIDE
{
// Make this int and not bool to allow passing it to ioctl().
const int isBlocking = (GetSocketFlags() & wxSOCKET_BLOCK) != 0;
ioctl(m_fd, FIONBIO, &isBlocking);
DoEnableEvents(wxSOCKET_INPUT_FLAG | wxSOCKET_OUTPUT_FLAG, !isBlocking);
}
// wxFDIOHandler methods // wxFDIOHandler methods
virtual void OnReadWaiting() wxOVERRIDE; virtual void OnReadWaiting() wxOVERRIDE;
virtual void OnWriteWaiting() wxOVERRIDE; virtual void OnWriteWaiting() wxOVERRIDE;
@@ -73,15 +82,6 @@ private:
wxCloseSocket(m_fd); wxCloseSocket(m_fd);
} }
virtual void UpdateBlockingState() wxOVERRIDE
{
// Make this int and not bool to allow passing it to ioctl().
const int isBlocking = (GetSocketFlags() & wxSOCKET_BLOCK) != 0;
ioctl(m_fd, FIONBIO, &isBlocking);
DoEnableEvents(wxSOCKET_INPUT_FLAG | wxSOCKET_OUTPUT_FLAG, !isBlocking);
}
// enable or disable notifications for socket input/output events // enable or disable notifications for socket input/output events
void EnableEvents(int flags = wxSOCKET_INPUT_FLAG | wxSOCKET_OUTPUT_FLAG) void EnableEvents(int flags = wxSOCKET_INPUT_FLAG | wxSOCKET_OUTPUT_FLAG)
{ DoEnableEvents(flags, true); } { DoEnableEvents(flags, true); }

View File

@@ -1672,7 +1672,20 @@ void wxSocketBase::SetFlags(wxSocketFlags flags)
"Using wxSOCKET_WAITALL or wxSOCKET_BLOCK with " "Using wxSOCKET_WAITALL or wxSOCKET_BLOCK with "
"wxSOCKET_NOWAIT doesn't make sense" ); "wxSOCKET_NOWAIT doesn't make sense" );
// Blocking sockets are very different from non-blocking ones and we need
// to [un]register the socket with the event loop if wxSOCKET_BLOCK is
// being [un]set.
const bool
blockChanged = (m_flags & wxSOCKET_BLOCK) != (flags & wxSOCKET_BLOCK);
m_flags = flags; m_flags = flags;
if ( blockChanged )
{
// Of course, we only do this if we already have the actual socket.
if ( m_impl )
m_impl->UpdateBlockingState();
}
} }