net: only monitor select exceptions while connecting

bitcoin/bitcoin: PR #36716

3 suggestions

Optional improvements

Link

[suggestion] · The regression test never exercises ConnErrorEventFlag

wait_ignores_urgent_data would still pass if the select() backend stopped registering the exception set entirely, leaving the new opt-in path untested.

Location: src/test/sock_tests.cpp:98 (head).

Details and suggested fix

The raw select() call confirms that an urgent byte is pending, but the only subsequent Wait() requests RecvEvent without the flag. Under #ifndef USE_POLL, add another wait requesting Sock::RecvEvent | Sock::ConnErrorEventFlag and assert that Sock::ErrorEvent is set while Sock::ConnErrorEventFlag is absent from occurred. Nothing consumes the urgent byte, so the existing setup supplies the positive case while preserving the negative assertion. This checks the backend opt-in, not whether ConnectToSocket passes the flag. The Windows failed-connect rationale comes from the contract in sock.h and the supplied historical context, not independently checked Winsock documentation.


Link

[suggestion] · The urgent-data test does not verify blocking

On select() builds, the test could accept an immediate return with no reported events, even though the current implementation correctly waits.

Location: src/test/sock_tests.cpp:97 (head).

Details and suggested fix

The test checks only that Wait() succeeds and reports occurred == 0. An implementation that still registered the socket in the exception set but masked the resulting ErrorEvent would return immediately for the pending urgent byte and pass both assertions, despite allowing repeated wakeups. Measure the existing Wait(100ms, ...) call with a steady clock and assert a generous lower bound, such as 50ms. Avoid an upper bound so scheduling delays do not cause failures. This distinguishes ignoring an exception result from actually avoiding the unwanted wakeup.


Link

[suggestion] · ConnErrorEventFlag unnecessarily depends on RecvEvent

ConnErrorEventFlag is ignored without RecvEvent, adding an unnecessary mode restriction to the API; no current caller uses the affected combination.

Location: src/util/sock.cpp:223 (head).

Details and suggested fix

The exception-set registration sits inside the RecvEvent branch, so SendEvent | ConnErrorEventFlag requests write readiness but does not enable exception monitoring. Connection-failure monitoring does not depend on read readiness, and making it independent would remove this ignored flag combination and its header caveat. Move the flag check outside the RecvEvent branch and include the flag in the socket_max condition. ConnectToSocket, the only current caller requesting the flag, already passes both readiness events, so its behavior and all unflagged waits on established connections would remain unchanged.


Coverage details

Background

Ralph's summary of historical context for the behaviour affected by this change. The historical record reviewed here is incomplete.

Documented decisions

Exception monitoring was retained as a select-specific Windows compatibility workaround, rather than because Bitcoin's established connections need TCP urgent-data notifications.

The earlier discussion explicitly says that watching exceptions is necessary for Windows and proposes registering it only where needed. This supports separating connection-establishment monitoring from ordinary receive readiness, but does not independently establish every Windows failure mode.

Source 1

Monitoring the exception set on established connections can expose persistent TCP urgent-data notifications that ordinary recv() does not consume; the historical concern was unwanted repeated wakeups, with BSD/macOS specifically identified as affected by the Windows workaround.

The earlier proposal states that select may keep reporting urgent data while Linux poll is indifferent. Its discussion explicitly connects the BSD/macOS issue to exception monitoring introduced for Windows. This is the reason to distinguish exceptional connect completion from established-connection receive readiness.

Source 1 · Source 2

Ralph's inferences

For the historically identified Windows case, the pinned change preserves the exception-notification path: ConnectToSocket requests RecvEvent, SendEvent, and the new flag together, then still checks SO_ERROR after waking.

The flag's nesting under RecvEvent does not exclude this connection-establishment wait, because that caller requests both. The base and head retain the distinction between a readiness notification and successful connection establishment: a nonzero SO_ERROR still causes failure. This is a control-flow comparison, not an exhaustive platform validation.

Source 1

The compatibility distinction is asynchronous connection completion versus ordinary socket I/O, not a new classification of connect error codes.

Both pinned versions enter the wait path for WSAEINPROGRESS, WSAEWOULDBLOCK, and WSAEINVAL, with a comment attributing the last to legacy Winsock. Those are treated as pending-connection indications, not the eventual failure reason. At head, select waits without the flag omit the socket from the exception set, excluding exception-only urgent-data wakeups; poll retains its existing POLLERR/POLLHUP reporting unchanged.