Skip to content

httpclient: an https open still connecting at RingHttpClient.Dispose is closed before its handshake, instead of calling SSL_new on the freed context (#611) - #624

Closed
MDA2AV wants to merge 1 commit into
mainfrom
fix-611
Closed

MDA2AV wants to merge 1 commit into
mainfrom
fix-611

Conversation

@MDA2AV

@MDA2AV MDA2AV commented Nov 14, 2026

Copy link
Copy Markdown
Owner

Fixes #611.

What was wrong. RingHttpClient.Dispose disposes its pools, then frees the TlsClientContext they share. A pool's Dispose closes its idle connections, but an open it has already started runs on. Once that open's resolve or connect completed, ClientTransport went on to the handshake, and TlsClientStream.HandshakeAsync called SSL_new(context.Handle) without asking whether the context was disposed. SSL_new takes a reference on the SSL_CTX and SSL_CTX_free drops one, so the free is final whenever no SSL holds the context: no idle connection (the pools have just closed them), no request in flight and no handshake under way. The client's first request to an https origin, still connecting when the host disposes the client, is that case.

Reproduced at main 19aec5c, not only read. A scratch program (RingHttpClient.Start, GetAsync("https://localhost:<port>/") to a bare TCP listener, then Dispose() in the same reactor pass) died with SIGSEGV in 3 runs of 3. Under gdb the reactor thread faults inside libioxide_boringssl.so, called from SSL_new. With this change the same program runs clean in 3 runs of 3: the open fails for the disposed context, the pool is left with nothing open, and the listener sees the connection close after 0 bytes.

What changes. HandshakeAsync refuses a disposed context before SSL_new: it closes the socket, which it owns on failure as on its other failure paths, and throws ObjectDisposedException. TlsClientContext gets an internal IsDisposed over its existing _disposed flag, so there is no new state, and one check per handshake, none per request. RingHttpClient.Dispose and the handshake both run on the reactor thread, so for RingHttpClient the check is exact, and so it is for a caller-owned context disposed on its pool's reactor after the pool.

Files: src/clients/ioxide.httpclient/Tls/TlsClientStream.cs, src/clients/ioxide.httpclient/Tls/TlsClientContext.cs, tests/Ioxide.Tests.Http/RingHttpClientTests.cs. No core change.

What a caller notices.

  • An https open still connecting when its client is disposed now fails and closes its socket, where it called SSL_new on the freed SSL_CTX, which crashed the process in every run here. Its pool logs it like any failed connect, over two lines because ObjectDisposedException puts the object name on a line of its own: [httpclient] connect to localhost:443 failed: Cannot access a disposed object. / Object name: 'TlsClientContext'.
  • TlsClientContext.ConnectAsync on a disposed context closes the socket and throws ObjectDisposedException, now documented on the method.
  • A pool whose caller-owned context is disposed while the pool still runs fails every open after that with the same error, and backs off as after any failed open. Before, such an open worked for as long as some connection's SSL kept the SSL_CTX alive, and crashed once none did.

Tests. ring client: an https open still connecting at Dispose is closed before its handshake (Http suite, RingHttpClientTests.cs). It starts a request to https://localhost:<port>/ on a bare TcpListener, disposes the client in the same pass, so the open is still resolving or connecting, and waits for the open to end. It asserts that the pool has nothing open, that the open failed for the disposed TlsClientContext, and that the listener saw the connection close after 0 bytes, so no ClientHello was sent.

  • The check removed: the Http suite process dies (exit 139) as this test runs. Under gdb: SIGSEGV on the reactor thread inside libioxide_boringssl.so, frame 1 SSL_new.
  • The check kept, without its socket.Dispose(): FAIL ring client: an https open still connecting at Dispose is closed before its handshake: expected [closed after 0 bytes], got [still open after 10 s, 0 bytes in].

Suites, built and run pinned to the safe CPUs: Unit 263 passed, E2E 730 passed, Http 105 passed, Tls 269 passed; 0 failed and 0 skipped in each.

Overlaps. git merge-tree --write-tree against the branches of all sixteen open PRs (#594 to #604, #619 to #623) exits 0 for every one.

For the reviewer.

  • Not closed here, as the issue says: a caller-owned context (HttpClientOptions.Tls) disposed on another thread than the reactor whose pool uses it, at the moment an open there starts its handshake. The reactor can pass the check just before the free and call SSL_new just after it. That covers a host disposing the context right after a pool.Dispose() made from another thread, which is only queued, and one context shared by pools on several reactors and disposed on one of them. The check narrows that race to the few instructions between it and SSL_new but does not close it. Closing it would need the context to hold its SSL_CTX in a SafeHandle and take a reference around SSL_new (DangerousAddRef and DangerousRelease), which also brings a finalizer and a thread-safe Dispose. I left that out as a change to the documented ownership (the caller owns the context's disposal) that is yours to make. The safe order today: dispose a caller-owned context on its pool's reactor after pool.Dispose(), or once that reactor has stopped.
  • A disposed pool reports the refused open as a failed connect on stderr. Skipping that log while _disposed would be a change in HttpClientPool.OpenOneAsync, which httpclient: a request waiting out a pool's reconnect backoff ends when its reactor stops, rather than waiting forever (#584) #596 also changes, so I left it.
  • The test reaches two private fields through UnsafeAccessor, as TlsClientTests already does for _idle, _tls and _disposed: RingHttpClient._origins, for the origin's pool before Dispose clears it, and HttpClientPool._lastOpenFailure, for why the open failed.

…is closed before its handshake, instead of calling SSL_new on the freed context (#611)

RingHttpClient.Dispose disposes its pools, then frees the TLS context they
share. A pool's Dispose closes only its idle connections, so an open it had
already started ran on, and once its resolve or connect completed the
handshake called SSL_new on the context. With no SSL left holding it, the
SSL_CTX was already freed, and the reactor thread died with SIGSEGV inside
SSL_new.

TlsClientStream.HandshakeAsync now refuses a disposed context before SSL_new:
it closes the socket, as on its other failures, and throws
ObjectDisposedException. Dispose and the handshake both run on the reactor
thread, so for RingHttpClient, and for a caller-owned context disposed on its
pool's reactor after the pool, the check is exact.

Test: "ring client: an https open still connecting at Dispose is closed before
its handshake". Without the check the Http suite dies with SIGSEGV in SSL_new
on the reactor thread; with the check but without closing the socket it fails
with "expected [closed after 0 bytes], got [still open after 10 s, 0 bytes
in]".
@MDA2AV

MDA2AV commented Nov 15, 2026

Copy link
Copy Markdown
Owner Author

Folded into #636, now merged: its fix, the disposed-context check before SSL_new in TlsClientStream, is the one #636 carries, line for line, and #636 closed #611.

@MDA2AV MDA2AV closed this Nov 15, 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

Development

Successfully merging this pull request may close these issues.

httpclient: RingHttpClient.Dispose frees its TLS context while a pool may still be connecting, and that connect then calls SSL_new on the freed context

1 participant