Repository navigation
PropertyLawAsync gains lifetime laws and a seeded job order you can replay.
checkAsyncLifetimePropertyLawschecks five things anAsyncSequenceoperator owes its upstream and its consumer once the consumer stops listening. It runs them in virtual time, so a hang is reported rather than awaited.LawCheckOptions.scheduling = .seededexplores job orders, andCheckResult.seedreplays them.LawCheckOptions.threading = .singleThreadedmakes hops back to the generic executor free.- Work that escapes the virtual-time scheduler is now reported as escaped, not as a false deadlock.
Safe to upgrade. Every API change is source-compatible. At the defaults (.fifo, .splitAtHops) the laws draw, run and fingerprint as in 4.13.0, so stored replay fingerprints still match. All 1,514 tests pass with Swift 6.4.2, on macOS and on Linux (swift:6.4-noble, aarch64). The 17 known issues are deliberate.
Know before upgrading:
checkEveryCarriernow fails when the suite throws. If the suite closure throws anything other than aPropertyLawViolation, the walk throwsCarrierSuiteError. Before 4.14.0 such a test passed after testing nothing, so a test that was silently green can now fail.- Hop splitting, the default, stands in for parallel threads. Nobody has checked that it never produces an order that would be impossible even with parallel threads. Treat a failure found only under the default as a lead to confirm on the real runtime, not as proof.
.singleThreadedstill treats entering an actor as a scheduling point. On the real runtime, entering an idle actor from the generic executor is usually free, and the scheduler does not model that.
Full write-up (Claude Opus 5.5)
What's new
Lifetime laws (PR 105, fifth law in PR 108)
checkAsyncLifetimePropertyLaws(for:using:options:applying:), plus an overload without options:, takes the operator under test as a closure. That closure receives a LifetimeProbeSource<Element> and a LawClock. Both are opaque public types with no public initializers. Each trial runs in virtual time and checks these laws (AsyncLifetimeLaw):
releasesUpstreamWhenConsumerLeaves: the consumer callsnext()once and drops the iterator. Once the run settles, every upstream iterator the operator made is released.cancellationReachesUpstream: the consumer is cancelled outsidenext(). Its nextnext()call returns, and the upstream is released.failureReachesConsumerWithoutDemand: an upstream failure that arrives with no demand outstanding still reaches the consumer's laternext()calls.finishedStaysFinished: once the iterator returnsnilor throws, every furthernext()returnsnil.releasesUpstreamWhenSequenceDroppedLast: the consumer takes one element, then drops the iterator and then the output. This is the order of swift-async-algorithms issue 473's "case 12", and it is the law.seededexists for.
The following behaviours apply to every law:
- Hangs. A trial that can go on only by passing its horizon, or in which every task is blocked, fails with a
hang:counterexample. The counterexample says "ignores cancellation" when the consumer had been cancelled. - Shrinking. Counterexamples shrink by removing single elements, then adjacent pairs.
- Suppression.
LawIdentifier.asyncLifetime(_:)names a law for aLawSuppression. - Assumptions. The operator's output ends once its source ends, and the operator needs at most 200 ms of the clock per element.
Escaped work is reported as escaped (PR 106)
Some work leaves the virtual-time scheduler: a real Task.sleep, MainActor.run, an actor on a custom executor, or a continuation resumed by Dispatch. Such work used to make a run look stuck, and the run called it a deadlock.
Now each run claims the tasks it creates, and an escape is recorded when one of them is enqueued from outside the run. Other signs also count as an escape: the body finishing on another thread, the virtual clock being used from another thread, and another live run draining one of the run's tasks.
All of this is internal. What a user sees:
- the lifetime laws fail an escaped run with
escaped:, nothang:; - the deadlock message now says the run may have escaped;
- a real deadlock is reported after a 100 ms real-time grace window.
Seeded job order with replay (PR 108)
LawCheckOptions.scheduling takes a SchedulingStrategy: .fifo, the default and unchanged, or .seeded.
- How it works. Under
.seeded, each trial of the timed and lifetime laws draws one schedule seed right after its input, and the scheduler picks among pending jobs with that seed.CheckResult.seedtherefore replays inputs and job orders alike, on the same toolchain, build configuration and OS. - What the replay line says. A seeded failure's replay line names the strategy. It also warns when
SWIFT_DETERMINISTIC_HASHINGis not1. - Step limit. A virtual-time run that takes 1,000,000 jobs without a verdict now fails with
stepLimitExceededinstead of hanging. Examples are a task that re-enqueues itself forever, or one that settles in a loop. Teardown is bounded too. - Scope. Only the virtual-time suites honour
scheduling:checkTimedAsyncSequencePropertyLawsandcheckAsyncLifetimePropertyLaws. Every other law ignores it and keeps its.fifodraws, fingerprint and failure text.
Threading model (PR 109)
LawCheckOptions.threading takes a ThreadingModel: .splitAtHops (the default) or .singleThreaded.
.splitAtHops. A task paused at a hop back to the generic executor rejoins the queue as a new job. Another task can then run at that point, as a parallel thread could on the real runtime. Seeded runs caught supabase-swift PR 1428 this way..singleThreaded. The paused task runs next, with no draw, so the hop is free in effect. Explicit suspensions (Task.yield(), sleeps on the clock, continuations) and actor entry stay scheduling points.- When to choose which. Keep the default for code that runs on the multi-threaded runtime. Choose
.singleThreadedfor code that only ever runs on one thread, or to sort out a failure: a failure that remains under.singleThreadedneeds no parallel thread. - Where it is documented. The PRD section "Hops as scheduling points" (§4.4) and the doc comment on
ThreadingModel.
checkEveryCarrier no longer passes a suite that throws (PR 102)
The bug. A suite closure that threw anything other than a PropertyLawViolation contributed no results, and the carrier still counted as visited. A suite that threw for every carrier passed.
The fix. The walk now stops at that carrier and throws CarrierSuiteError(carrier:underlying:), which names the carrier in its counterexample form.
What still passes through unwrapped. These errors mean "stop" rather than "this carrier failed":
- cancellation, including Swift Testing's
Test.cancel(); - a failed
#require; ReplayEnvironmentMismatch.
API changes
Everything below is new. Existing declarations changed only by gaining trailing parameters with defaults.
| Module | Addition |
|---|---|
| PropertyLawAsync | checkAsyncLifetimePropertyLaws(for:using:options:applying:) and checkAsyncLifetimePropertyLaws(for:using:applying:) |
| PropertyLawAsync | AsyncLifetimeLaw (5 cases), LawIdentifier.asyncLifetime(_:) |
| PropertyLawAsync | LawClock (a Clock) and LifetimeProbeSource<Element> (an AsyncSequence), with no public initializers |
| PropertyLawKit | SchedulingStrategy (.fifo, .seeded), ThreadingModel (.splitAtHops, .singleThreaded), each with identifier and description |
| PropertyLawKit | LawCheckOptions.scheduling and .threading, plus matching trailing init parameters defaulting to .fifo and .splitAtHops |
| PropertyLawKit | Environment.scheduling and .threading (String?), plus matching trailing init parameters defaulting to nil |
| PropertyLawKit | CarrierSuiteError |
Fingerprints. Environment.scheduling and Environment.threading stay nil at the defaults and outside virtual time:
- Decoding. A fingerprint encoded by 4.13.0 decodes and compares equal;
SchedulingFingerprintTestschecks this. - Encoding. Default fingerprints encode exactly as before, because nil fields are omitted.
- Relaxations. Every
EnvironmentRelaxationnow also requires these two fields to match, because a different strategy or model replays different inputs and jobs from the same seed. For existing fingerprints both fields arenil, so nothing changes.
swift package diagnose-api-breaking-changes v4.13.0 reports no breaking changes in any module except PropertyLawKit, where it reports two:
Environment.init(swiftVersion:backendIdentity:generatorSchemaHash:)"has been removed";LawCheckOptions.init(budget:…:equalValuePairing:)"has been removed".
Both are the same initializer with new trailing parameters that have defaults. Every existing call compiles unchanged; the test suite still makes the three-argument Environment call. Only an unapplied reference that spells out the old full label list would fail. 4.10.0 added equalValuePairing: to LawCheckOptions.init the same way in a minor release. The package does not use library evolution, so ABI does not apply.
Behaviour changes at the defaults
checkEveryCarrierthrowsCarrierSuiteErrorfor a suite that throws (see above).checkTimedAsyncSequencePropertyLawsreports a run that escaped the scheduler as escaped rather than as a deadlock. A real deadlock is reported after a 100 ms real-time grace window.- A virtual-time run that never settles fails with
stepLimitExceededafter 1,000,000 jobs instead of running on.
Evidence
These results come from the umbrella fuzzing project's evaluation, which used this code before the release:
- Lifetime laws. They caught postgres-nio PR 641 out of sample.
- Seeded job orders. They caught supabase-swift PR 1428 at equal trials, in 3 of 3 repeats, where FIFO did not. That catch depends on hop splitting, which is why
.splitAtHopsstays the default. - Mutation testing. A scoped mutation run on postgres-nio's connection pool found no mutant that seeds catch and FIFO misses, so the case for seeds rests on PR 1428 alone.
- Not re-run. PR 1428 has not been run under the shipped
.singleThreaded. The expected result is that the catch vanishes, as it did under an earlier patch that ran every job under the generic executor's identity.
Tests
swift test was run on the release-prep branch (main at 62bda0a, plus doc-only changes):
| Test target | macOS 27.3, Swift 6.4.2 (arm64) | Linux, swift:6.4-noble, Swift 6.4.2 (aarch64) |
|---|---|---|
| PropertyLawKitTests | 610 passed, 17 known issues | 610 passed, 17 known issues |
| PropertyLawCoreTests | 317 passed | 317 passed |
| PropertyLawDiscoveryToolTests | 237 passed | 237 passed |
| PropertyLawAsyncTests | 137 passed | 137 passed |
| PropertyLawMacroTests | 62 passed | 62 passed |
| PropertyLawCollectionsTests | 48 passed | 48 passed |
| PropertyLawSyntaxTests | 42 passed | 42 passed |
| PropertyLawSyntaxSupportTests | 33 passed | 33 passed |
| PropertyLawMinimalTypesTests | 15 passed | 15 passed |
| PropertyLawDiscoveryPluginTests | 10 passed | 10 passed |
| PropertyLawComplexTests | 3 passed | 3 passed |
| Total | 1,514 passed, 0 failed | 1,514 passed, 0 failed |
The 17 known issues are deliberate withKnownIssue tests.
Also unreleased until now
PR 107 regenerated two async mutant patches under mutants/. It does not change the shipped library.
Not covered
- CI. CI runs macOS tests and Apple-platform builds only. The Linux results above come from a local Docker run on aarch64. x86_64 Linux was not run.
- Toolchains. Only Swift 6.4.2 ran the full suite. The manifest's
swift-tools-versionis 6.1, and Swift 6.1 to 6.3 were not run for this release. Swift 6.3.3 ran the async tests on Linux during development. - Install snippet. The README install snippet still reads
from: "1.0.0", as in earlier releases. It resolves the latest release.
🤖 Generated with Claude Code