Repository navigation
Don't run a queued task's escalation handlers on the thread that enqueues it - #106
Merged
Merged
Conversation
…r the caller's lock Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HLuVQLo2UsTkDdhDM9Keiv
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HLuVQLo2UsTkDdhDM9Keiv
… new task Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HLuVQLo2UsTkDdhDM9Keiv
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #106 +/- ##
==========================================
Coverage 100.00% 100.00%
==========================================
Files 15 15
Lines 3607 3714 +107
==========================================
+ Hits 3607 3714 +107
🚀 New features to boost your workflow:
|
…nly once its operation may run Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HLuVQLo2UsTkDdhDM9Keiv
…escalating from a new task Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HLuVQLo2UsTkDdhDM9Keiv
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HLuVQLo2UsTkDdhDM9Keiv
…iority Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HLuVQLo2UsTkDdhDM9Keiv
The task still escalates itself as its turn begins, in case that new task has not run yet. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HLuVQLo2UsTkDdhDM9Keiv
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.
Requested by Dan · project thread
Problem
A task enqueued on a queue can start running before the queue registers it. If a higher-priority task starts waiting in that window,
escalateHolder(to:)records the priority but can't escalate the unregistered task.Turn.register(_:)then applied that escalation by callingtask.escalatePriority(to:)synchronously, on the thread runningTask(name:priority:on:operation:).Escalating a task runs its
onPriorityEscalatedhandlers on the escalating thread, under the task's status lock. So the enqueueing code could end up running the operation's own escalation handlers. That causes two problems:CLAUDE.mdforbids.Fix
register(escalation:for:)still applies a pending escalation as soon as the ticket holds the current turn, but now does it fromTask.detached(priority:). This matches whatwaitingTicket(_:wasEscalatedTo:)already does. If the task hasn't begun yet, the escalation also gets its queued job scheduled sooner.begin()reads the pendingholderPriorityin the lock it already takes. After releasing the lock, the task escalates itself withwithUnsafeCurrentTask { $0?.escalatePriority(to:) }, before any of its own escalation handlers are installed. This makes sure the task takes its turn at the escalated priority, even if the new task fromregisterhasn't run yet.No synchronous party can apply the escalation in
register: the enqueueing thread is the bug, and the task may already be running the caller's operation. The extra hop only affects how early the boost lands. The task is always at the escalated priority by the time its turn begins.Tests
register_whileRegisteringCodeHoldsLockThatTaskEscalationHandlerTakes_escalatesTaskWithoutDeadlockingreproduces the bug deterministically. Without a fix, it traps on macOS with "Trying to recursively lock an os_unfair_lock" inswift_task_escalateImpl←Turns.register(escalation:for:).register_beforeTurnBegins_afterHigherPriorityTaskBeginsWaiting_escalatesRegisteredTaskBeforeItsTurnBeginsshows that registering escalates a task whose turn hasn't begun.register_beforeTurnBegins_…tests checkTask.currentPriorityright afterbegin()returns, using aManualTaskExecutor.register_afterTurnBegins_…covers a task whose operation began before it was registered.🤖 Generated with Claude Code
https://claude.ai/code/session_01HLuVQLo2UsTkDdhDM9Keiv