Skip to content

net, http: handle socket errors separately from reads - #36963

Open
Sjors wants to merge 8 commits into
bitcoin:masterfrom
Sjors:2026/11/http-socket-errors
Open

Sjors wants to merge 8 commits into
bitcoin:masterfrom
Sjors:2026/11/http-socket-errors

Conversation

@Sjors

@Sjors Sjors commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

With poll(), which we only use on Linux, a socket error or hangup is reported whether or not we asked for it. If that happens for a peer we did not request to read from, disconnect directly. This just adds clarity: previously we would attempt a send that would then fail. The only side-effect is a changed log message. Nothing changes on other platforms: since #36716 they no longer report errors for established connections.

The last commit does the same for HTTP, where on error it also no longer attempts a final read from a client whose reads are paused.

As a followup to #36716, p2p_socket_errors.py adds coverage for existing behavior:

  • the node ignores a single urgent byte: the connection stays open, ordinary data is still processed and the urgent byte is not counted as received.
  • a peer resets the connection while the node's send to it is stalled and receiving from it is paused: the node disconnects without reading anything more (the change above only changes the log message)

Similarly interface_http.py checks that:

  • a single urgent byte does not disturb an ordinary request.
  • a client reset while the server has unsent responses (the HTTP change only changes the log message)

The first two commits follow up on review comments in #36716: the ErrorEvent documentation now describes the behavior by poll() and select() rather than by platform, as suggested in #36716 (comment), and a comment is dropped from the wait_ignores_urgent_data unit test, as suggested in #36716 (comment).

To support the new tests, the test framework gains:

  • wait_for_send_stall(), moved out of p2p_ping_ibd.py
  • a reset option for peer_disconnect(), which closes the connection with a TCP reset
  • mine_large_block() returns the block hash and size
  • pack_linger(), and linux_host / windows_target to tell which log message to expect

@DrahtBot

DrahtBot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

External sites

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
ACK jeanpablojp

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #36434 (http: support unix sockets by pinheadmz)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@Sjors
Sjors force-pushed the 2026/11/http-socket-errors branch from 0ac83b6 to ccd8095 Compare November 25, 2026 14:50
@Sjors
Sjors force-pushed the 2026/11/http-socket-errors branch 2 times, most recently from 4713c03 to 08172c5 Compare November 26, 2026 16:48
@Sjors

Sjors commented Nov 26, 2026

Copy link
Copy Markdown
Member Author

#36716 landed, rebased.

I made the check_reset_while_sending test easier to understand.

@Sjors
Sjors marked this pull request as ready for review November 26, 2026 16:49
@Sjors
Sjors force-pushed the 2026/11/http-socket-errors branch from 08172c5 to 020ff3b Compare November 27, 2026 20:47
@Sjors

Sjors commented Nov 27, 2026

Copy link
Copy Markdown
Member Author

Trivial rebase after #36916.

Sjors and others added 8 commits December 1, 2026 11:19
Name the wait mechanism rather than the platform in the ErrorEvent
documentation. No behavior change.

Co-authored-by: Matthew Zipkin <[email protected]>
Move it out of p2p_ping_ibd.py and return the stalled byte count.
Check that urgent indications do not prevent ordinary P2P input from
being processed or count as received message bytes.

Co-authored-by: Matthew Zipkin <[email protected]>
Reset a slow reader with block output stalled and more input queued
behind receive backpressure.

Add pack_linger() and the linux_host and windows_target checks
to the framework. Give peer_disconnect() a reset option and have
mine_large_block() return the block hash and size.
On Linux a socket error or hangup is reported whichever events were
requested. Disconnect directly when no read was requested, skipping
the send attempt that would otherwise fail and disconnect the peer.
This also changes the debug log message.

Co-authored-by: Cory Fields <[email protected]>
Check that an urgent byte does not disturb an ordinary request, and
reset a client while the server has unsent responses.

Co-authored-by: Matthew Zipkin <[email protected]>
When a socket error or hangup is reported and no read was requested,
disconnect before sending instead of reading. This also changes the
debug log message.

Co-authored-by: Matthew Zipkin <[email protected]>
@Sjors
Sjors force-pushed the 2026/11/http-socket-errors branch from 020ff3b to 4a9e07c Compare December 1, 2026 12:18

@jeanpablojp jeanpablojp left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tACK 4a9e07c

Tested on Linux and macOS.

# Send more pings while receiving is paused. Give them time to reach the
# node's kernel, as the reset discards anything not yet delivered.
peer.send_raw_message(pings)
time.sleep(1 * self.options.timeout_factor)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test takes 42 to 44 s with CI's --timeout-factor=40, against 2 to 3 s without the factor.

Without the sleep, the last check still failed locally when I made the node read the pings. Wouldn't it be better for the sleep to be one second?

Comment on lines +919 to +920
conn.conn.sock.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, pack_linger(enabled=True, timeout=0))
with self.node.assert_debug_log(expected_msgs, timeout=10):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The suggestion below fails on Linux without client->Disconnect(), and passes on Linux and macOS with it.

Suggested change
conn.conn.sock.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, pack_linger(enabled=True, timeout=0))
with self.node.assert_debug_log(expected_msgs, timeout=10):
port = conn.conn.sock.getsockname()[1]
expected_msgs.append(f"Disconnecting HTTP client 127.0.0.1:{port} (")
conn.conn.sock.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, pack_linger(enabled=True, timeout=0))
with self.node.assert_debug_log(expected_msgs, timeout=10):

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants