fix(coding-agent): cancel the final kernel snapshot before disposal cleanup - #1708
Closed
snimu wants to merge 13 commits into
Closed
fix(coding-agent): cancel the final kernel snapshot before disposal cleanup#1708snimu wants to merge 13 commits into
snimu wants to merge 13 commits into
Conversation
…ot-dispose-timeout
…ot-dispose-timeout
…ot-dispose-timeout # Conflicts: # packages/coding-agent/src/core/kernel/index.ts # packages/coding-agent/test/kernel-abort.test.ts
…ot-dispose-timeout
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 35d4433. Configure here.
| ]); | ||
| if (timeout) globalThis.clearTimeout(timeout); | ||
| if (!queueSettled) return; | ||
| await this.captureSnapshot({ executionTimeoutMs: SNAPSHOT_EXECUTION_TIMEOUT_MS }); |
There was a problem hiding this comment.
Dispose snapshot wait can hang
Medium Severity
flushSnapshotForDispose only bounds the wait for the queue tail captured at entry, then awaits captureSnapshot with no outer deadline. A second execute (or other enqueue) that joins during that wait becomes prev for the snapshot and has no timeout, so dispose/shutdown can block indefinitely and delay /reload and signal teardown.
Reviewed by Cursor Bugbot for commit 35d4433. Configure here.
This was referenced Aug 27, 2026
Contributor
Author
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.


What was wrong
When a kernel was being disposed, its final state snapshot was raced against a 5-second guard that couldn't cancel anything: if the snapshot was slow, the guard "won", disposal proceeded to close the ZMQ sockets, and the still-running snapshot then failed against dead sockets.
The fix
The final snapshot now runs through the kernel's existing cancellable execution path (interrupt, then force-abort after a grace period). Disposal waits for the snapshot to settle — completed or cleanly aborted — before any socket cleanup. The separate dispose-timeout constant and the uncancellable race are deleted; there is one timeout policy for snapshot execution, applied in one place. Both teardown paths (dispose and shutdown-with-snapshot) share the fix.
Net −13 lines.
How it's verified
The existing timeout test was updated to exercise the real flow: dispose() directly, the timeout starting only after queued work, and cleanup deferred until the aborted snapshot settles. tsgo/biome/suite green, re-validated after each stack merge. Two-model implement/review loop, approved first pass.
Stacked on #1705 (test the whole stack at the leaf; merge base-first).
Note: intentionally no Linear ticket for this cleanup stack, so that check stays red.
Note
Medium Risk
Changes kernel teardown ordering and snapshot behavior on dispose/shutdown; mistakes could hang disposal or skip needed state persistence, but scope is bounded to ReplKernelManager graceful cleanup.
Overview
Fixes a race where graceful kernel disposal could close ZMQ sockets while a final namespace snapshot was still running, leaving that work failing against dead sockets.
flushSnapshotForDisposeno longer races an uncancellablesnapshotState()against a separate dispose guard. On dispose/shutdown-with-snapshot it interrupts any active execution, waits for theexecutionQueueto settle withinSNAPSHOT_EXECUTION_TIMEOUT_MS, then runscaptureSnapshotthrough the normal cancellable execution path (or skips the final snapshot if the queue never settles).SNAPSHOT_DISPOSE_TIMEOUT_MSis removed so snapshot timing uses one policy in one place.Tests in
repl-kernel-abort.test.tscover dispose waiting until a timed-out snapshot aborts beforecleanupResources, and tearing down when a hung execution blocks the queue without starting a snapshot.Reviewed by Cursor Bugbot for commit 35d4433. Bugbot is set up for automated code reviews on this repo. Configure here.
Note
Cancel hung final kernel snapshot before disposal cleanup in
ReplKernelManagerRewrites
ReplKernelManager.flushSnapshotForDisposeto interrupt any active execution and wait for theexecutionQueueto settle withinSNAPSHOT_EXECUTION_TIMEOUT_MSbefore taking a final snapshot. If the queue does not settle in time, the snapshot is skipped so teardown proceeds without hanging.SNAPSHOT_DISPOSE_TIMEOUT_MSconstant from shared.tsdispose(), if an active execution exists it is interrupted immediately, and the final snapshot is only captured when the queue settles within the timeout; previously the snapshot was raced against a separate dispose guard timeoutMacroscope summarized 35d4433.
Linear: ENG-5650