-
Notifications
You must be signed in to change notification settings - Fork 0
Fix session stuck in Stopping state when interrupt hangs #334
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
brendanlong
wants to merge
2
commits into
main
Choose a base branch
from
claude/39ef0903-ec28-4acf-9882-70febe888bbd
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+66
−3
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -603,8 +603,21 @@ export function getPendingInput( | |||||||||
| }; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * Race a promise against a timeout. Resolves with `undefined` if the timeout fires first. | ||||||||||
| */ | ||||||||||
| function withTimeout<T>(promise: Promise<T>, ms: number): Promise<T | undefined> { | ||||||||||
| return Promise.race([ | ||||||||||
| promise, | ||||||||||
| new Promise<undefined>((resolve) => setTimeout(() => resolve(undefined), ms)), | ||||||||||
| ]); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| const INTERRUPT_TIMEOUT_MS = 5_000; | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * Interrupt a running Claude query. | ||||||||||
| * Uses a timeout to prevent hanging if the SDK's interrupt() never resolves. | ||||||||||
| */ | ||||||||||
| export async function interruptClaude(sessionId: string): Promise<boolean> { | ||||||||||
| const state = sessions.get(sessionId); | ||||||||||
|
|
@@ -614,10 +627,18 @@ export async function interruptClaude(sessionId: string): Promise<boolean> { | |||||||||
| } | ||||||||||
|
|
||||||||||
| try { | ||||||||||
| await state.currentQuery.interrupt(); | ||||||||||
| const result = await withTimeout(state.currentQuery.interrupt(), INTERRUPT_TIMEOUT_MS); | ||||||||||
| if (result === undefined) { | ||||||||||
|
Comment on lines
+630
to
+631
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update this check to align with the improved
Suggested change
|
||||||||||
| log.warn('interruptClaude: Timed out, force-cleaning state', { sessionId }); | ||||||||||
| forceCleanSession(sessionId); | ||||||||||
| } | ||||||||||
| return true; | ||||||||||
| } catch (err) { | ||||||||||
| log.warn('interruptClaude: Failed', { sessionId, error: toError(err).message }); | ||||||||||
| log.warn('interruptClaude: Failed, force-cleaning state', { | ||||||||||
| sessionId, | ||||||||||
| error: toError(err).message, | ||||||||||
| }); | ||||||||||
| forceCleanSession(sessionId); | ||||||||||
| return false; | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
@@ -709,6 +730,37 @@ export async function markLastMessageAsInterrupted(sessionId: string): Promise<v | |||||||||
| }); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * Force-clean a session's in-memory state and kill the subprocess. | ||||||||||
| * Used as a fallback when interrupt() hangs or fails. | ||||||||||
| * | ||||||||||
| * Calls close() on the query to trigger the SDK's subprocess kill | ||||||||||
| * (SIGTERM immediately, SIGKILL after 5s), then cleans up our state. | ||||||||||
| */ | ||||||||||
| function forceCleanSession(sessionId: string): void { | ||||||||||
| const state = sessions.get(sessionId); | ||||||||||
| if (!state) return; | ||||||||||
|
|
||||||||||
| // close() is synchronous (fire-and-forget) and kills the subprocess | ||||||||||
| if (state.currentQuery) { | ||||||||||
| try { | ||||||||||
| state.currentQuery.close(); | ||||||||||
| } catch { | ||||||||||
| // Ignore close errors | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| if (state.pendingInput) { | ||||||||||
| state.pendingInput.reject(new Error('Session force-cleaned')); | ||||||||||
| state.pendingInput = null; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| state.isRunning = false; | ||||||||||
| state.currentQuery = null; | ||||||||||
| sessions.delete(sessionId); | ||||||||||
| sseEvents.emitClaudeRunning(sessionId, false); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * Stop a session's Claude query and clean up state. | ||||||||||
| * Called when a session is stopped. | ||||||||||
|
|
||||||||||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current implementation of
withTimeouthas two issues:undefinedboth when the timeout occurs and when the input promise resolves toundefined. Sincestate.currentQuery.interrupt()returnsPromise<void>, it always resolves toundefinedon success, making the timeout check at line 631 always true.setTimeouttimer is not cleared if the promise resolves before the timeout, which can lead to an accumulation of active timers in a long-running process.I suggest using a wrapper object to distinguish between success and timeout, and ensuring the timer is cleared.