Repository navigation
Closed
Conversation
…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]".
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #611.
What was wrong.
RingHttpClient.Disposedisposes its pools, then frees theTlsClientContextthey share. A pool'sDisposecloses its idle connections, but an open it has already started runs on. Once that open's resolve or connect completed,ClientTransportwent on to the handshake, andTlsClientStream.HandshakeAsynccalledSSL_new(context.Handle)without asking whether the context was disposed.SSL_newtakes a reference on theSSL_CTXandSSL_CTX_freedrops 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, thenDispose()in the same reactor pass) died with SIGSEGV in 3 runs of 3. Under gdb the reactor thread faults insidelibioxide_boringssl.so, called fromSSL_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.
HandshakeAsyncrefuses a disposed context beforeSSL_new: it closes the socket, which it owns on failure as on its other failure paths, and throwsObjectDisposedException.TlsClientContextgets an internalIsDisposedover its existing_disposedflag, so there is no new state, and one check per handshake, none per request.RingHttpClient.Disposeand 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.
SSL_newon the freedSSL_CTX, which crashed the process in every run here. Its pool logs it like any failed connect, over two lines becauseObjectDisposedExceptionputs the object name on a line of its own:[httpclient] connect to localhost:443 failed: Cannot access a disposed object./Object name: 'TlsClientContext'.TlsClientContext.ConnectAsyncon a disposed context closes the socket and throwsObjectDisposedException, now documented on the method.SSL_CTXalive, 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 tohttps://localhost:<port>/on a bareTcpListener, 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 disposedTlsClientContext, and that the listener saw the connection close after 0 bytes, so no ClientHello was sent.libioxide_boringssl.so, frame 1SSL_new.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-treeagainst the branches of all sixteen open PRs (#594 to #604, #619 to #623) exits 0 for every one.HttpClientPool.cs(the backoff retry), adds its own test file and registers it inProgram.cs; this PR changes none of those. Its retry checks_disposedbefore it connects, and this check covers a connect already in flight, so the two compose with nothing to resolve.HttpClientConnectTests.cs; no file in common.TlsClientContext.Dispose, asRingHttpClient.Dispose's does, it sets the same flag, so an open that outlives it meets this check too. If its PR also appends tests at the end ofRingHttpClientTests.Register, that textual conflict resolves by keeping both.For the reviewer.
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 callSSL_newjust after it. That covers a host disposing the context right after apool.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 andSSL_newbut does not close it. Closing it would need the context to hold itsSSL_CTXin aSafeHandleand take a reference aroundSSL_new(DangerousAddRefandDangerousRelease), which also brings a finalizer and a thread-safeDispose. 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 afterpool.Dispose(), or once that reactor has stopped._disposedwould be a change inHttpClientPool.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.UnsafeAccessor, asTlsClientTestsalready does for_idle,_tlsand_disposed:RingHttpClient._origins, for the origin's pool beforeDisposeclears it, andHttpClientPool._lastOpenFailure, for why the open failed.