WIP: net, http: handle socket errors separately from reads

bitcoin/bitcoin: PR #36963

1 minor finding · 1 suggestion

Findings

Link

[minor] · P2P reset test can time out on a split ping batch

The new P2P reset test can time out if TCP splits the ping batch across reads and receive backpressure pauses input before the exact byte target; this failure has not been observed.

Location: test/functional/p2p_socket_errors.py:92 (head).

Details and suggested fix

With -maxreceivebuffer=1, the processing-queue limit is 1000 bytes. After each recv(), MarkReceivedMsgsForProcessing() counts the memory of completed messages, including each CNetMessage object. A partial read containing enough complete pings can therefore set fPauseRecv before all 100 pings arrive. GenerateWaitSockets() then omits RecvEvent, while stalled block output keeps ProcessMessages() from reaching PollMessage() to drain the queue. The exact totalbytesrecv == received target remains unreachable until the wait times out. One transport.write() does not guarantee one receiver read, although the frequency of this split on loopback is unconfirmed.

Use a single message whose payload alone exceeds 1000 bytes, such as a getdata with a few dozen inventory entries, as the backpressure trigger. Incomplete messages do not count toward the processing queue, so receiving continues until that message is complete regardless of read boundaries. Wait for its bytes, then send the extra input and reset as before.


Optional improvements

Link

[suggestion] · HTTP reset test never checks that the client is removed

The HTTP reset test checks no reset outcome on select() builds and only an error log on poll() builds, so it can pass without verifying removal of the reset client.

Location: test/functional/interface_http.py:920 (head).

Details and suggested fix

With expected_msgs == [], assert_debug_log() returns immediately after close_sock(). This is the last check in the test. On select() builds, the stalled client requests only SendEvent, so reset handling depends on MaybeSendBytesFromBuffer() failing and setting m_disconnect. On poll() builds, the expected error line precedes client->Disconnect(); removal happens separately in DisconnectClients(). Neither outcome is asserted here.

Before closing, record port = conn.conn.sock.getsockname()[1]. Always expect f"Disconnecting HTTP client 127.0.0.1:{port}", retaining the additional poll-only error expectation. Matching the port avoids accepting another client's removal. This adds a disconnection assertion on every platform, as the P2P counterpart already has. It assumes select() reports the reset socket as writable on Windows and macOS, which the P2P test also relies on.


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

Exceptional-condition monitoring has a necessary platform-specific role during non-blocking connection establishment: Windows requires the select() exception set to detect a failed connect. Removing exceptional-condition monitoring everywhere would therefore lose a required connection-completion signal.

The explicit ErrorEvent contract must retain this wakeup during connection establishment. At the pinned head, ConnectToSocket() requests RecvEvent, SendEvent and ErrorEvent, then checks SO_ERROR after a non-timeout wakeup; readiness alone is not treated as successful connection establishment.

Source 1

The same select() exception set has a different meaning on established connections: TCP urgent data can remain pending even though ordinary recv() never returns it. Monitoring that indication can repeatedly wake the socket handler without corresponding ordinary input. The predecessor discussion describes these repeated wakeups as undesirable, while explicitly distinguishing them from a crash or complete loss of progress after the earlier mitigation.

The established-connection requirement is to avoid turning an urgent indication into a persistent ErrorEvent notification. The pinned head separates exceptional-condition monitoring from RecvEvent, and its unit test verifies that a pending urgent byte does not produce ErrorEvent for a receive-only wait. It deliberately does not require that every readiness bit remain clear.

Source 1

The historical API discussion recognized that ErrorEvent cannot have identical request semantics across the two backends: poll() reports errors whether requested or not, whereas requesting the select() exception set is useful during connecting but undesirable for established connections. A separate connecting flag was considered; reusing ErrorEvent was chosen to avoid another flag, with an explicit warning about its restricted use.

Callers must not interpret omission of ErrorEvent as suppressing poll() errors, nor request it indiscriminately to obtain ordinary connection-failure reporting. The pinned header documents both the unconditional poll() reporting and the connecting-only select() opt-in.

Source 1 · Source 2

Ralph's inferences

ErrorEvent is a backend-dependent notification, not a portable assertion that a socket has suffered a fatal error. Its significance depends on both the backend and whether the socket is connecting or established.

The pinned base implicitly adds receive-waiting sockets to select()'s exception set; the head makes that membership explicit. Meanwhile, both versions translate POLLERR/POLLHUP into ErrorEvent without requesting those conditions. This distinction explains why Windows failed-connect wakeups and established-connection urgent-data handling must be considered separately.

Source 1