0.3.4 -- cancel a request in flight with a std::stop_token by yspbwx2010 · Pull Request #23 · mcpplibs/tinyhttps
Repository navigation
0.3.4 -- cancel a request in flight with a std::stop_token - #23
Merged
Sunrisepeak merged 15 commits into mcpplibs:master from yspbwx2010:feat/cancel Oct 1, 2026
Merged
0.3.4 -- cancel a request in flight with a std::stop_token #23 Sunrisepeak merged 15 commits into mcpplibs:master from yspbwx2010:feat/cancel
Conversation
yspbwx2010 commented Oct 1, 2026 · edited by Sunrisepeak Loading
Copy link Copy Markdown
Contributor
A request that has been sent cannot be abandoned. A caller that stops caring about the response (a user who pressed Esc, a deadline that passed elsewhere) can only leave the call running on a thread and wait for readTimeoutMs, with the connection held open and the server still producing the answer. The TLS handshake has no timeout at all, so a server that accepts the connection and never replies holds the call for good.
This adds an optional std::stop_token to send, send_stream and download_to_file. Stopping its source from another thread ends the call within about 50 ms and closes the connection. Released as 0.3.4 : everything is added, nothing changes without a token, and every dependency written tinyhttps = "0.3.x" receives it.
HttpResponse response;
std::jthread worker([&](std::stop_token stop) { response = client.send(request, stop); });
// elsewhere:
worker.request_stop();
Design and plan: .agents/docs/design-cancel-stop-token.md, .agents/docs/exec-plan-0.3.4-cancel.md.
API
HttpResponse send(const HttpRequest&, std::stop_token stop = {});
HttpResponse send_stream(const HttpRequest&, SseCallbackFn, std::stop_token stop = {});
DownloadToFileResult download_to_file(const std::string& url, const std::filesystem::path&,
DownloadProgressFn = nullptr,
std::function<bool()> isCancelled = nullptr,
std::stop_token stop = {});
HttpResponse::cancelled and DownloadToFileResult::cancelled say why the call ended, so nobody has to match text. Before the status line: statusCode 0, text Cancelled. After it: the server's status, bodyError / error cancelled. ok() does not look at cancelled, as it does not look at bodyComplete. A cancelled request is not retried and its redirect is not followed. isCancelled keeps working and sets cancelled too.
proxy_tunnel takes the token as a trailing default argument; Socket and TlsSocket gain set_stop, Socket gains stop_possible. A call by name compiles unchanged; a pointer to one of the three members needs the new parameter in its type.
How it works
Three invariants:
- The token lives only on the socket at the bottom of a connection. Every wait ends there (
Socket::wait_fd), so that is where it is set;TlsSocket::set_stopforwards to the session beneath it, which inside an https:// proxy's tunnel is where the waits are. - It lives only for one call.
perform_exchangesets it;PooledConnection::keep()takes it off when the connection goes back to the pool, so nothing in the pool carries a token. A token stopped after its call returned (astd::jthreaddoes that in its destructor) cannot reach the next call. - Without a token nothing changes. Every wait is the single
pollit was, andbio_recvdoes not poll first.
With a token, waits are polls in slices of at most 50 ms with a check between them, the connect checks before each address, and bio_recv waits before recv so the handshake and the second half of a TLS record are covered. A stop wins over whatever error the failure would otherwise report. shutdown(fd) from the stopping thread and a wake-up pipe were considered and rejected (fd-reuse races, no pipe on Windows, unmeasured connect semantics on macOS/Windows/openkal); slicing uses only the poll already used on every platform.
Not covered: name resolution, a write blocked because the server is not reading, and the connect above openkal (synchronous there).
Found and fixed on the way
- A reused https:// proxy tunnel kept the first call's token.
set_stopset only the outer session's own socket, which is unused inside a tunnel. Once that token was stopped, every wait on the tunnel failed at once, and the stale-connection retry sent a POST the target had already received a second time (reproduced: three requests at the target for two calls); a later call's own token never reached the tunnel. Fixed by invariants 1 and 2; covered directly, through http:// and through https:// proxies. - TLS never worked above openkal on x86_64-windows-musl. mbedTLS seeds from
/dev/urandomunless it recognises glibc, and that file does not exist there: every handshake failed withCTR_DRBG - The entropy source failed. Where the C library is musl (cfg(c-abi = "musl"), as for the socket interface), the generator is now seeded from musl'sgetentropy, which openkal-musl answers throughopenkal.random. - libc++ 20 and 22 fail to link
std::stop_source::request_stop()in a file that includes<thread>or<condition_variable>beforeimport std;(an inline helper is never emitted); libstdc++ needs the opposite order.test_cancel.cppchooses by_LIBCPP_VERSION; README tells users.
Tests
tests/test_cancel.cpp, 24 cases against the in-process TLS server, a listener that never speaks, and the proxy servers. Read timeout 30 s, so only the token can end them.
- Stopped while waiting for the head, between stream chunks, during the handshake, before the call, during connect; a timeout left alone without a stop.
- Stopped while an HTTP, https or SOCKS5 proxy has not answered, and during the handshake inside an https proxy's tunnel.
- Not retried, redirect not followed, cancelled connection not reused, 20 cancellations leak no descriptors.
- Reuse, ×{direct, http proxy, https proxy}: a stop after the call returned does not reach the next call (a POST arrives once, the connection is reused); the next call's own token reaches the reused connection.
download_to_file: cancelled before the head (no file created), during the body, with a token already stopped, and byisCancelled.
Removing the forwarding in TlsSocket::set_stop fails exactly the two https-proxy reuse cases.
CI
| job | runs |
|---|---|
| linux, gcc@16.1.0 and llvm@22.1.8 | every test, templates |
| macos-15 arm64, llvm@22.1.8 | every test (new) |
| windows-2022, msvc and llvm@20.1.7 | test_ca_store + test_framing, test_tls_verify, test_pool, test_proxy, test_cancel (new) |
| above openkal: x86_64-linux-gnu, x86_64-linux-musl, aarch64-linux-musl (qemu), x86_64-windows-musl (wine) | examples/openkal: parsers, a cancelled handshake against a local listener, one real HTTPS request |
Above openkal on Windows there is no name resolution (openkal has no resolver interface; musl reads /etc/resolv.conf), so the HTTPS request there reports "no network"; the cancellation check needs none.
feat(http): let another thread cancel a request in flight
f96897e
There was no way to abandon a request that had been sent. A caller that stopped caring (a user pressing Esc, say) could only leave the call running on a thread and wait out readTimeoutMs, with the connection held open and the server still producing the response. send and send_stream take an optional std::stop_token. While a token is attached, every wait on the socket is made in slices of at most 50 ms with a check for a stop request between them, and bio_recv waits for the socket before reading, so a handshake or the second half of a TLS record is covered too. When the token is stopped the call returns within about one slice, and the connection is dropped rather than pooled, so the server sees it end. A close_notify goes out only when the stop comes after the handshake has finished. HttpResponse::cancelled says why the call ended, without matching text. Before a status line has arrived the response is statusCode 0 with statusText and bodyError "Cancelled"; after it, statusCode is the server's and bodyError is "cancelled". A cancelled request is not resent on a fresh connection by the stale-connection retry, and a redirect is not followed. proxy_tunnel and proxy_connect take the token as a trailing default argument and hand it to the socket or the TLS session they open, so the exchange with an HTTP, https or SOCKS5 proxy is covered as well. Without a token (stop_possible() is false) every wait is the single poll it was, and bio_recv does not poll first. Not covered: getaddrinfo, a write blocked because the peer is not reading, and connect on openkal, where connect completes synchronously. download_to_file is unchanged. Tests in test_cancel run against the in-process TLS server and a listener that accepts and says nothing: stopped while waiting for the response head, while a stream waits for its next chunk, and during the handshake, each with a 30 s read timeout so only the token can have ended it; a token already stopped sends nothing; a token never stopped leaves a 300 ms timeout alone; a cancelled connection is not reused; a cancelled request on a pooled connection is not retried; and 20 cancelled requests leave the descriptor count where it was. Not run on macOS or Windows; the change adds no platform-specific code beyond the poll the sockets already use.
fix(socket): do not start a connect once a stop has been requested
4fab0e2
A stop that arrived while one address was being tried did not end the connect. The loop went on to the remaining addresses and sent each of them a SYN, and when all had failed the manual DNS fallback ran, which does not look at the token and waits up to 2.5 s for each nameserver. Check the token before each connect attempt, before each lookup and before the fallback. A lookup that has already started still cannot be interrupted.
test(cancel): cover proxies, connect and redirects, and bound the tim...
fb5d38a
...eout test * Cancelling while an HTTP proxy has not answered CONNECT, while an https proxy has not finished its handshake, and while the handshake with the target inside an https proxy's tunnel has not been answered. * A redirect is not followed once the request has been cancelled. * A connect after the stop sends nothing to the listener. * The timeout test carries a stop 5 s in, so a timeout that never comes fails it instead of holding the suite until its own limit. * The stream test uses the same stopper as the others, and a stopper that is no longer needed does not wait out its delay. * The file starts with the includes and then imports std, like the other test files.
docs: cancellation reaches the proxy hop, and where ok() stands
8996984
The README example uses the jthread's own token. CHANGELOG now names the proxies the stop reaches and the Socket and TlsSocket members that are new, and both say that ok() does not look at cancelled, as it does not look at bodyComplete.
fix(cancel): a token belongs to its call; downloads take one; openkal...
a778ca1
... Windows has entropy The token is set on the socket at the bottom of a connection and nowhere else. TlsSocket::set_stop forwards to the session beneath it, which is where every wait happens inside an https:// proxy's tunnel; setting only its own socket left a reused tunnel on the token of the call that opened it. Once that token was stopped, which a std::jthread does in its destructor, every wait on the tunnel failed at once and the stale-connection retry sent a POST that had already arrived a second time; a later call's own token never reached the tunnel at all. PooledConnection::keep() takes the token off the connection it returns to the pool, so nothing in the pool carries one. download_to_file takes a std::stop_token as its last argument, and DownloadToFileResult::cancelled is set by it or by isCancelled, as HttpResponse::cancelled is. proxy_connect, the wrapper nothing here calls, does not take one. Where the C library is musl, the TLS random generator is seeded from musl's getentropy rather than mbedTLS's reading of /dev/urandom, which above openkal on x86_64-windows-musl does not exist: every handshake there failed with "CTR_DRBG - The entropy source failed". Tests: a stop after the call returned does not reach the next call, and the next call's token does, each directly, through an http:// proxy and through an https:// proxy; a SOCKS5 proxy that never answers; a download cancelled before the head, during the body, with a token already stopped, and by isCancelled. test_cancel imports std before its includes under libc++, whose 20 and 22 fail to link stop_source::request_stop() the other way round. examples/openkal follows mcpp-index's openkal pins (runtime 0.15.2) and cancels a handshake against a local listener, which needs no network; it runs on x86_64-linux-gnu, x86_64-linux-musl, aarch64-linux-musl and x86_64-windows-musl.
ci: libc++ on Linux, a macOS job, the hermetic tests on Windows, four...
180ab96
... openkal targets Linux runs every test under gcc 16 and llvm 22. macOS (arm64, llvm 22) runs every test; nothing ran there before. Windows runs test_framing, test_tls_verify, test_pool, test_proxy and test_cancel besides test_ca_store. examples/openkal runs for the four targets mcpp-index measures tinyhttps on, under wine and qemu where the host cannot run the program itself.
0.3.4 --- docs: cancellation for all three entry points, openkal note...
5164fd6
...s, plan README and CHANGELOG describe the cancellation contract, download_to_file's token, the libc++ link defect and what differs above openkal on Windows. .agents/docs holds the design and the execution plan.Sunrisepeak changed the title Cancel a request in flight with a std::stop_token 0.3.4 -- cancel a request in flight with a std::stop_token Oct 1, 2026
test(pool): a Windows build does not meet <windows.h>'s max macro
49cc935
speak-agent
mentioned this pull request
Oct 1, 2026
x86_64-windows-musl: getaddrinfo cannot resolve a host name (no /etc/resolv.conf on Windows)
mcpplibs/openkal-musl#46
Open
speak-agent
added 7 commits
October 2, 2026 03:28
docs: name the openkal-musl issue for name resolution on Windows; rec...
e5beace
...ord what the plan found
ci: mcpp 2026.9.30.2, which lets openkal-llvm-runtime supply x86_64-w...
d734d65
...indows-musl
ci: install only the runner a target needs, without recommends, and b...
a977fdb
...ound the step
ci: build x86_64-windows-musl here and run it on Windows rather than ...
c92034a
...under wine wine took from four to more than thirty minutes to install on the Linux runners. The program is a static PE that imports only system DLLs, so the Linux job builds it and a windows-2022 job runs it, with Git for Windows's CA bundle beside it.
docs: x86_64-windows-musl is run on Windows in CI, under wine locally
927ed8a
ci: name llvm@22.1.8 for the openkal targets, as mcpp-index's measure...
49efc68
...ment does
ci: install llvm 22.1.8 before building for an openkal target
10e28b0
Sunrisepeak
merged commit 156edeb
into
mcpplibs : master
Oct 1, 2026
10 checks passed
Sunrisepeak
mentioned this pull request
Oct 1, 2026
tinyhttps 0.3.4
mcpplibs/mcpp-index#504
Merged
Sunrisepeak
added a commit
to mcpplibs/mcpp-index
that referenced
this pull request
Oct 1, 2026
tinyhttps 0.3.4 (#504)
265f3c6
A request in flight can be cancelled with a std::stop_token (mcpplibs/tinyhttps#23). A patch of a version consumers already name, so ^ resolution reaches it without a manifest change. The tinyhttps member moves to 0.3.4 and checks the new API without a network: a token stopped before send and download_to_file ends both with cancelled set and statusCode 0, and HttpResponse's four-initialiser brace-initialisation still compiles with the new field defaulted.yspbwx2010 deleted the feat/cancel branch October 9, 2026 07:41 yspbwx2010 mentioned this pull request Oct 9, 2026 Bound the TLS handshake with connectTimeoutMs #25 Merged Sign up for free to join this conversation on GitHub . Already have an account? Sign in to comment