Skip to content

http: Lingering close on request parse errors - #2

Closed
winterrdog wants to merge 2 commits into
masterfrom
fix/linger-close-tk2
Closed

winterrdog wants to merge 2 commits into
masterfrom
fix/linger-close-tk2

Conversation

@winterrdog

@winterrdog winterrdog commented Aug 27, 2026 •

Copy link
Copy Markdown
Owner

fixes bitcoin#35632

when an incoming HTTP request has a parse error, we send an error
response and immediately close the connection. if the peer still has
data in flight, closing the socket with unread data can cause the OS to
send an RST instead of a clean close. this showed
up as ConnectionAbortedError: [WinError 10053] in a functional test
on Windows.

this adds a lingering close for these cases. after sending the error
response, we shut down the send side and drain any remaining data
before closing the socket. a fixed timeout makes sure that a peer
cannot keep the connection open indefinitely by continuously sending
small amounts of data.

Wrap the platform-specific shutdown() call behind the Sock interface.

This allows callers to shut down individual socket directions without
depending on platform-specific SHUT_* or SD_* constants.
Keep connections open after a parse error long enough to flush the error
response and drain unread request data before closing the socket.

This avoids an RST replacing the response when the peer still has
request data in flight.
@winterrdog

winterrdog commented Aug 27, 2026 •

Copy link
Copy Markdown
Owner Author

todos:

  • i think some tests might be missing. i am gonna investigate if they're needed because the test that exposed the issue is already in the current test suite

tradeoffs

  • initially, i had considered a dual lingering timeout i.e.
    1. a rolling soft timeout that gets pushed out whenever draining incoming data from a peer. it was to allow well behaved clients close off gracefully plus also avoid keeping the connection open for far too long till the long absolute timeout rolls around to close the socket and,
    2. an absolute hard timeout (set only once when a lingering close starts and checked in MaybeDisconnect() to determine when to close off the socket) to prevent abuse by clients who might trickle in data slowly which can lead to wastage on the server's part - but i found out that a single timeout will do, for now.
  • never went the way of setting SO_LINGER option on a socket because that only operates at the send (our transmission side) side and controls how to close() a socket (unACKed pkts get some treat from SO_LINGER here) but nothing about our receive side when a peer is still sending in data to us - which is the actual issue. therefore i realised an application-level lingering close will do because we're visibility into how the polite closing is going down unlike if i used the SO_LINGER way (nginx devs seem to be thinking the same..)

Note

this is a swing at what i think the fix could look like (that is subjective).

winterrdog pushed a commit that referenced this pull request Nov 6, 2026
…:__pad_and_output

fa3821e test: Add missing suppression for race:std::__1::__pad_and_output (MarcoFalke)

Pull request description:

  From https://github.com/bitcoin/bitcoin/actions/runs/41684419453/job/126086262840?pr=35990#step:11:8669:

  ```
  WARNING: ThreadSanitizer: data race (pid=17903)
    Write of size 8 at 0x7ff5c1990c20 by thread T12 (mutexes: write M0):
      #0 width /llvm-project/libcxx/include/ios:498:18 (libc++.so.1+0xd3816) (BuildId: 6e001266114c9106207bb18363e2d5a09b534306)
      #1 std::__1::ostreambuf_iterator<char, std::__1::char_traits<char>> std::__1::__pad_and_output[abi:dee230102]<char, std::__1::char_traits<char>>(std::__1::ostreambuf_iterator<char, std::__1::char_traits<char>>, char const*, char const*, char const*, std::__1::ios_base&, char) /llvm-project/libcxx/include/__locale_dir/pad_and_output.h:67:9 (libc++.so.1+0xd3816)
      #2 std::__1::ostreambuf_iterator<char, std::__1::char_traits<char>> std::__1::num_put<char, std::__1::ostreambuf_iterator<char, std::__1::char_traits<char>>>::__do_put_integral[abi:dee230102]<long long>(std::__1::ostreambuf_iterator<char, std::__1::char_traits<char>>, std::__1::ios_base&, char, long long) const /llvm-project/libcxx/include/__locale_dir/num.h:907:10 (libc++.so.1+0xbcfa0) (BuildId: 6e001266114c9106207bb18363e2d5a09b534306)
      #3 std::__1::num_put<char, std::__1::ostreambuf_iterator<char, std::__1::char_traits<char>>>::do_put(std::__1::ostreambuf_iterator<char, std::__1::char_traits<char>>, std::__1::ios_base&, char, long long) const /llvm-project/libcxx/include/__locale_dir/num.h:919:16 (libc++.so.1+0xbcb79) (BuildId: 6e001266114c9106207bb18363e2d5a09b534306)
      #4 put /llvm-project/libcxx/include/__locale_dir/num.h:780:12 (libc++.so.1+0x9b40c) (BuildId: 6e001266114c9106207bb18363e2d5a09b534306)
      bitcoin#5 std::__1::basic_ostream<char, std::__1::char_traits<char>>& std::__1::basic_ostream<char, std::__1::char_traits<char>>::__put_num[abi:dee230102]<long long>(long long) /llvm-project/libcxx/include/__ostream/basic_ostream.h:104:21 (libc++.so.1+0x9b40c)
      bitcoin#6 std::__1::basic_ostream<char, std::__1::char_traits<char>>::operator<<(long long) /llvm-project/libcxx/include/__ostream/basic_ostream.h:316:10 (libc++.so.1+0x9b2a0) (BuildId: 6e001266114c9106207bb18363e2d5a09b534306)
      bitcoin#7 KernelLog::LogMessage(btck::LogEntry const&) /home/runner/work/_temp/src/bitcoin-chainstate.cpp:49:33 (bitcoin-chainstate+0xf2667) (BuildId: cff46198c8fd29997404a749cd55fdd6fcb3e865)

ACKs for top commit:
  fanquake:
    utACK fa3821e

Tree-SHA512: 383705fe33e429d40baaf7811a3edd6ce5164c423a1450c7a7a5d749d1ba44b8dac7a1270cbb593fcd0888dd9a2efbe7507650adf69b2f46ac09db4488cb9aec
@winterrdog winterrdog closed this Nov 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant