Skip to content

Update coverage counters atomically and time out test jobs - #95

Merged
dfed merged 1 commit into
mainfrom
claude/2.0-0-atomic-coverage
Nov 17, 2026
Merged

dfed merged 1 commit into
mainfrom
claude/2.0-0-atomic-coverage

Conversation

@dfed

@dfed dfed commented Nov 15, 2026

Copy link
Copy Markdown
Owner

Summary

Four CI and test-target changes that the rest of the 2.0 series relies on. None changes the library.

  • Atomic coverage counters. Coverage counters are plain loads and stores, and Swift Testing runs tests in parallel, so a counter that several tests increment can lose an increment. llvm-cov does not count every region directly: it subtracts one counter from another for an else branch, or for the code after a guard that can return or a call that can throw. A lost increment can then make a line that ran report as never executed, which breaks the 100% coverage target at random. The Linux coverage job now passes -Xswiftc -Xllvm -Xswiftc -instrprof-atomic-counter-update-all, so every count is exact. This adds about a second to the build.
  • A guard for the flag. A build system can drop the flag; SwiftPM's swiftbuild build system dropped it from SafeDI's macOS build. A new step, "Check Atomic Coverage Counters", runs between the tests and "Prepare Coverage Reports". It disassembles the test binary with llvm-objdump into .build/disassembly.txt and fails with an ::error:: annotation if any coverage counter update lacks a lock prefix, or if none has one, as when the binary has no counters. Writing the disassembly to a file rather than piping it means a missing test binary or a missing llvm-objdump fails the step with llvm-objdump's or the shell's own error, rather than with a misleading "no atomic coverage counter updates" annotation. The check reads x86_64 code, which the ubuntu-24.04 runner runs; on another architecture it finds no lock and fails rather than passing silently.
  • Job timeouts. Every job that runs tests now has timeout-minutes: 50 for the Xcode simulator matrix and 15 for the others. On recent runs of main the simulator jobs took 4 to 11 minutes and the others about a minute. A test that deadlocks keeps the test run from ending, and would otherwise hold a runner for GitHub's six-hour default. Later PRs add tests that can only hang when the code they guard regresses, so for those the timeout is the failure signal.
  • Unused Expectation helper removed. Tests/AsyncQueueTests/Utilities/Expectation.swift and ExpectationTests.swift are a copy of the swift-testing-expectation package that no other test refers to. Its fulfillment(withinSeconds:) fails a test by wall-clock timeout, so a test that used it would depend on timing.

The Xcode jobs still upload coverage built with plain counters. Codecov merges the uploads and counts a line as covered when any upload reports it as run, so the exact Linux report keeps a line that ran from reading as missed.

Tests

No test is added. The six ExpectationTests tests are removed with the helper they test.

Four lines of main's CancellableQueueTests.swift still read zero here and in #86: lines 217, 218 and 316 in every run so far, and 315 in some. They are the closing braces of the while true { try Task.checkCancellation() } spinner and of its closure, in cancelTasks_actorQueue_cancelsCurrentlyExecutingAndPendingTasks and cancelTasks_mainActorQueue_cancelsCurrentlyExecutingAndPendingTasks. They are not lost increments: llvm-cov counts them as the loop's completed iterations (regions 217:5 and 315:5), so they read zero whenever cancellation reaches the spinner before it finishes its first iteration, which depends on thread timing. They read the same with atomic counters. #87 rewrites both spinners, and from #87 on no line in Sources or Tests reads zero.

Series

PR 1 of 10 in the 2.0 series (this PR, then #86 through #94, each stacked on the previous one). It is based on main, and #86 is stacked on it. It changes no API and no library code.

Validation

On Linux, from a git archive copy, with Swift 6.2.4 as the swift:6.2 job uses:

  • The build has exactly the nine weak var warnings main has, and swiftformat --lint is clean.
  • The full suite passed 7 runs unpinned and 3 with the test runner pinned to one CPU.
  • The linux-6-2 job's three steps, run from this commit's ci.yml under bash -e, pass: "903 atomic and 0 non-atomic coverage counter updates." With the flag dropped from the test command, the check fails with both ::error:: lines ("885 coverage counter updates in the test binary are not atomic." and "The test binary has no atomic coverage counter updates.").
  • The same check passes at Cancel queued tasks outside of Delivery's lock #86 (933 atomic) and Cancel tasks outside of CancellableQueue's lock #87 (997 atomic), and fails at both with the flag dropped.
  • Across the CI run and 4 more runs of the test binary (2 pinned), the only zero lines are the four spinner lines above.

llvm-objdump is in the Swift 6.2.4 and 6.4.2 toolchains' usr/bin, next to the llvm-cov the job already calls, so it should be in the swift:6.2 image; the first CI run confirms it. If it were missing, the step would fail with "llvm-objdump: not found" (exit 127, checked here under bash -e and sh -e with llvm-objdump removed from PATH); with the test binary missing, it fails with llvm-objdump's "No such file or directory". Neither case prints an ::error:: annotation.

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

Review checklist

Left for code review because no test can check them:


Generated by Claude Code

Coverage counters are plain loads and stores, and Swift Testing runs
tests in parallel, so a counter that several tests increment can lose
an increment. llvm-cov does not count every region directly: it
subtracts one counter from another for an `else` branch, or for the
code after a `guard` that can return or a call that can throw. A lost
increment can then make a line that ran report as never executed,
which breaks the 100% coverage target at random.

The Linux coverage job now passes `-instrprof-atomic-counter-update-all`
to LLVM, which makes every counter update atomic, so every count is
exact. This adds about a second to the build. The Xcode jobs still
upload coverage built with plain counters. Codecov merges the uploads
and counts a line as covered when any upload reports it as run, so the
exact Linux report keeps a line that ran from reading as missed.

A build system can drop the flag: SwiftPM's swiftbuild build system
dropped it from SafeDI's macOS build. A new step after the tests
disassembles the test binary with llvm-objdump and fails with an
`::error::` annotation if any coverage counter update lacks a `lock`
prefix, or if none has one, as when the binary has no counters. It
writes the disassembly to a file before reading it, so that a missing
llvm-objdump or test binary fails the step with llvm-objdump's own
error rather than with a misleading annotation. The check reads x86_64
code, which the ubuntu-24.04 runner runs; on another architecture it
finds no `lock` and fails.

Every job that runs tests now times out: the simulator jobs after 50
minutes and the others after 15. On recent runs of main, the simulator
jobs took 4 to 11 minutes and the others about a minute. A test that
deadlocks keeps the test run from ending, and would otherwise hold a
runner for GitHub's six-hour default, so for a regression that makes a
test hang, the timeout is the failure signal.

The unused `Expectation` helper and its tests are removed. They are a
copy of the swift-testing-expectation package that no other test
refers to, and its `fulfillment(withinSeconds:)` fails a test by
wall-clock timeout, so a test that used it would depend on timing.

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

codecov Bot commented Nov 15, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (a33a3f6) to head (02d93e6).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##              main       #95    +/-   ##
==========================================
  Coverage   100.00%   100.00%            
==========================================
  Files           13        11     -2     
  Lines         1311      1209   -102     
==========================================
- Hits          1311      1209   -102     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@dfed
dfed added this pull request to stack #103 November 16, 2026 21:42
@dfed
dfed merged commit b6a2c2f into main Nov 17, 2026
12 checks passed
@dfed
dfed deleted the claude/2.0-0-atomic-coverage 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