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:
@@ -59,6 +59,15 @@ public:
|
||||
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
|
||||
virtual void OnReadWaiting() wxOVERRIDE;
|
||||
virtual void OnWriteWaiting() wxOVERRIDE;
|
||||
@@ -73,15 +82,6 @@ private:
|
||||
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
|
||||
void EnableEvents(int flags = wxSOCKET_INPUT_FLAG | wxSOCKET_OUTPUT_FLAG)
|
||||
{ DoEnableEvents(flags, true); }
|
||||
|
Reference in New Issue
Block a user