Repository navigation
Make the scheduling fixture's order checks tolerate enqueue pauses - #110
Open
Joseph-Cursio wants to merge 1 commit into
Open
Joseph-Cursio wants to merge 1 commit into
Joseph-Cursio wants to merge 1 commit into
Conversation
ExecutorFixture's Delays and Timestamps sub-tests failed when the thread running their enqueue loop paused, though the executor ran every job correctly. Under ThreadSanitizer on Linux this failed test(ioBackend:) in about 3 of 8 runs; any 15 ms preemption at the wrong moment can do the same on a busy CI machine. Unstalled runs are checked as strictly as before. Details: The cause. Both sub-tests enqueue six jobs, longest first, and required the run order to be exactly the nominal one. Delays measured every job from one start instant taken before the loop, but enqueue(_:after:) counts from the moment of the call, so a pause between two enqueues shifted every later job by the length of the pause and could put a short job after a longer one (a 20 ms pause between the 30 and 15 ms enqueues is enough). Timestamps used absolute targets, so a pause could let a target pass before its job was enqueued. Under TSan, false-positive reports on Synchronization.Mutex (swiftlang/swift#94511) are printed on the thread that hits them, sometimes inside enqueue, pausing the loop for 50-150 ms. The new order check rejects only orders a correct executor can't produce: a job ran while another job, whose deadline came first by more than 10 ms, had been enqueued more than 10 ms before it ran. Deadlines are bounded per job: [call start, call return] + delay for Delays, and [target, max(target, call return)] for Timestamps, because PThreadExecutor keys a past-due job by its target but may already have taken other jobs for the current tick. Whether jobs that come due during a pause run on time or only after the loop depends on whether the loop runs on the executor's own thread, which is why the check doesn't predict a single order. Other changes: - Delays measures each job's lateness from its own enqueue call. - Timestamps measures lateness from max(target, enqueue return). - A new check fails any enqueue call that takes longer than maxDelayTolerance (250 ms), so an executor whose enqueue sleeps until the deadline can't hide behind the pause tolerance. - Each sub-test records its runs in one Mutex of (job, instant) pairs, failure messages name missing jobs, and an order failure names both jobs with their deadlines and run instants. Accepted weakening: in Delays, a pause of 5 ms or more between the 30 and 15 ms enqueues can excuse a real swap of that pair, because the 15 ms nominal gap minus the 10 ms slack leaves 5 ms. Verified in swift:6.4-resolute (Swift 6.4.2, aarch64): - Plain runs: 3 of 3 pass. swift format lint is clean and reformats nothing. - Injected pauses, no TSan: 160 ms after the 250 ms enqueue (3/3 pass), 20 ms after the 30 ms enqueue (5/5), 120 ms after the +30 ms Timestamps enqueue (3/3). The unchanged fixture fails the Timestamps pause 2/2 and the Delays pauses 3/3. - Negative controls fail as intended: a 15 ms job actually scheduled for 300 ms, in each sub-test, and an enqueue call that takes 300 ms. - TSan with the real Mutex (swift-collections patched locally for swiftlang/swift#86265 so it builds under TSan): 16 of 16 runs pass, with 18-29 false race reports each. Before this change, 3 of 8 failed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Joseph-Cursio
requested review from
FranzBusch,
al45tair and
ktoso
as code owners
November 20, 2026 13:29
FranzBusch
reviewed
Nov 23, 2026
FranzBusch
left a comment
Member
There was a problem hiding this comment.
I'm not yet sure this is the best way to go about it. Ultimately we are at the mercy of the OS scheduler here. My feeling is we should just rewrite how these tests work and not assert on things that are bound to become flakey.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
ExecutorFixture's scheduling sub-tests (Delays and Timestamps) fail whenever the thread running their enqueue loop pauses, even though the executor runs every job correctly. Under ThreadSanitizer on Linux,PThreadExecutorTests.test(ioBackend:)failed in 3 of 8 runs this way. Without TSan, a pause of about 15 ms at the wrong moment, such as a preemption on a busy CI runner, is enough. This PR changes only the test fixture. The order check now rejects only orders a correct executor can't produce, and runs without a pause are checked as strictly as before.Evidence, all in
swift:6.4-resolute(Swift 6.4.2, aarch64):swift format lintis clean.enqueuethat blocks for 300 ms, still fail.One accepted trade-off: in Delays, a pause of 5 ms or more between the 30 ms and 15 ms enqueues can excuse a real swap of those two jobs.
Full write-up (Claude Opus 5.5)
The failure
Both sub-tests enqueue six jobs, longest first (1000, 500, 250, 100, 30 and 15 ms), and required the run order to be exactly
[15, 30, 100, 250, 500, 1000].startinstant before the loop, butenqueue(_:after:)counts from the moment of each call. A pause between two enqueues therefore shifts every later job by the length of the pause. In one failing TSan run, the loop paused for about 152 ms just before the 100 ms enqueue. The 15, 30 and 100 ms jobs ran at 167, 182 and 252 ms, while the 250 ms job, enqueued before the pause, ran on time at 250 ms. That producedFAILED ([15, 30, 250, 100, 500, 1000]).Under TSan the pauses come from false-positive race reports on
Synchronization.Mutex(swiftlang/swift#94511). TSan prints them on the thread that hits them. The report in that run was raised on the test's own thread, insideSchedulingExecutor.enqueue(_:after:)→PThreadExecutor.enqueue(_:at:)→modifyMultiThreadedStateAndWakeUpIfNeeded. Busy-wait pauses injected into the loop reproduce both failures with no TSan:FAILED ([15, 30, 250, 100, 500, 1000]), 3/3FAILED ([30, 15, 100, 250, 500, 1000]), 3/3What the checks do now
Order. A job must not run while another job with an earlier deadline is waiting. Concretely, the check fails when the other job's deadline came first by more than
orderingSlack(10 ms), and itsenqueuecall had returned more than 10 ms before the first job ran. It doesn't predict one order, because what happens during a pause depends on the executor. If the loop runs on another thread, jobs that come due during the pause run on time. If it runs on the executor's own thread (theRunLoopExecutorpath), they run in deadline order after the loop. Each job's deadline is bounded:[call start + delay, call return + delay]. The defaultenqueue(_:after:)reads the clock during the call.[target, max(target, call return)].PThreadExecutorkeys a past-due job by its target, but by then it may already have taken other jobs into the current tick.With no pause, every enqueue returns within microseconds of the loop's start. The bounds then collapse to the nominal deadlines, which are at least 15 ms apart, so the check is as strict as the old equality check.
Lateness. Delays measures each job from the start of its own
enqueuecall. Timestamps measures frommax(target, call return). The minimum-time checks are unchanged, and the Delays minimum is slightly stricter, since it no longer counts the loop's own time.Slow
enqueue. A new check fails anyenqueuecall that takes longer thanmaxDelayTolerance(250 ms). Without it, an executor whoseenqueuesleeps until the deadline would pass under the new pause tolerance.Smaller changes.
Mutexof(job, instant)pairs.Verification
All in
swift:6.4-resolute(Swift 6.4.2, aarch64), Docker Desktop on Apple silicon.swift testpasses 3 of 3.swift format lintis clean, andswift formatchanges nothing.the 15 ms job (due by …) ran at …, after the 30 ms job (due no earlier than …) ran at ….enqueueing the 100 ms job took 0.300003084 seconds.Mutex: 16 of 16 runs pass, with 18 to 29 false race reports each. Before the change, 3 of 8 failed. Building under TSan needed swift-collections patched locally for Invalid bitcast in non-copyable struct with pointer and TSAN swift#86265.PThreadExecutorkeys a past-due job by its target. That is why the Timestamps lower bound is the target itself.Known limits, unchanged by this PR
PThreadTaskExecutor) give each job's thread its own timer. A pool thread that wakes more than about 15 ms late can still fail the order check, as it does onmain.main.🤖 Generated with Claude Code