Skip to content

Fix cancellation submission race#389

Open
SpiraMira wants to merge 2 commits into
ml-explore:mainfrom
SpiraMira:fix/cancellation-submission-race-3x
Open

Fix cancellation submission race#389
SpiraMira wants to merge 2 commits into
ml-explore:mainfrom
SpiraMira:fix/cancellation-submission-race-3x

Conversation

@SpiraMira

Copy link
Copy Markdown

Proposed changes

Fixes #382

Checklist

Put an x in the boxes that apply.

  • I have read the CONTRIBUTING document
  • I have run pre-commit run --all-files to format my code / installed pre-commit prior to committing changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the necessary documentation (if needed)

SpiraMira and others added 2 commits July 3, 2026 15:06
…submission

TokenIterator.next() calls asyncEval() to pipeline the next GPU evaluation. The
3.x loop `tokenLoop: while let token = iterator.next()` checked Task.isCancelled
only AFTER next() had already submitted work, allowing one extra asyncEval after
cancellation was observed.

Reorder to `while !Task.isCancelled { guard let token = iterator.next() else { break } }`
so the check happens before next(). The existing post-loop block already assigns
stopReason = .cancelled on a cancelled exit, and Stream().synchronize() still settles
any in-flight evaluation at the end of the task body, so the settlement boundary is
unchanged.

3.x re-authoring of the 2.x fix (0737a6e), adapted to the refactored tokenLoop shape.

Fixes the iOS/iPadOS lifecycle crash when the app backgrounds mid-generation:
  [METAL] Command buffer execution failed: Insufficient Permission
  (kIOGPUCommandBufferCallbackErrorBackgroundExecutionNotPermitted)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Ports the 2.x settlement tests (b03a2b7) to 3.x: stream-cancellation settle,
immediate-cancel settle, .cancelled stopReason, and .length at maxTokens (the
guard-nil natural-termination path). Compile-verified against 3.31.4.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Cancelling generation can still submit one more GPU evaluation (iOS/iPadOS crash)

1 participant