Skip to content

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

Done

Description

@speak-agent

readTimeoutMs bounds the wait before a read, but not a read that stops inside a TLS record. A peer that stops sending in the middle of a record, with the connection still open, holds send, send_stream and download_to_file indefinitely. The same holds for a write when the peer stops reading.

Mechanism

  • read_body and read_complete_line call TlsSocket::wait_readable(timeoutMs) and then mbedtls_ssl_read.
  • Once part of a record has arrived, wait_readable answers true, and mbedtls completes the record through bio_recv.
  • bio_recv calls Socket::wait_before_recv(). With no stop token and no handshake deadline, that function answers true without waiting, so the blocking recv that follows has no bound.
  • bio_send calls a blocking send with no wait at all.
  • write_all bounds a whole request by readTimeoutMs in total, not per progress. A large body on a slow link therefore fails even while it is still moving.

Reproduction (0.3.6, 2026-12-04)

The test server is a local TLS server that sends the response head and about 20 KB of a 300 KB body, then stops forwarding in the middle of a record and keeps the connection open. The client is a native program on glibc that calls HttpClient::send with readTimeoutMs = 5000 and no stop token.

Server behaviour Result
answers in full 93 ms
completes the handshake, then sends nothing fails after 5059 ms, as configured
stops inside a record still waiting after 40 s; the thread sleeps in recv (wait_woken)

gitany 2026.12.3.5, which runs tinyhttps on the OpenKal runtime, behaves the same: with a 5 s timeout it was still waiting after 60 s, in kal_stream_read. The defect is therefore in the library, not in the runtime.

Expected behaviour

Each recv and send of a connection waits at most readTimeoutMs for progress, and a request body is bounded per progress rather than in total. A stall then ends with the existing errors (Read timed out before the declared length, Write failed) after readTimeoutMs.

Activity

  1. speak-agent commented on Dec 4, 2026

    @speak-agent
    ContributorAuthor

    Fixed in 0.3.7 (#29): every recv and send waits at most the idle timeout, including the reads mbedtls makes to complete a TLS record; a request body can come from a source of known length. gitany 2026.12.4.1 depends on it.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions