Skip to content

Replace DispatchQueue-backed locks with Mutex - #89

Merged
dfed merged 1 commit into
claude/2.0-3-raise-minimumsfrom
claude/2.0-4-mutex
Nov 17, 2026
Merged

dfed merged 1 commit into
claude/2.0-3-raise-minimumsfrom
claude/2.0-4-mutex

Conversation

@dfed

@dfed dfed commented Nov 14, 2026 •

Copy link
Copy Markdown
Owner

Summary

Now that the platform minimums include the Synchronization module, Delivery and CancellableQueue guard their state with Mutex instead of hand-rolled locks built on serial dispatch queues.

CancellableQueue also stops deferring its bookkeeping with DispatchQueue.async. Reservations, registrations, and removals now apply synchronously, in program order. That is safe because nothing is cancelled while the lock is held (#86 and #87).

Tests

No test is added. Because registration is now synchronous:

  • the three cancelTasks_*_doesNotCancelFutureTasks tests drop the second cancelTasks() call that Cancel tasks outside of CancellableQueue's lock #87 used to flush pending registrations, and check the task right after it ends;
  • the comments in the four cancelTasks_*_cancelsCurrentlyExecutingThrowingTask* tests now say that the task registers its cancellation before Task(on:) returns.

Series

PR 5 of 10. It is stacked on #88.

Validation

Each commit in the series was validated on its own, on Linux with Swift 6.4.2, from a git archive copy:

  • It builds with -warnings-as-errors, and swiftformat --lint is clean.
  • The full suite passed 3 runs unpinned and 1 with the test runner pinned to one CPU.
  • The Linux job's coverage steps, run from the commit's own ci.yml under bash -e, pass, including the atomic-counter check. No line in Sources or Tests reads zero in the CI run.
  • This round changed no test here. Earlier rounds checked the race-guarding tests against mutants of the code they guard: each fails 10 of 10 runs against its mutant and passes on the real code.
  • At the top of the series, 3 rounds of 10 concurrent full suites pinned to one CPU pass, and ThreadSanitizer reports no races in 3 runs. TSan on Linux flags Synchronization.Mutex even in trivially correct code, so the TSan runs replace Mutex with a pthread-based lock that TSan understands.

Nothing was run on Apple platforms. CI is the first run there.

Review checklist

Left for code review because no test can force them:

  • Delivery and CancellableQueue: one critical section each. With Mutex, Delivery.cancel() must set isCancelled and read the task in one withLock, and execute must store the task and read isCancelled in one. In CancellableQueue, cancelTasks() must collect its closures, and each reservation, registration and removal must update the map, in one withLock each. No test can force a thread switch inside one.
  • The timeout is the failure signal for re-entry. Mutex is not reentrant, so cancelling under CancellableQueue's lock again would now deadlock: Cancel tasks outside of CancellableQueue's lock #87's canBeCalledFromCancellationHandler tests would hang until the job's timeout.
  • Darwin. Nothing was run on Apple platforms. CI is the first run there.

🤖 Generated with Claude Code

https://claude.ai/code/session_01HLuVQLsTNvRnyh155N25UM

@codecov

codecov Bot commented Nov 14, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (9a40bac) to head (0958fc0).

Additional details and impacted files

Impacted file tree graph

@@                      Coverage Diff                      @@
##           claude/2.0-3-raise-minimums       #89   +/-   ##
=============================================================
  Coverage                       100.00%   100.00%           
=============================================================
  Files                               12        12           
  Lines                             1509      1476   -33     
=============================================================
- Hits                              1509      1476   -33     
Files with missing lines Coverage Δ
Sources/AsyncQueue/CancellableQueue.swift 100.00% <100.00%> (ø)
Sources/AsyncQueue/Utilities/Delivery.swift 100.00% <100.00%> (ø)
Tests/AsyncQueueTests/CancellableQueueTests.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 commented Nov 16, 2026 •

Copy link
Copy Markdown
Owner Author

Correction: the CodeQL Swift failure I reported here is fixed by 9a40bac on #88, which builds without SwiftPM's sandbox under CodeQL, and this PR now sits on top of it. I was wrong that it needed a decision. See the corrected comment on #88.


Generated by Claude Code

Now that the platform minimums include the Synchronization module, use
`Mutex` for the state that `Delivery` and `CancellableQueue` guard
instead of hand-rolled locks built on serial dispatch queues.

`CancellableQueue` also stops deferring its bookkeeping with
`DispatchQueue.async`: reservations, registrations, and removals now
apply synchronously, in program order. This is safe because no task is
cancelled while the lock is held.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HLuVQLsTNvRnyh155N25UM
@dfed
dfed force-pushed the claude/2.0-4-mutex branch from ee9ebb7 to 0958fc0 Compare November 16, 2026 23:40
@dfed
dfed merged commit c3494ed into main Nov 17, 2026
12 checks passed
@dfed
dfed deleted the claude/2.0-4-mutex branch November 17, 2026 02:27
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