Skip to content

Commit 3230490

Browse files
zardusclaude
andauthored
SocketServer: close _serverFd exactly once (#585)
SocketServer::stop() closed _serverFd but never cleared it, and stop() runs again from both ~WebSocketServer() and ~SocketServer(), so every teardown closed the same fd number two or three times. If another thread opens a descriptor between those closes, the kernel reuses the number and the stale close destroys it — observed as intermittent process aborts ("Unexpected error 9 on netlink descriptor", glibc's getaddrinfo netlink probe) when a DNS lookup raced a server teardown. Close the fd once and set it to -1, in stop() and in the listen() error paths. Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
1 parent fb21fa2 commit 3230490

1 file changed

Lines changed: 16 additions & 1 deletion

File tree

ixwebsocket/IXSocketServer.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ namespace ix
9595
<< "at address " << _host << ":" << _port << " : " << strerror(Socket::getErrno());
9696

9797
Socket::closeSocket(_serverFd);
98+
_serverFd = -1;
9899
return std::make_pair(false, ss.str());
99100
}
100101

@@ -113,6 +114,7 @@ namespace ix
113114
<< strerror(Socket::getErrno());
114115

115116
Socket::closeSocket(_serverFd);
117+
_serverFd = -1;
116118
return std::make_pair(false, ss.str());
117119
}
118120

@@ -125,6 +127,7 @@ namespace ix
125127
<< strerror(Socket::getErrno());
126128

127129
Socket::closeSocket(_serverFd);
130+
_serverFd = -1;
128131
return std::make_pair(false, ss.str());
129132
}
130133
}
@@ -143,6 +146,7 @@ namespace ix
143146
<< strerror(Socket::getErrno());
144147

145148
Socket::closeSocket(_serverFd);
149+
_serverFd = -1;
146150
return std::make_pair(false, ss.str());
147151
}
148152

@@ -155,6 +159,7 @@ namespace ix
155159
<< strerror(Socket::getErrno());
156160

157161
Socket::closeSocket(_serverFd);
162+
_serverFd = -1;
158163
return std::make_pair(false, ss.str());
159164
}
160165
}
@@ -169,6 +174,7 @@ namespace ix
169174
<< "at address " << _host << ":" << _port << " : " << strerror(Socket::getErrno());
170175

171176
Socket::closeSocket(_serverFd);
177+
_serverFd = -1;
172178
return std::make_pair(false, ss.str());
173179
}
174180

@@ -231,7 +237,16 @@ namespace ix
231237
}
232238

233239
_conditionVariable.notify_one();
234-
Socket::closeSocket(_serverFd);
240+
241+
// stop() runs again from ~WebSocketServer() and ~SocketServer(), so
242+
// close the listening fd exactly once: a second close of the stale
243+
// number would destroy whatever descriptor another thread has since
244+
// opened with it.
245+
if (_serverFd != -1)
246+
{
247+
Socket::closeSocket(_serverFd);
248+
_serverFd = -1;
249+
}
235250
}
236251

237252
void SocketServer::setConnectionStateFactory(

0 commit comments

Comments
 (0)