Skip to content

fix(concurrency): blocking engine work stays off the Swift cooperative pool - #725

Merged
superuser404notfound merged 2 commits into
mainfrom
fix/cooperative-pool-blocking
Oct 8, 2026
Merged

superuser404notfound merged 2 commits into
mainfrom
fix/cooperative-pool-blocking

Conversation

@superuser404notfound

Copy link
Copy Markdown
Owner

Summary

CI's swift test went red on a different set of unrelated tests nearly every run (#719, #723 and several runs on main). The cause is one mechanism: blocking work on the Swift cooperative pool, which has one thread per core and never grows. The xcode-27-arm64 runner has 3 cores. When a handful of jobs blocked, every other test starved, and so did the time-limit watchdogs: nine 120 s limits fired together at 151 s.

The blocking came from both sides:

  • Engine: every Task.detached ran on the pool. That covered demuxer opens and packet reads, the live ingest's FIFO writes under backpressure, the native subtitle readers for the whole session, stop()'s waitForFinish, recording finish and probe closes. This affects host apps too: on an Apple TV every blocked job takes a core's worth of async work out of the app.
  • Tests: synchronous engine APIs and parked gates were driven straight from test bodies.

Measured locally by sampling a full run: 53 s of blocked pool time in a 24 s run. LIBDISPATCH_COOPERATIVE_POOL_STRICT=1 (a one-thread pool) reproduces the CI failure on any Mac: 26 issues across 470 s before this change.

What changed

  • BlockingWork.detached creates the same detached task with an executor preference for BlockingExecutor, which runs jobs on GCD's global queues; those add a thread when one blocks. Awaits, main-actor hops and cancellation are unchanged. visionOS 1 has no TaskExecutor and keeps Task.detached.
  • Every Task.detached in Sources/AetherEngine goes through it, and a test refuses new ones.
  • Tests that block on purpose run under a new .offCooperativePool trait (34 suites, chosen from the sample).
  • CI also runs the Swift Testing suite on a one-thread pool, so new blocking code fails deterministically instead of as a flake.
  • The suite now runs in about 13 s instead of 72, which exposed tests that had leaned on the old pace. Each is fixed at its race; the second commit lists them.
  • docs/architecture.md gains a section on blocking work and the cooperative pool, and docs/api.md warns hosts that a detached task is no background thread.

Test plan

  • Locally: four full swift test runs (both runners) and one Swift Testing run on a one-thread pool, all green.
  • Counter-check: with BlockingWork switched back to plain Task.detached, the new blocked-jobs test hangs on a one-thread pool, and the time-limit watchdog never gets to run, the same as on the runner.
  • Scripts/check-doc-links.py passes.

🤖 Generated with Claude Code

https://claude.ai/code/session_01WJcXkGQrMiBPixVEFTSBJS

Vincent Herbst and others added 2 commits October 8, 2026 08:58
…e pool

Every detached engine task ran on the cooperative pool, one thread per core
that never grows: demuxer opens and packet reads, the live ingest's FIFO
writes under backpressure, the native subtitle readers for the whole session,
stop()'s waitForFinish, recording finish, probe closes. Each blocked job took
a core's worth of async work out of the process, the host app's included.

BlockingWork.detached creates the same task with an executor preference for
BlockingExecutor, which runs jobs on GCD's global queues; those add a thread
when one blocks. Awaits, main-actor hops and cancellation are unchanged.
visionOS 1 has no TaskExecutor and keeps Task.detached. A test refuses any
Task.detached that comes back into Sources/AetherEngine, and CI also runs the
suite on a one-thread pool (LIBDISPATCH_COOPERATIVE_POOL_STRICT=1), which
reproduces the 3-core runner's starvation deterministically on any Mac.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WJcXkGQrMiBPixVEFTSBJS
…t hid behind the slow pace

On the 3-core CI runner a handful of tests blocking pool threads starved the
rest, so a different set of unrelated tests timed out every run: nine 120 s
limits fired together at 151 s because their watchdog is a pool task too.
The 34 suites measured blocking the pool (sample of a full run, leaf waits
under completeTaskWithClosure) run under the new .offCooperativePool trait,
and the AE#629 test models the engine's detached read through BlockingWork.

The suite now runs in about 13 s instead of 72, which exposed tests that had
leaned on the old pace:
- LiveStartupAdmission: a parked job's 30 s deadline makes the gate return
  true, so a late test read it as an admission; the jobs now wait a day.
- Issue281: a 3 s suffix stall could land before the late read; 60 s now,
  and the 200-body bound sits above the 4 MB the loopback buffers absorb.
- SourceOpenRecovery: the retry can still be on its way when load() gives up.
- RangedResponseIntegrity: the test's own read may refill legitimately; the
  over-delivery check now reads where that refill starts.
- Issue243 / Issue445: process-wide memory figures beside parallel suites;
  best of three, and the census allocation on a VM tag of its own.
- Issue605: 150 ms of quiet is not a park; SoftwarePacketReadAhead reports
  its producer's park the way VideoSegmentProvider reports parked waiters.
- The five suites that clear the process-wide prewarm store run serialized
  against each other under one parent suite.

Four full runs and one on a one-thread pool, all green.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WJcXkGQrMiBPixVEFTSBJS
@superuser404notfound
superuser404notfound merged commit 54fb637 into main Oct 8, 2026
4 checks passed
@superuser404notfound
superuser404notfound deleted the fix/cooperative-pool-blocking branch October 8, 2026 07:22
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.

1 participant