Skip to content

Fix cancellation submission race#389

Merged
davidkoski merged 2 commits into
ml-explore:mainfrom
SpiraMira:fix/cancellation-submission-race-3x
Jul 14, 2026
Merged

Fix cancellation submission race#389
davidkoski merged 2 commits into
ml-explore:mainfrom
SpiraMira:fix/cancellation-submission-race-3x

Conversation

@SpiraMira

Copy link
Copy Markdown
Contributor

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 14, 2026 14:49
…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>
@davidkoski
davidkoski force-pushed the fix/cancellation-submission-race-3x branch from 2f93db5 to ee2c3c2 Compare July 14, 2026 21:50
@davidkoski

Copy link
Copy Markdown
Collaborator

Rebase on current main.

@davidkoski davidkoski left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change looks good, thank you!

@davidkoski
davidkoski merged commit 10e0cb7 into ml-explore:main Jul 14, 2026
2 checks passed
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)

2 participants