Skip to content

Don't run a queued task's escalation handlers on the thread that enqueues it - #106

Merged
dfed merged 8 commits into
mainfrom
claude/project-thread-cidgxs
Nov 20, 2026
Merged

dfed merged 8 commits into
mainfrom
claude/project-thread-cidgxs

Conversation

@dfed

@dfed dfed commented Nov 19, 2026 •

Copy link
Copy Markdown
Owner

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 calling task.escalatePriority(to:) synchronously, on the thread running Task(name:priority:on:operation:).

Escalating a task runs its onPriorityEscalated handlers 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:

  • If the enqueueing code holds a lock that the handler takes, it deadlocks.
  • If the enqueueing code runs inside a cancellation handler, we escalate a task while holding the runtime's lock on another task, which CLAUDE.md forbids.

Fix

  • register(escalation:for:) still applies a pending escalation as soon as the ticket holds the current turn, but now does it from Task.detached(priority:). This matches what waitingTicket(_:wasEscalatedTo:) already does. If the task hasn't begun yet, the escalation also gets its queued job scheduled sooner.
  • begin() reads the pending holderPriority in the lock it already takes. After releasing the lock, the task escalates itself with withUnsafeCurrentTask { $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 from register hasn'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_escalatesTaskWithoutDeadlocking reproduces the bug deterministically. Without a fix, it traps on macOS with "Trying to recursively lock an os_unfair_lock" in swift_task_escalateImpl ← Turns.register(escalation:for:).
  • register_beforeTurnBegins_afterHigherPriorityTaskBeginsWaiting_escalatesRegisteredTaskBeforeItsTurnBegins shows that registering escalates a task whose turn hasn't begun.
  • The other register_beforeTurnBegins_… tests check Task.currentPriority right after begin() returns, using a ManualTaskExecutor.
  • register_afterTurnBegins_… covers a task whose operation began before it was registered.

🤖 Generated with Claude Code

https://claude.ai/code/session_01HLuVQLo2UsTkDdhDM9Keiv

…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
@dfed dfed self-assigned this Nov 19, 2026
… new task

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HLuVQLo2UsTkDdhDM9Keiv
@codecov

codecov Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (32c1c9d) to head (521fde0).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##              main      #106    +/-   ##
==========================================
  Coverage   100.00%   100.00%            
==========================================
  Files           15        15            
  Lines         3607      3714   +107     
==========================================
+ Hits          3607      3714   +107     
Files with missing lines Coverage Δ
Sources/AsyncQueue/Utilities/Turns.swift 100.00% <100.00%> (ø)
Tests/AsyncQueueTests/TurnsTests.swift 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@dfed dfed changed the title Escalate a task from a new task when registering it Don't run a queued task's escalation handlers on the thread that enqueues it Nov 19, 2026
…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
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
@dfed
dfed merged commit c65d55b into main Nov 20, 2026
14 of 15 checks passed
@dfed
dfed deleted the claude/project-thread-cidgxs branch November 20, 2026 00:55
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.

2 participants