Skip to content

Bound the wait of WebSocket::close() as a whole (alternative to #2667) - #2678

Open
yhirose wants to merge 1 commit into
masterfrom
websocket-close-deadline
Open

yhirose wants to merge 1 commit into
masterfrom
websocket-close-deadline

Conversation

@yhirose

@yhirose yhirose commented Nov 17, 2026

Copy link
Copy Markdown
Owner

An alternative to #2667 for comparison.

WebSocket::close() waits for the peer's Close reply for
CPPHTTPLIB_WEBSOCKET_CLOSE_TIMEOUT_SECOND, but the timeout applied to each
read. A peer that kept sending other frames, or sent one frame a byte at a
time, held close() for as long as it liked.

This bounds close()'s own wait as a whole, with the existing
Stream::set_max_timeout() (the mechanism that bounds the request head) and
a deadline check in the frame loop:

  • The max timeout shortens each wait to what is left, which stops a peer
    that trickles a frame.
  • The deadline check stops a peer that keeps the socket full, where no wait
    happens at all.
  • WebSocketSSLStream gains set_max_timeout(), the one stream a WebSocket uses
    that lacked it.

How it differs from #2667:

  • Bound the wait of a read() left running by WebSocket::close() #2667 also ends a read() running on another thread when close() is called.
    To do that portably it makes every WebSocket read wait in 100 ms slices,
    also by default and on connections that never call close(). Here that case
    stays as currently designed and documented: the other thread's read() waits
    up to its own read timeout. README-websocket.md and the graceful shutdown
    cookbook page now say how to bound it with set_read_timeout().
  • No ABI change in WebSocket. The tests run in test_websocket_heartbeat, which
    is built with a 1 second close timeout, so they take about 1 s each instead
    of the real 5 s.

Tests (WebSocketCloseTest in test_websocket_heartbeat): a raw ws and wss
client that never answers the server's close() and either floods empty Pings
or sends a 20-byte Ping a byte at a time. Each fails without the piece it
covers: the flood without the deadline check, the trickle without the max
timeout, and the wss trickle without WebSocketSSLStream::set_max_timeout().

close() waited for the peer's Close reply with a read timeout that
applied to each read, so a peer that kept sending other frames, or sent
one frame a byte at a time, held close() for as long as it liked.

Bound the wait as a whole. close() sets the stream's max timeout, which
shortens each wait to what is left, and stops reading frames at the
deadline, which matters when the peer keeps the socket full and no wait
happens at all. WebSocketSSLStream gains set_max_timeout(), the one
stream a WebSocket uses that lacked it.

A read() running on another thread when close() is called still waits
for the reply up to its own read timeout, as documented. The docs now
say how to bound it.

This branch has not been deployed

No deployments
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.

1 participant