Repository navigation
Close ends every accepted connection instead of waiting for it - #19
Merged
Merged
Conversation
Server.Close cancelled the context, closed the listener and waited for the connection goroutines, but nothing ended those connections: an H2 session ran until the client hung up, so Close blocked indefinitely and the session kept proxying. lantern-box's inbound Close calls it, so rotating or turning off a volunteer's inbound left open sessions serving. Serve now records every accepted connection (and adds it to the wait group) under a lock that Close takes to mark the server closed, so a connection is either seen by Close or refused and closed on arrival. Close closes them all before waiting, which ends authenticated sessions, handshakes in progress and connections still sending a ClientHello. The masquerade path also watches the server context: the dial to the origin is abandoned and the origin connection is closed too, since a held-open origin would otherwise keep the copy loop alive. Close is idempotent, and a Serve that starts after Close returns. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01KqE58aYaai3u8CFZe5nXdE
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are reported for the server shutdown and connection-termination changes.
No open issues 🎉
What changed in this PR
This PR makes Server.Close terminate accepted connections rather than wait for clients to disconnect, ensuring shutdown and volunteer-peer rotation stop existing sessions.
Changes:
- Tracks accepted connections and coordinates registration with shutdown, including repeated
Closecalls and listener-startup races. - Cancels masquerade dialing and closes both proxy connections when the server shuts down.
- Adds shutdown tests for authenticated sessions, masquerade traffic, partial TLS headers, and racing accepts.
| File | Description |
|---|---|
server.go |
Tracks and closes accepted connections before waiting for their goroutines. |
masquerade.go |
Adds context-aware cancellation while preserving the public ProxyConnection signature. |
close_test.go |
Tests connection termination, shutdown timing, and repeated closure. |
Review effort: Balanced
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.
Server.Closenow ends every connection the server accepted, then waits for their goroutines. Until now it cancelled its context, closed the listener and waited, and each connection's goroutine lived until the client left. So Close blocked for as long as any session stayed open, and the sessions kept proxying.This matters for Lantern's volunteer peers ("direct mode"). radiance rotates a volunteer's inbound hourly and, with helper invites, as soon as the user turns off "help others too". It closes the old sing-box, gives up waiting after a timeout, and the old samizdat sessions keep running. So a volunteer who stops serving strangers still serves every stranger already connected, and rotated-out credentials keep working on open sessions.
What changed
Serverecords each accepted connection and adds it to the wait group under one lock, checking aclosedflag. A connection accepted afterClosestarted is closed at once instead of handled.Closecancels the context, marks the server closed, closes the listener (first call only), closes every recorded connection, then waits. Calling it twice is safe.Servereturns at once ifClosealready ran before it recorded the listener. Previously that case accepted forever.DialContext(s.ctx)through an unexportedproxyConnection(ctx, …). The publicProxyConnectionkeeps its signature.HandshakeContext(s.ctx), and its connection is now closed too.http2.ServeConn: closing the raw connection ends it. Streams see an ordinary disconnect, the same as aSetShortIDsrevoke.SetShortIDsand thesessionsmap are unchanged.Testing
go test -race -count=1 ./...andgo vet ./...pass, which is what CI runs.close_test.goadds four tests, each of which failed before the fix:Close, followed by a secondClose.Each asserts that
Closereturns within 1 s and that the client then sees a real error, not a timeout. The new tests and theSetShortIDstests also passed 10 runs in a row under-race.Not tested: a masquerade dial cancelled mid-dial, because a hanging dial can't be reproduced reliably on a local machine.
Risk
Closenow drops live sessions instead of letting them drain. In lantern-box's samizdat inbound, that means a radiance volunteer's hourly credential rotation drops open sessions, paired ones included, and clients reconnect within seconds. That is the behaviour radiance's rotation already assumed.Closereturns, a handler's own outbound dial or copy can take a moment to notice its stream is gone. That is unchanged.server.go.How to revert
Revert the merge commit. Nothing else depends on it until lantern-box bumps its samizdat pin.
Pre-PR review (local Codex gate)
CODEX GATE: verdict=approve critical=0 important=0 minor=0 preexisting=0 reviewed=12dd65ff6AGENTS.mdadditions: noneGitHub review
🤖 Generated with Claude Code
https://claude.ai/code/session_01KqE58aYaai3u8CFZe5nXdE