Review report

bitcoin/bitcoin: PR #36751

1 minor finding · 1 suggestion

Findings

Link

[minor] · Forced chunking discards datagram tails and can block shutdown

Applying the receive cap to datagram sockets causes port-mapping failures and can leave a blocking netlink gateway query preventing shutdown in manual runs of this diagnostic branch.

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

Details and suggested fix

Sock::Recv previously passed through the caller's buffer length, but now caps every socket without checking its type. For an isolated small PCP or NAT-PMP UDP reply, the cap is one byte. On POSIX, receiving that byte discards the rest of the datagram, so the parser rejects the reply, including the unsupported-version response needed for NAT-PMP fallback. Retries cannot recover the discarded tail. Linux/FreeBSD gateway discovery also uses SOCK_DGRAM, on a netlink socket that CreateSockOS leaves blocking. If truncation prevents parsing a gateway or completion message before the response queue drains, the next receive can block indefinitely without reaching the error or cumulative-byte-limit exits. InterruptMapPort only signals the interrupt object; shutdown then waits in StopMapPort for this thread. Apply the cap only to SOCK_STREAM, for example using SO_TYPE, and preserve the caller's length for datagram sockets. These failures come from the reproducer rather than incorrect datagram callers.

Mapping is enabled by default for manual runs, while -natpmp=0 disables it and proxy/non-listening parameter interactions disable it by default. Standard unit and functional setups disable mapping, and PCP unit mocks override Recv. The shutdown consequence depends on netlink probe behavior and response layout; no observed hang has been reported.


Optional improvements

Link

[suggestion] · sock_tests/send_and_receive assumes a complete message in one receive

The reproducer makes the existing four-byte receive assertion fail predictably; accumulating short reads would make this test tolerate valid TCP chunking.

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

Details and suggested fix

TcpSocketPair::send_and_receive sends "abcd" over real blocking TCP sockets and immediately expects one Recv to return all four bytes. Before the first receive, at most four bytes can be available, so the new wrapper requests only one byte even if the availability probe runs before arrival or fails. The return-count assertion therefore fails, and the helper leaves unread bytes queued for the moved-socket repeat. This assumption already exists in base and should be classified as a test-assumption failure in the reproducer's results. Accumulate receive results until msg_len bytes arrive, handling EOF/errors and bounding the wait, before comparing the payload.


Coverage details