Skip to content

0.3.7 — the read timeout bounds every recv and send, and a request body can be streamed - #29

Merged
Sunrisepeak merged 4 commits into
mcpplibs:masterfrom
speak-agent:feat/idle-bound-and-body-source
Dec 4, 2026
Merged

Sunrisepeak merged 4 commits into
mcpplibs:masterfrom
speak-agent:feat/idle-bound-and-body-source

Conversation

@speak-agent

Copy link
Copy Markdown
Contributor

Closes #28.

What changes

readTimeoutMs bounds every recv and send. The wait before a read was already bounded. The recv that mbedtls makes to complete a TLS record, once the record's first bytes have arrived, was not. A peer that stopped inside a record therefore held send, send_stream and download_to_file indefinitely, and a peer that stopped reading held a write the same way.

  • Socket::set_idle_timeout: each wait before a recv or send of the connection is bounded, and bio_recv and bio_send wait against that bound. The client sets it to readTimeoutMs for every exchange, after the connection is open, so inside an https:// proxy's tunnel the bottom socket carries it.
  • SO_SNDTIMEO is set to the same value. It covers a send that the poll found room for, but that needs more room than was free.
  • write_all bounds the time without progress, not the whole write. A large body on a slow link is no longer cut while it is still moving.
  • A stop token now also ends a write that is blocked because the peer is not reading.
  • The errors are the existing ones: Read timed out before the declared length, No response, Write failed.

Streamed request body. BodySource holds a size and a function that reads the bytes at a given offset. HttpRequest::bodySource sends it with Content-Length, in 64 KiB slices, and BodySource::file(path) reads a file. When the request is sent again (a stale pooled connection, or a 307/308 redirect), the source is read again from offset 0. A source that fails ends the request with Request body source failed at byte N of M, and the server, still owed bytes, has not acted on it. A request that sets both body and bodySource is refused before anything is sent.

The release is a patch release, 0.3.7. Everything is additive. The only change in behaviour is that a stall inside a record now fails after readTimeoutMs instead of hanging.

Verification

  • tests/test_idle_timeout.cpp: the peer stops inside a record in the head, in the body, during a stream and during a download; another peer stops reading; a 32 MiB upload keeps moving for longer than the bound. With readTimeoutMs = 500, the five stall cases end in about 0.6 s, or 1.2 s for the write. With the idle bound removed (a mutation run), they wait the full 20 s the peer holds the connection.
  • tests/test_body_source.cpp: the bytes the server received are compared for a generated source and for a file; also covered are a source that fails partway, both bodies set at once, and a stale pooled connection that sends the source again from the start.
  • tests/tls_test_server.hpp: Conn::write_cut (a record cut partway), Conn::read_some, Conn::body, and an AfterHandshake hook.
  • Local: mcpp test passes all 13 programs with gcc 16.1.0 and with llvm 22.1.8 (libc++).
  • The Windows job runs the two new programs with the other hermetic tests.

Authorship: ai-and-human

…ody can be streamed (mcpplibs#28)

A peer that stopped inside a TLS record held send, send_stream and
download_to_file for good: the wait before a read was bounded, the recv mbedtls
makes to complete the record was not. A peer that stopped reading held a write
the same way. Each recv and send now waits at most readTimeoutMs for progress
(Socket::set_idle_timeout, wait_before_send, SO_SNDTIMEO), and write_all bounds
the time without progress instead of the whole write.

BodySource streams a request body of known length from a reader by offset;
BodySource::file reads a file. tests/test_idle_timeout.cpp and
tests/test_body_source.cpp cover both.
The 32 MiB upload to a peer that slept 1 ms per record read at about 1 MB/s on
Windows, whose timer ticks every 16 ms, and the writer then waited past the
1 s bound for room (Windows CI, both toolchains). An upload whose source is slow
and a download whose server is slow move at a pace every system keeps.
@Sunrisepeak
Sunrisepeak merged commit 7d2be50 into mcpplibs:master Dec 4, 2026
10 checks passed
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.

readTimeoutMs does not bound a read that stops inside a TLS record, or a write the peer stops reading

2 participants