Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 122 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,89 @@ 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.
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
# 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
Expand All @@ -62,6 +145,45 @@ 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
# 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
# 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
Expand Down
46 changes: 46 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
@@ -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.
12 changes: 8 additions & 4 deletions Scripts/build.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)"

Expand All @@ -50,7 +56,8 @@ enum Platform: String, CaseIterable, CustomStringConvertible {

var sdk: String {
switch self {
case .iOS:
case .iOS,
.iOS18:
"iphonesimulator"

case .tvOS:
Expand Down Expand Up @@ -107,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)
}
40 changes: 40 additions & 0 deletions Scripts/copy-ios-18-simulator.sh
Original file line number Diff line number Diff line change
@@ -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
80 changes: 80 additions & 0 deletions Scripts/install-ios-18-simulator.sh
Original file line number Diff line number Diff line change
@@ -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'
24 changes: 10 additions & 14 deletions Tests/AsyncQueueTests/Utilities/ManualExecutors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}

Expand Down Expand Up @@ -117,7 +116,6 @@ final class ManualTaskExecutor: TaskExecutor {

private let enqueuedJobsContinuation: AsyncStream<Void>.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()`.
Expand Down Expand Up @@ -149,21 +147,19 @@ 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())
}
}

// MARK: Private

private let enqueuedJobsContinuation: AsyncStream<Void>.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.
Expand Down
Loading