Skip to content

Make the scheduling fixture's order checks tolerate enqueue pauses - #110

Open
Joseph-Cursio wants to merge 1 commit into
swiftlang:mainfrom
Joseph-Cursio:fix/fixture-per-job-delays
Open

Joseph-Cursio wants to merge 1 commit into
swiftlang:mainfrom
Joseph-Cursio:fix/fixture-per-job-delays

Conversation

@Joseph-Cursio

Copy link
Copy Markdown

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):

  • TSan: 16 of 16 runs pass, where 3 of 8 failed before.
  • Plain runs pass, and swift format lint is clean.
  • Injected pauses: each one fails the current fixture and passes with this change.
  • Negative controls: a job scheduled later than it claims, and an enqueue that 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].

  • Delays took one start instant before the loop, but enqueue(_: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 produced FAILED ([15, 30, 250, 100, 500, 1000]).
  • Timestamps uses absolute targets, so a pause can let a target pass before its job is enqueued. The job then can't run at its target.

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, inside SchedulingExecutor.enqueue(_:after:) → PThreadExecutor.enqueue(_:at:) → modifyMultiThreadedStateAndWakeUpIfNeeded. Busy-wait pauses injected into the loop reproduce both failures with no TSan:

Injected pause Current fixture With this PR
160 ms after the 250 ms enqueue (Delays) FAILED ([15, 30, 250, 100, 500, 1000]), 3/3 passes, 3/3
20 ms after the 30 ms enqueue (Delays) FAILED ([30, 15, 100, 250, 500, 1000]), 3/3 passes, 5/5
120 ms after the +30 ms enqueue (Timestamps) order failure, 2/2 passes, 3/3

What 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 its enqueue call 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 (the RunLoopExecutor path), they run in deadline order after the loop. Each job's deadline is bounded:

  • Delays: [call start + delay, call return + delay]. The default enqueue(_:after:) reads the clock during the call.
  • Timestamps: [target, max(target, call return)]. PThreadExecutor keys 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 enqueue call. Timestamps measures from max(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 any enqueue call that takes longer than maxDelayTolerance (250 ms). Without it, an executor whose enqueue sleeps until the deadline would pass under the new pause tolerance.

Smaller changes.

  • Each sub-test records its runs in one Mutex of (job, instant) pairs.
  • A failure message lists any missing jobs.
  • An order failure names both jobs, with their deadline bounds and run instants.

Verification

All in swift:6.4-resolute (Swift 6.4.2, aarch64), Docker Desktop on Apple silicon.

  • Plain runs: swift test passes 3 of 3. swift format lint is clean, and swift format changes nothing.
  • Injected pauses: see the table above.
  • Negative controls: each fails with a specific message.
    • In each sub-test, the 15 ms job is secretly scheduled at 300 ms. The message is the 15 ms job (due by …) ran at …, after the 30 ms job (due no earlier than …) ran at ….
    • A 300 ms busy-wait inside the 100 ms job's enqueue window gives enqueueing the 100 ms job took 0.300003084 seconds.
  • TSan with the real 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.
  • Independent review: three reviewers, each finding checked by a skeptic. They confirmed that PThreadExecutor keys a past-due job by its target. That is why the Timestamps lower bound is the target itself.

Known limits, unchanged by this PR

  • Pool threads can wake late. Pools (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 on main.
  • 2-core hosts. With a pool of 2, a long pause inside the stop job's enqueue can hold up one thread's due jobs. That case also fails on main.
  • The stop job still fires at 1100 ms, which is inside the 1000 ms job's lateness tolerance. A job it cuts off is now named in the failure message.

🤖 Generated with Claude Code

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>

@FranzBusch FranzBusch left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

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