Skip to content

Commit 3e9337f

Browse files
Fix busy-loop in I2P Accept when socket has error
The Wait() function returns false for both timeout and socket errors. Previously, the Accept loop treated all false returns as timeouts and continued, which could cause a busy-loop if the socket had an error. Now we call IsConnected() when Wait() returns false to distinguish: - If socket has error: break out of loop with error message - If genuine timeout: continue waiting for connections This prevents CPU spinning when the socket is in an error state. Co-authored-by: reuben <[email protected]>
1 parent 9ce18cb commit 3e9337f

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/i2p.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,15 @@ bool Session::Accept(Connection& conn)
206206

207207
while (!(*m_interrupt)) {
208208
if (!Wait(conn.sock, MAX_WAIT_FOR_IO, true)) {
209-
// Timeout, no incoming connections or errors within MAX_WAIT_FOR_IO.
209+
// Wait returned false - could be timeout or socket error
210+
// Check if the socket is still valid to distinguish between the two
211+
std::string socket_errmsg;
212+
if (!IsConnected(conn.sock, socket_errmsg)) {
213+
// Socket error - break out to avoid busy-looping
214+
errmsg = strprintf("Socket error while waiting for connection: %s", socket_errmsg);
215+
break;
216+
}
217+
// Genuine timeout - continue waiting for incoming connections
210218
continue;
211219
}
212220

0 commit comments

Comments
 (0)