From db3771cd3bba85fb878f8353919b5cc1f6a05628 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 16 Nov 2026 03:34:33 +0000 Subject: [PATCH 1/5] Run Thread Sanitizer on macOS No CI job ran Thread Sanitizer. On Linux it flags `Synchronization.Mutex` even in correct code, so a Linux job would fail on every run. A new job on the xcode-27 runner builds the tests with Thread Sanitizer and runs them on macOS instead. When Thread Sanitizer reports a data race, the test run exits with an error and the job fails. In three trial runs on GitHub's runners the job took about a minute and a half and reported nothing. Like the other jobs that run the tests outside a simulator, it stops after 15 minutes so that a deadlocked test can't hold the runner for GitHub's six-hour default. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01HLuVQLsTNvRnyh155N25UM --- .github/workflows/ci.yml | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 774b41c..74dd045 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -62,6 +62,25 @@ jobs: run: sudo xcode-select --switch /Applications/Xcode_27.2.app/Contents/Developer - name: Build and Test Framework run: xcrun swift test -c release -Xswiftc -enable-testing + tsan: + name: "Thread Sanitizer on macOS" + runs-on: xcode-27 + # A test that deadlocks keeps the test run from ending, so stop the + # job after 15 minutes rather than at GitHub's six-hour default. + timeout-minutes: 15 + permissions: + contents: read + steps: + - name: Checkout Repo + uses: actions/checkout@v7 + - name: Select Xcode Version + run: sudo xcode-select --switch /Applications/Xcode_27.2.app/Contents/Developer + - name: Build With Thread Sanitizer + run: xcrun swift build --build-tests --sanitize=thread + # When Thread Sanitizer reports a data race, the test process exits + # with an error, which fails this step. + - name: Test With Thread Sanitizer + run: xcrun swift test --skip-build --sanitize=thread linux: name: "Build and Test on Linux" runs-on: ubuntu-24.04 From 62899c61697bb6cb9dbb1a2b8b7f103e2be90de8 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 16 Nov 2026 03:37:41 +0000 Subject: [PATCH 2/5] Test on the iOS 18 simulator The package supports iOS 18, but CI tested only on the iOS 27.2 simulator. Before iOS 26, a queue's tasks wait for their turns without escalating priority. Tests reached that path in `Turns` only through `Turns(escalatesPriority: false)`, and no queue's tests ran on it. Xcode 27 can't download the iOS 18 simulator runtime: `xcodebuild -downloadPlatform iOS -buildVersion 22F77` fails with "iOS 22F77 is not available for download". The `macos-15` runner has iOS 18.5 installed as a disk image, so a new job there copies the image and uploads it as an artifact. Exporting the runtime with `xcodebuild -downloadPlatform -exportPath` instead fails at random with "Unable to connect to simulator." A second job, on the `xcode-27` runner, downloads the image and installs it. Adding the 9 GB image to the runner's simulator runtimes fails with "Cannot copy the image because the disk is almost full", so it first deletes every runtime but the 27.2 runtime of each platform. It then creates an iPad (10th generation) on iOS 18.5, and runs and uploads coverage for the tests there with Xcode 27.2, like the other simulator jobs. Tests that need iOS 26 are skipped. The two scripts in Scripts/ explain each step. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01HLuVQLsTNvRnyh155N25UM --- .github/workflows/ci.yml | 57 ++++++++++++++++++++ Scripts/build.swift | 9 +++- Scripts/copy-ios-18-simulator.sh | 40 +++++++++++++++ Scripts/install-ios-18-simulator.sh | 80 +++++++++++++++++++++++++++++ 4 files changed, 185 insertions(+), 1 deletion(-) create mode 100755 Scripts/copy-ios-18-simulator.sh create mode 100755 Scripts/install-ios-18-simulator.sh diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 74dd045..cdc1e7f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -47,6 +47,63 @@ jobs: with: fail_ci_if_error: true verbose: true + # Xcode 27 can't download the iOS 18 simulator runtime, but the + # `macos-15` image has it installed, so this job copies its disk image + # and passes it to `spm-minimum-ios` as an artifact. + minimum-ios-runtime: + name: Copy Minimum iOS Simulator + runs-on: macos-15 + # Copying and uploading the 9 GB disk image takes a few minutes, so + # stop a stalled upload after 15 minutes rather than at GitHub's + # six-hour default. + timeout-minutes: 15 + permissions: + contents: read + steps: + - name: Checkout Repo + uses: actions/checkout@v7 + - name: Copy the Simulator Runtime + run: Scripts/copy-ios-18-simulator.sh runtime + - name: Upload the Simulator Runtime + uses: actions/upload-artifact@v7 + with: + name: ios-18-simulator-runtime + path: runtime + # Skip compressing the 9 GB disk image, which only slows the upload. + compression-level: 0 + # Only `spm-minimum-ios` needs the image, minutes later. + retention-days: 1 + spm-minimum-ios: + name: Build Xcode Minimum iOS + needs: minimum-ios-runtime + runs-on: xcode-27 + # A test that deadlocks keeps the test run from ending, so stop the + # job after 50 minutes rather than at GitHub's six-hour default. + timeout-minutes: 50 + permissions: + contents: read + steps: + - name: Checkout Repo + uses: actions/checkout@v7 + - name: Select Xcode Version + run: sudo xcode-select --switch /Applications/Xcode_27.2.app/Contents/Developer + - name: Download the Simulator Runtime + uses: actions/download-artifact@v8 + with: + name: ios-18-simulator-runtime + path: runtime + - name: Install the Simulator Runtime + run: Scripts/install-ios-18-simulator.sh runtime/iphonesimulator_18.5_22F77.dmg + - name: Build and Test Framework + run: Scripts/build.swift iOS18 + - name: Prepare Coverage Reports + run: ./Scripts/prepare-coverage-reports.sh + - name: Upload Coverage Reports + if: success() + uses: codecov/codecov-action@v7 + with: + fail_ci_if_error: true + verbose: true spm-swift: name: Swift Build Xcode runs-on: xcode-27 diff --git a/Scripts/build.swift b/Scripts/build.swift index d5a0447..443dc32 100755 --- a/Scripts/build.swift +++ b/Scripts/build.swift @@ -22,6 +22,9 @@ enum TaskError: Error { enum Platform: String, CaseIterable, CustomStringConvertible { case iOS + /// The minimum iOS version. Scripts/install-ios-18-simulator.sh + /// installs its simulator runtime, which Xcode 27 can't download. + case iOS18 case tvOS case macOS case macCatalyst @@ -33,6 +36,9 @@ enum Platform: String, CaseIterable, CustomStringConvertible { case .iOS: "platform=iOS Simulator,OS=27.2,name=iPad (A16)" + case .iOS18: + "platform=iOS Simulator,OS=18.5,name=iPad (10th generation)" + case .tvOS: "platform=tvOS Simulator,OS=27.2,name=Apple TV 4K (4th generation)" @@ -50,7 +56,8 @@ enum Platform: String, CaseIterable, CustomStringConvertible { var sdk: String { switch self { - case .iOS: + case .iOS, + .iOS18: "iphonesimulator" case .tvOS: diff --git a/Scripts/copy-ios-18-simulator.sh b/Scripts/copy-ios-18-simulator.sh new file mode 100755 index 0000000..890c3f9 --- /dev/null +++ b/Scripts/copy-ios-18-simulator.sh @@ -0,0 +1,40 @@ +#!/bin/zsh +# Copies the disk image of the iOS 18.5 simulator runtime from the +# `macos-15` runner, which has it installed, into the given directory, so +# that a job on the `xcode-27` runner can install it with +# Scripts/install-ios-18-simulator.sh. +# +# Xcode 27 can't download the runtime itself: +# `xcodebuild -downloadPlatform iOS -buildVersion 22F77` fails with +# "iOS 22F77 is not available for download". Xcode 26 on `macos-15` can +# export the installed runtime with +# `xcodebuild -downloadPlatform iOS -buildVersion 22F77 -exportPath`, but +# that fails at random with "Unable to connect to simulator.", so this +# script copies the disk image that is already on the runner instead. +# +# Usage: copy-ios-18-simulator.sh directory + +set -euo pipefail + +# The build of iOS 18.5. Scripts/build.swift tests on iOS 18.5. +build=22F77 +directory=$1 + +runtimes=$(xcrun simctl runtime list -j) +# simctl lists each runtime's disk image under "path". +image=$(print -r -- "$runtimes" | python3 -c ' +import json, sys +build = sys.argv[1] +runtimes = json.load(sys.stdin).values() +print(next((runtime["path"] for runtime in runtimes if runtime.get("build") == build and runtime.get("path")), "")) +' $build) + +if [[ -z $image ]]; then + print -r -- "$runtimes" + echo "::error::The runner has no disk image of the iOS $build simulator runtime." + exit 1 +fi + +mkdir -p $directory +cp $image $directory/iphonesimulator_18.5_$build.dmg +ls -la $directory diff --git a/Scripts/install-ios-18-simulator.sh b/Scripts/install-ios-18-simulator.sh new file mode 100755 index 0000000..7a81073 --- /dev/null +++ b/Scripts/install-ios-18-simulator.sh @@ -0,0 +1,80 @@ +#!/bin/zsh +# Installs the iOS 18.5 simulator runtime from the disk image that +# Scripts/copy-ios-18-simulator.sh copied, and creates the iPad that +# Scripts/build.swift's iOS18 platform tests on. Run it on the `xcode-27` +# runner with Xcode 27.2 selected. +# +# Usage: install-ios-18-simulator.sh image + +set -euo pipefail + +image=$1 +if [[ ! -f $image ]]; then + echo "::error::The iOS 18.5 simulator image $image doesn't exist." + exit 1 +fi +uuid_pattern='[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}' + +echo "::group::Free disk space" +# The runner's simulator runtimes take up so much of its disk that +# adding the 9 GB iOS 18.5 image fails with "Cannot copy the image +# because the disk is almost full". Delete every runtime except the 27.2 +# runtime of each platform, and also the second iOS 27.2 runtime, build +# 24B91016s, which is for iPhone only. A runtime that can't be deleted +# only frees less space; adding the image below fails if it isn't +# enough. +df -h / +runtimes=$(xcrun simctl runtime list) +print -r -- "$runtimes" +unused_runtimes=(${(f)"$( + { + grep -v ' 27\.2 (' <<<"$runtimes" + grep '24B91016s' <<<"$runtimes" + } | grep -oE $uuid_pattern || true +)"}) +for runtime in $unused_runtimes; do + sudo xcrun simctl runtime delete $runtime || true +done +# `simctl runtime delete` returns while the runtime is still being +# deleted, and its space is free only once it is gone. +for attempt in {1..60}; do + if [[ $(xcrun simctl runtime list) != *Deleting* ]]; then + break + fi + sleep 5 +done +if [[ $(xcrun simctl runtime list) == *Deleting* ]]; then + echo "::warning::Some simulator runtimes are still being deleted after 5 minutes, so adding the iOS 18.5 image may run out of disk space." +fi +xcrun simctl runtime list +df -h / +echo "::endgroup::" + +echo "::group::Add the iOS 18.5 runtime" +sudo xcodebuild -runFirstLaunch +# If `xcodebuild -importPlatform` doesn't add the runtime, add it with +# simctl instead. +sudo xcodebuild -importPlatform $image || true +if [[ $(xcrun simctl list runtimes) != *'iOS 18.'* ]]; then + if ! sudo xcrun simctl runtime add $image; then + echo "::endgroup::" + echo "::error::Neither xcodebuild -importPlatform nor simctl runtime add could add $image." + exit 1 + fi +fi +# Build the runtime's dyld shared cache now rather than during the +# simulator's first boot. +sudo xcrun simctl runtime dyld_shared_cache update --all || true +xcrun simctl list runtimes +echo "::endgroup::" + +runtime=$(xcrun simctl list runtimes | grep -oE 'com\.apple\.CoreSimulator\.SimRuntime\.iOS-18-[0-9-]+' | tail -1 || true) +if [[ -z $runtime ]]; then + echo "::error::Xcode does not list an iOS 18 simulator runtime after adding $image." + exit 1 +fi +# Scripts/build.swift finds the iPad by its name and iOS version. +if [[ $(xcrun simctl list devices 'iOS 18.5') != *'iPad (10th generation) ('* ]]; then + xcrun simctl create 'iPad (10th generation)' com.apple.CoreSimulator.SimDeviceType.iPad-10th-generation $runtime +fi +xcrun simctl list devices 'iOS 18.5' From dede17274ce9a4f4d0b7473d150fcb9c1e9110da Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 16 Nov 2026 03:38:25 +0000 Subject: [PATCH 3/5] Run each test once and require deterministic tests The simulator jobs ran the suite up to 100 times and stopped at the first failure, so that a test which fails only on some interleavings had more chances to fail. Every test should instead give the same result on every run: pass every run on correct code, and fail every run, or hang until the job's timeout, on the regression it guards. The simulator jobs now run the suite once, which also makes them shorter. A new CLAUDE.md records that rule for people and agents working on the package: force interleavings with semaphores, manual executors, and test hooks, never with sleeps, wall-clock timeouts, polling, or repetition. It names the few older loop tests that stay as backstops until a deterministic test fails 10 of 10 runs for every bug each catches. It also records the package's other rules: 100% code coverage; never cancel, resume, or escalate a task while holding a lock, including the runtime's own lock on a task while it runs the task's handlers; faster code over shorter code on the path that enqueues a task; `guard` but no early return from an `if` without an `else`; and documentation in the plain style of The Swift Programming Language. Those loop tests now get one run per simulator job instead of up to 100, so they are less likely to catch a rare bad interleaving there. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01HLuVQLsTNvRnyh155N25UM --- CLAUDE.md | 46 +++++++++++++++++++++++++++++++++++++++++++++ Scripts/build.swift | 3 --- 2 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 CLAUDE.md diff --git a/CLAUDE.md b/CLAUDE.md new file mode 100644 index 0000000..0c8b223 --- /dev/null +++ b/CLAUDE.md @@ -0,0 +1,46 @@ +# swift-async-queue + +## Tests + +All tests must be deterministic. A test gives the same result on every +run of the same code: on correct code it passes every run, and on the +regression it guards it fails every run, or hangs, in which case CI's +job timeout is the failure. + +- Force the interleaving a test needs with semaphores, manual + executors, and test hooks. +- Never rely on sleeps, wall-clock timeouts, polling, or repetition. +- CI runs each test once. + +A few older tests loop in the hope of catching a bad interleaving: the +`sendsEventsInOrder` tests, the `executesAsyncBlocksAtomically` tests, +and `task_isNotReentrant` and `throwingTask_isNotReentrant`. They stay +as backstops because no deterministic test catches what they catch yet. +Delete a probabilistic test only once a deterministic test fails 10 of +10 runs for every bug it catches. + +## Coverage + +Code coverage must stay at 100%, the target in `codecov.yml`. + +## Concurrency + +Never cancel, resume, or escalate a task while holding a lock. That +includes the runtime's own lock on a task, which it holds while it runs +the task's cancellation and priority escalation handlers: do that work +after the lock is released, or from a new task. + +## Performance + +Prefer the faster code over the shorter code on the path that enqueues +a task. + +## Style + +`guard` is fine, but don't return early from an `if` without an `else`. + +## Documentation + +Write the README and documentation comments in the plain style of The +Swift Programming Language's chapter on concurrency. Use the terms it +uses, and don't invent new concepts. diff --git a/Scripts/build.swift b/Scripts/build.swift index 443dc32..ff9b06a 100755 --- a/Scripts/build.swift +++ b/Scripts/build.swift @@ -114,9 +114,6 @@ for rawPlatform in rawPlatforms { xcodeBuildArguments.append("YES") xcodeBuildArguments.append("build") xcodeBuildArguments.append("test") - xcodeBuildArguments.append("-test-iterations") - xcodeBuildArguments.append("100") - xcodeBuildArguments.append("-run-tests-until-failure") try execute(commandPath: "/usr/bin/xcodebuild", arguments: xcodeBuildArguments) } From 47c7dadced0c31fcefaf99d9a22de7bb60fff288 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 16 Nov 2026 05:10:48 +0000 Subject: [PATCH 4/5] Run the manual executors' jobs without a dispatch queue ManualTaskExecutor and ManualSerialExecutor ran their jobs inside `DispatchQueue.sync` on a private serial queue, which only serialized calls to `runUntilIdle()`. No test needs that. Each test makes its own executors, and calls `runUntilIdle()` from its own task, one call after another. No job calls `runUntilIdle()` on the executor that runs it. So run the jobs directly on the calling thread, as `DispatchQueue.sync` already did, and say in the documentation comments that a test calls `runUntilIdle()` from one task at a time. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01HLuVQLsTNvRnyh155N25UM --- .../Utilities/ManualExecutors.swift | 24 ++++++++----------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/Tests/AsyncQueueTests/Utilities/ManualExecutors.swift b/Tests/AsyncQueueTests/Utilities/ManualExecutors.swift index 637a8c4..82bddae 100644 --- a/Tests/AsyncQueueTests/Utilities/ManualExecutors.swift +++ b/Tests/AsyncQueueTests/Utilities/ManualExecutors.swift @@ -71,13 +71,12 @@ final class ManualTaskExecutor: TaskExecutor { /// Runs every pending job, and every job those jobs enqueue on this executor, until none is pending. /// - /// The jobs run synchronously, one call at a time. `DispatchQueue.sync` runs them on the calling thread, so a job run - /// from a test's task runs nested inside that task, which the runtime makes current again once the job returns. + /// The jobs run synchronously on the calling thread, so a job run from a test's task runs nested inside that task, + /// which the runtime makes current again once the job returns. Call this function from one task at a time, and never + /// from a job that this executor runs. func runUntilIdle() { - dispatchQueue.sync { - while let job = state.withLock({ $0.pendingJobs.isEmpty ? nil : $0.pendingJobs.removeFirst() }) { - job.runSynchronously(on: asUnownedTaskExecutor()) - } + while let job = state.withLock({ $0.pendingJobs.isEmpty ? nil : $0.pendingJobs.removeFirst() }) { + job.runSynchronously(on: asUnownedTaskExecutor()) } } @@ -117,7 +116,6 @@ final class ManualTaskExecutor: TaskExecutor { private let enqueuedJobsContinuation: AsyncStream.Continuation private let state = Mutex(State()) - private let dispatchQueue = DispatchQueue(label: "ManualTaskExecutor") } /// A serial executor that runs its jobs only when a test calls `runUntilIdle()`. @@ -149,13 +147,12 @@ final class ManualSerialExecutor: SerialExecutor { /// Runs every pending job, and every job those jobs enqueue on this executor, until none is pending. /// - /// The jobs run synchronously, one call at a time. `DispatchQueue.sync` runs them on the calling thread, so a job run - /// from a test's task runs nested inside that task, which the runtime makes current again once the job returns. + /// The jobs run synchronously on the calling thread, so a job run from a test's task runs nested inside that task, + /// which the runtime makes current again once the job returns. Call this function from one task at a time, and never + /// from a job that this executor runs. func runUntilIdle() { - dispatchQueue.sync { - while let job = pendingJobs.withLock({ $0.isEmpty ? nil : $0.removeFirst() }) { - job.runSynchronously(on: asUnownedSerialExecutor()) - } + while let job = pendingJobs.withLock({ $0.isEmpty ? nil : $0.removeFirst() }) { + job.runSynchronously(on: asUnownedSerialExecutor()) } } @@ -163,7 +160,6 @@ final class ManualSerialExecutor: SerialExecutor { private let enqueuedJobsContinuation: AsyncStream.Continuation private let pendingJobs = Mutex<[UnownedJob]>([]) - private let dispatchQueue = DispatchQueue(label: "ManualSerialExecutor") } /// Reserves the next turn of `turns` for a probe task that runs only when a test runs it. From 2bf44ae83608640289f2265ddaeff07606d3fc7f Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 16 Nov 2026 07:54:40 +0000 Subject: [PATCH 5/5] Upload test results and crash reports when a test job fails A test process that crashes, such as one in a simulator, fails the job without saying why in the log. When the Xcode jobs fail, upload the result bundles that xcodebuild writes under .build/derivedData and the crash reports in ~/Library/Logs/DiagnosticReports. When the macOS swift test jobs fail, upload the crash reports. The artifacts expire after 7 days. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01HLuVQLsTNvRnyh155N25UM --- .github/workflows/ci.yml | 46 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index cdc1e7f..77668ed 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -47,6 +47,19 @@ jobs: with: fail_ci_if_error: true verbose: true + # A test process that crashes, such as one in a simulator, fails + # the job without saying why in the log. The test results and the + # crash reports that macOS writes for the process explain it. + - name: Upload Test Results and Crash Reports + if: failure() + uses: actions/upload-artifact@v7 + with: + name: xcode-test-results-${{ matrix.platforms }} + path: | + .build/derivedData/*/Logs/Test/*.xcresult + ~/Library/Logs/DiagnosticReports/*.ips + if-no-files-found: ignore + retention-days: 7 # Xcode 27 can't download the iOS 18 simulator runtime, but the # `macos-15` image has it installed, so this job copies its disk image # and passes it to `spm-minimum-ios` as an artifact. @@ -104,6 +117,19 @@ jobs: with: fail_ci_if_error: true verbose: true + # A test process that crashes, such as one in a simulator, fails + # the job without saying why in the log. The test results and the + # crash reports that macOS writes for the process explain it. + - name: Upload Test Results and Crash Reports + if: failure() + uses: actions/upload-artifact@v7 + with: + name: xcode-test-results-iOS18 + path: | + .build/derivedData/*/Logs/Test/*.xcresult + ~/Library/Logs/DiagnosticReports/*.ips + if-no-files-found: ignore + retention-days: 7 spm-swift: name: Swift Build Xcode runs-on: xcode-27 @@ -119,6 +145,16 @@ jobs: run: sudo xcode-select --switch /Applications/Xcode_27.2.app/Contents/Developer - name: Build and Test Framework run: xcrun swift test -c release -Xswiftc -enable-testing + # A test process that crashes fails the job without saying why in + # the log. The crash report that macOS writes for it explains it. + - name: Upload Crash Reports + if: failure() + uses: actions/upload-artifact@v7 + with: + name: swift-build-xcode-crash-reports + path: ~/Library/Logs/DiagnosticReports/*.ips + if-no-files-found: ignore + retention-days: 7 tsan: name: "Thread Sanitizer on macOS" runs-on: xcode-27 @@ -138,6 +174,16 @@ jobs: # with an error, which fails this step. - name: Test With Thread Sanitizer run: xcrun swift test --skip-build --sanitize=thread + # A test process that crashes fails the job without saying why in + # the log. The crash report that macOS writes for it explains it. + - name: Upload Crash Reports + if: failure() + uses: actions/upload-artifact@v7 + with: + name: thread-sanitizer-crash-reports + path: ~/Library/Logs/DiagnosticReports/*.ips + if-no-files-found: ignore + retention-days: 7 linux: name: "Build and Test on Linux" runs-on: ubuntu-24.04