net: only monitor select exceptions while connecting

bitcoin/bitcoin: PR #36716

4 suggestions

Optional improvements

Link

[suggestion] · Tests do not check the select() exception-set gate

The urgent-data probes check progress and byte accounting, so restoring unconditional exception monitoring can leave them passing; no test checks that ConnErrorEventFlag preserves failed-connect detection.

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

Details and suggested fix

check_probe() only requires version processing and the expected bytesrecv. The previous handler already read ordinary input on recvSet || errorSet and ignored WSAEWOULDBLOCK, so those assertions do not distinguish unwanted exceptional wakeups. The existing sock_tests wait case also discards occurred. Add a TcpSocketPair case that sends MSG_OOB and checks that an unflagged RecvEvent wait does not report ErrorEvent on a select() build. Ensure the urgent indication has arrived before checking, otherwise the test can pass before it becomes observable. A refused non-blocking connection test through ConnectToSocket() should separately distinguish the "failed after wait" outcome from a timeout. Native Linux builds use poll() and cannot exercise the changed gate.


Link

[suggestion] · ConnErrorEventFlag has an unnecessary dependency on RecvEvent

Removing the RecvEvent dependency simplifies the API and avoids delayed Windows failure detection for SendEvent | ConnErrorEventFlag, although no current caller uses that combination.

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

Details and suggested fix

The exception-set registration sits inside the RecvEvent branch, even though the flag selects failed-connect monitoring rather than read readiness. A SendEvent | ConnErrorEventFlag wait would silently omit that monitoring on Windows, where a failed non-blocking connect is reported only in the exception set. ConnectToSocket(), the only current caller, also requests RecvEvent and is unaffected. Register err whenever ConnErrorEventFlag is set, include the flag in the socket_max condition, and remove the documented dependency. This eliminates the no-effect combination without changing the existing caller or established-connection behavior.


Link

[suggestion] · Early disconnects omit the socket error from debug logs

On poll() builds, P2P and HTTP resets handled before sending now log only a generic error/hangup, losing the OS error detail available through the previous I/O-error paths.

Location: src/net.cpp:2214 (head).

Details and suggested fix

When ErrorEvent occurs without RecvEvent requested, the new branches close the connection before attempting I/O. For a reset with sending ready, this skips the send-error log that previously included NetworkErrorString(err), making resets and other failures indistinguishable in -debug=net and -debug=http output. Before closing, read SO_ERROR with GetSockOpt() and append its description when nonzero, following the existing pattern in ConnectToSocket(). Keep the generic message for a hangup with no pending error.


Link

[suggestion] · HTTP urgent-data coverage is paired with an unrelated error-path change

In 05084e78, check_urgent_data() checks healthy request progress rather than the new error/hangup branch; moving it beside the P2P urgent-data tests would make the series' coverage boundaries clearer.

Location: test/functional/interface_http.py:612 at 05084e787914 (commit) (commit).

Details and suggested fix

Commit 05084e7879140aaf7c9b77e372ebc828f98e3153 adds both check_urgent_data() and the HTTP err_ready && !recv_requested disconnect. The test sends MSG_OOB on a healthy connection and checks a successful RPC response, so it does not exercise that disconnect. Move the test, its invocation, and imports into 2ec146efa0828a541124897882a9f882fdfbaed9, which introduces P2P urgent-data coverage before the exception-registration change in 6919268d7172360c464512bf744580cd18def0c7. All required helpers already exist there, and the earlier HTTP receive path tolerates transient errors. This keeps urgent-data coverage together and leaves the final HTTP commit focused on its error-before-send behavior, alongside the P2P counterpart in 8b8a2f49267daa6a3f9d85d9cb511bd7c286e52a. The final tree would be 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

A separate objection to SO_OOBINLINE was that urgent data has no meaning in Bitcoin's protocol: theuni stated that Bitcoin would neither generate it nor act on it. Darosior also objected to interpreting socket exceptions in the connection handler as necessarily urgent data, because the exception set has platform-dependent meanings.

These arguments support keeping transport-specific readiness handling separate from P2P message interpretation. They explain the motivation for neither introducing urgent bytes into the ordinary stream nor requiring established-connection handlers to classify select exceptions as protocol misbehavior.

Source 1 · Source 2

Unresolved disagreements

The inspected predecessor discussion did not settle ignoring urgent indications versus disconnecting on exceptions. Sjors and theuni proposed disconnecting, with theuni favoring consistent treatment across backends. Darosior argued that connection-handler interpretation was fragile and favored limiting exception registration at the socket layer; he also expressed a longer-term preference for poll on all platforms.

These were competing responses to an unused transport mechanism, not evidence that urgent data must enter the application stream. The available record explains the alternatives and their objections, but does not justify describing pending-connect-only monitoring as an independently established consensus.

Source 1 · Source 2 · Source 3 · Source 4 · Source 5