Fix vite bridge conn swap race, blocking send, and stdin goroutine leak#5489
Open
simonfaltum wants to merge 3 commits into
Open
Fix vite bridge conn swap race, blocking send, and stdin goroutine leak#5489simonfaltum wants to merge 3 commits into
simonfaltum wants to merge 3 commits into
Conversation
The dev tunnel bridge had three concurrency issues in one file: reconnects swapped tunnelConn without synchronization (and leaked the old connection), the connection:request send could block the tunnel reader and hang shutdown, and each approval prompt spawned a stdin reader goroutine that leaked on timeout and swallowed the next answer. Use atomic.Pointer for tunnelConn (closing the replaced connection), guard the connection request send with stopChan and a timeout, and read stdin with a single persistent goroutine feeding a channel. Existing tests synchronized on time.Sleep with a shared variable, which the race detector flags; they now synchronize on a channel so the package is race-clean. Co-authored-by: Isaac
Contributor
Waiting for approvalBased on git history, these people are best suited to review:
Eligible reviewers: Suggestions based on git history. See OWNERS for ownership rules. |
Collaborator
Integration test reportCommit: d8c3332
31 interesting tests: 15 SKIP, 9 flaky, 7 KNOWN
Top 50 slowest tests (at least 2 minutes):
|
Co-authored-by: Isaac
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.
Why
Found during a full-repo review of the CLI. The vite dev tunnel bridge (
libs/apps/vite/bridge.go) had three concurrency bugs:connection:requestmessages were forwarded with an unguarded channel send. The consumer can sit on a stdin approval prompt for up to 60 seconds (and exits entirely on shutdown), so a full queue could stall the single tunnel reader and hang Ctrl+C shutdown.Changes
Before, a tunnel reconnect raced with in-flight writes, leaked the old connection, and the approval prompt could hang shutdown or eat the user's answers; now connection swaps are atomic and clean, request forwarding can't block the tunnel reader, and one persistent stdin reader serves all prompts.
tunnelConnis now anatomic.Pointer[websocket.Conn](the same pattern the ssh tunnel proxy uses). The newsetTunnelConnswaps the pointer and closes the connection it replaced.connection:requestforward is wrapped in aselectwithstopChanand a timeout, like every other channel send in the file.stdinLineschannel that prompts consume. It is started inStart()only when--auto-approveis not set, and closes the channel on read error so prompts fail fast instead of waiting on input that will never come.time.Sleepwith a shared variable, which the race detector flags. They now synchronize on a channel, sogo test -race ./libs/apps/vitepasses cleanly and the new race-detector test is meaningful.Test plan
TestBridgeSetTunnelConnSwapDuringWrites)connection:requestarriving after stop with a full queue returns instead of blocking (TestBridgeConnectionRequestSendDoesNotBlockAfterStop)TestBridgeConnectionRequestSequentialPrompts)go test -race -count=2 ./libs/apps/vite/passes./task fmt-q,./task lint-q,./task checkspassThis pull request and its description were written by Isaac.