Repository navigation
Forward a TLS-terminating connection to a peer when its local connect fails - #85
Merged
Merged
Conversation
There was a problem hiding this comment.
Code Review
This pull request enables TLS-terminating failover routes to connect to their upstreams before the TLS handshake, allowing the connection to be forwarded to a peer if the local connect fails. It introduces ALPN prediction to select the correct upstream destination early, handles replacing connections that close during the handshake, and adds comprehensive test coverage. A critical compilation issue was identified in src/upstream.rs where std::task::Context::from_waker is passed a Waker by value instead of by reference.
kriszyp
marked this pull request as ready for review
November 6, 2026 20:42
… fails A TLS-terminating failover route connected to its upstream after the handshake, which had already consumed the hello, so the connection that discovered a dead upstream was lost. It now connects while a complete hello is still unread, on a client listener and not suspended: a failure goes to the peers as raw TLS, and a success is kept for after the handshake. The destination is predicted from the hello's ALPN offer and the route's protocol list. The negotiated protocol still decides; the early connection is replaced if it disagrees, or if the reactor has seen the upstream close it. A route that requires a client certificate stays handshake-first, so a client with no certificate cannot reach the upstream. Refs #83 Dispatch-Task: symphony-83-tls-terminating-connect-forward Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Dispatch-Task: pr-maint-b9fcd85adedac6da46e26060a31608c4 Co-Authored-By: Gemini <noreply@google.com>
A client that closes right after its reply can leave a stream error on the route, depending on timing, so asserting no errors at all fails intermittently. Refs #83 Dispatch-Task: symphony-83-tls-terminating-connect-forward Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Dispatch-Task: pr-maint-b9fcd85adedac6da46e26060a31608c4 Co-Authored-By: Gemini <noreply@google.com>
kriszyp
force-pushed
the
fix/tls-terminating-connect-forward
branch
from
November 6, 2026 21:08
1a882ba to
786025b
Compare
kriszyp
added a commit
that referenced
this pull request
Nov 28, 2026
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.
⊙ Problem
On a TLS-terminating failover route, the first connection after the local upstream stops is dropped, even with a healthy peer. The route connects after its handshake, which has already read the ClientHello, so nothing is left to send to a peer. Forward a passthrough connection to a peer when its local connect fails covered passthrough only, and Fabric's Unix-socket routes all terminate TLS.
💡 Solution
⚖️ Alternatives
🤔 Should a hello too large to replay connect early?
No. A hello over 16 KiB cannot be forwarded, so a dead h1 destination would close a client that a healthy h2 destination can serve.
🤔 Should routes requiring a client certificate connect early?
No. A client with no certificate could hold Harper sockets before authenticating, with connection limits off by default. These routes still lose the first connection after an outage. Flipping this is one condition.
🤔 Is the new exposure acceptable on other routes?
A client that abandons its handshake holds a Harper socket, with no bytes sent, for up to the handshake timeout. A hello flood can overrun the accept queue and push the route to its peers. Finishing the handshake already allowed a longer hold.
🤔 Is the added cost acceptable?
Performance is below. It buys the first post-outage connection.
🔧 Changes
Connecting before the handshake
upstream_connect1,forwarded0.upstream_connect1,forwarded1.proxy_conn.rs:382
A first connection after the upstream stops
The peer terminates TLS with its own copy of the certificate, so the client handshakes with it through the tunnel.
sequenceDiagram participant Client participant Symphony participant Local as Local upstream participant Peer Client->>Symphony: ClientHello (peeked, unread) Symphony->>Local: connect alt connect fails Symphony->>Peer: PROXY v2 + ClientHello Peer-->>Client: handshake through the tunnel else connect succeeds Symphony-->>Client: handshake Symphony->>Local: PROXY header, then client bytes endWhich connections connect early
Choosing h1 or h2 before ALPN is known
rustls negotiates the first server protocol the client offered. Symphony's list is either empty or starts with h2, so the prediction needs one bit from the hello. Fabric routes set neither
http2nor an h2 upstream today, so on them this has no effect.proxy_conn.rs:500
Holding a connection across the handshake
Example: a slow client on a long handshake timeout
Harper destroys a socket that sends nothing for 10 s. A device takes 11 s to finish its handshake, on a route with
tlsHandshakeTimeoutMs: 20000.Outcome:
closed()sees the close when the handshake ends, and symphony connects again. The client is served.closed()reads the reactor's read-closed readiness rather than the socket. A peek cannot see a close behind unread data, and costs a syscall. The readiness check can miss a close the reactor has not yet processed; that window is sub-millisecond, and the connection is then lost.Files
src/proxy_conn.rs: the early connect and its conditions; what is left of the timeout goes to the handshake; the reuse check;predicts_h2;connect_or_forward, moved from the passthrough branch;proxy_via_tlsborrows the connection, likeproxy_raw;destination_for; prediction tests against real rustls.src/sni.rs:PeekInfo::offers_h2, set fromalpn_offers_h2, with parser tests.src/upstream.rs:UpstreamStream::closedoverread_closed, with socket tests.src/router.rs:Route::requires_client_cert, set from the same policy as the TLS config.src/protection.rs: a test fixture for the newPeekInfofield.README.md: when a connection is forwarded, h1 and h2, the shared timeout, the exposure, which connections are still lost, whatupstream_connectcounts.__test__/failover.spec.ts: the first-connection cases run in both route modes; TLS-terminating cases;markDownnow goes through ingress, so existing tests keep their counts.__test__/harper-integration.spec.ts: aborted handshakes and a handshake past Harper's idle limit against a real Harper.__test__/util.tsadds the slow relay.CLAUDE.md: the data-flow diagram, the forwarding rule, the invariants of a held connection, the test notes.✅ Verification
npm test: 294 pass.cargo test: 249 pass. Clippy and fmt clean.src/from 1b1fd72, 18 offailover.spec.ts's 65 cases fail, all TLS-terminating (actual: null, expected: 'ping').Performance: every connection gains one ALPN scan, 48 ns of
parse_client_hello's 4.37 µs, and 24 bytes of future (3488 to 3512, debug layout, budget 4096). An early-connecting connection adds two clock reads and oneclosed(): 73 ns, no syscall. Release build,bench-run, turbo off.Cases covered, and what each safeguard's removal breaks
closed()checkreplaces a connection the upstream closed during a slow handshake,loses the connection, once counted, when the upstream stops during the handshakeconnects after the handshake for a hello too large to replayconnects each client only to the destination its protocol selects,with only its h2 destination refusing…gives the handshake only what a slow local connect left of the timeoutAlso covered: h2 offers in either order, h1 and no ALPN, one accept per client; all peers refuse with one
upstream_connect; hanging connects and drain fallback in both modes; a failed and an abandoned handshake close the upstream with no bytes; mTLS routes reach the upstream only after verification; other local slots are tried before peers; ingress and suspended routes unchanged. Rust tests compare the prediction with real rustls negotiation over offer order, server config and TLS 1.2 and 1.3, plus resumption and a real hello retry.Refs Forward to a peer when the local upstream connect fails on a TLS-terminating failover route.
PR QA: skipped @ 786025b — symphony is outside the trial's scope.
Docs: this repo's README. HarperFast/documentation has no symphony page.
🤖 Generated by Claude Code (Claude Opus 5.5); posted via @kriszyp.
Related PRs: #81 overlaps (this generalizes its connect-or-forward block), #86 overlaps (rebased onto its failover probe and test additions), #76 overlaps (same failover tests; its refusal classification is reused)
Complexity: complicated
Origin — the dispatch brief this PR was written from
Forward to a peer when the local connect fails on a TLS-terminating failover route
Implement #83. On a TLS-terminating route with a failover policy (client listener, not suspended), connect upstream before the TLS handshake. If that connect fails, forward the untouched ClientHello to peers with forward_to_peers in ForwardMode::Down, the way #81 does for passthrough routes in src/proxy_conn.rs. If it succeeds, use that connected upstream in proxy_via_tls after the handshake instead of connecting again. Open a draft PR.
Acceptance
Dispatch: task
symphony-83-tls-terminating-connect-forward· queued by kris-session · ran by claude/opus/xhigh · worker kzyp-xps-1Deferred findings
7f7b569091b7f0ddnit reliability —src/proxy_conn.rs— replacement connect after handshake uses full upstream_connect_timeout (30 s without protection) — NOT FILEDReview-Coverage: authored=gemini; ran=muse,codex,cursor-composer,claude,cursor-grok; late=cursor-grok:adjudicated; adjudicated=domain; blocked=claude(fallback)(out-of-budget); rounds=8; full=3 @ 786025b
Review-Attention: study ~9m (decisions: connect-before-handshake, balancer-guard-across-handshake, handshake-budget-shared, upstream-connect-metric-semantics, readiness-close-check, do-less-alternative, requirement-shift, perf-cost; raised: open major) @ 786025b