fix(drift): close active connections on shutdown to prevent Ctrl+C hang#219
Merged
sohil-kshirsagar merged 5 commits intomainfrom Apr 11, 2026
Merged
Conversation
…rl+C hang When pressing Ctrl+C during `tusk drift run`, the process would hang indefinitely because server.Stop() waited for handleConnection goroutines that were blocked on io.ReadFull. Closing the listener only prevented new connections — existing ones were never closed. Also adds force-exit on second Ctrl+C as a safety net.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 6418f90. Configure here.
Closes the race window where Stop() could iterate activeConns before handleConnection had a chance to register the conn, leaving it unclosed.
jy-tan
approved these changes
Apr 8, 2026
In the TUI, Bubble Tea's raw terminal mode converts Ctrl+C into a key event rather than an OS signal. Since cleanup runs synchronously in the update loop, a second Ctrl+C can't be delivered if cleanup hangs. Spawn a goroutine that force-exits after 5s as a safety net.
Contributor
There was a problem hiding this comment.
1 issue found across 3 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="internal/runner/server.go">
<violation number="1" location="internal/runner/server.go:636">
P1: Shutdown still has a race: a connection accepted right around cancellation can be added after `Stop()` closes active connections, leaving a handler blocked on `io.ReadFull` and potentially hanging `wg.Wait()`.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review, or fix all with cubic.
Comment on lines
+636
to
+638
| ms.activeConns[conn] = struct{}{} | ||
| ms.activeConnsMu.Unlock() | ||
|
|
Contributor
There was a problem hiding this comment.
P1: Shutdown still has a race: a connection accepted right around cancellation can be added after Stop() closes active connections, leaving a handler blocked on io.ReadFull and potentially hanging wg.Wait().
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At internal/runner/server.go, line 636:
<comment>Shutdown still has a race: a connection accepted right around cancellation can be added after `Stop()` closes active connections, leaving a handler blocked on `io.ReadFull` and potentially hanging `wg.Wait()`.</comment>
<file context>
@@ -622,6 +632,10 @@ func (ms *Server) acceptConnections() {
}
+ ms.activeConnsMu.Lock()
+ ms.activeConns[conn] = struct{}{}
+ ms.activeConnsMu.Unlock()
+
</file context>
Suggested change
| ms.activeConns[conn] = struct{}{} | |
| ms.activeConnsMu.Unlock() | |
| if ms.ctx.Err() != nil { | |
| _ = conn.Close() | |
| return | |
| } | |
| ms.activeConnsMu.Lock() | |
| ms.activeConns[conn] = struct{}{} | |
| ms.activeConnsMu.Unlock() |
The connection-closing fix in server.Stop() makes cleanup return promptly, so a timeout safety net is unnecessary. If cleanup ever hangs again, the right fix is to address the root cause.
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.

tusk drift run --printhangs on Ctrl+C when the mock server has an active SDK connection.Root cause
server.Stop()callsms.wg.Wait(), which blocks until allhandleConnectiongoroutines finish. Those goroutines are stuck onio.ReadFull(conn, ...)— a blocking read with no deadline. Closing the listener prevents new connections but doesn't close existing ones, soio.ReadFullnever returns and the process hangs indefinitely.Changes
Stop()beforewg.Wait(), so blocked reads return immediately with an errorTestServerStopClosesActiveConnections— verified it fails without the fix (hangs for 2s then times out) and passes with it