DeepFilterNet: wait by Instant, not on the Windows timer tick; deterministic tests (#910) #914

Closed
charles wants to merge 0 commits from fix-910-deepfilter-windows-timing into textchat
Owner

Refs #910

This follows #912. Windows CI run 388 failed sustained_slowness_gives_deep_filter_up_after_50_late_frames: it got 60 frames where 49 were expected.

Cause

  • Product. Remote::process waited with recv_timeout(8 ms). A timed blocking wait in std ends on the system timer tick. On Windows that tick is 15.6 ms by default, and nothing in Flaxative raises the timer resolution: there is no timeBeginPeriod or high-resolution timer anywhere in apps/, crates/ or xtask/. So the "8 ms" wait could hold the engine thread for about 15.6 ms. That thread does both capture and playback refill for 10 ms frames. It also meant a frame back after 12 ms was counted as on time.
    • The engine's own TICK has the same weakness. recv_timeout(5 ms) in Engine::run most likely wakes about every 15.6 ms on Windows, not every 5 ms. The playback ring keeps at least 30 ms, so this is tolerable. I left it out of this PR to keep the scope narrow.
  • Test. The scripted-worker tests compared the worker's sleeps against the chain's timed waits, so they depended on the timer tick.

Fix

  • The wait (Poll). It polls the result channel without sleeping: a short spin, then yield_now. It declares a frame late exactly when Instant passes the deadline, so lateness is judged by the same clock everywhere. A result that is already there is always taken.
    • Cost: DeepFilterNet is normally back in 0.1–0.5 ms, so the engine thread is busy only that long. A late frame costs the 8 ms budget, and 50 late frames in a row still end DeepFilterNet.
    • Why not a fully non-blocking design (take whatever result is ready at the next frame): it needs a fixed extra pipeline delay at least as long as the capture bursts. Capture arrives in bursts of 2 frames or more: PipeWire's default ~21 ms quantum, or WASAPI periods plus Windows' 15.6 ms engine tick. With less delay than that, most frames would go out unprocessed; with that much, it adds 10–20 ms of latency to every call.
  • Tests. A Wait seam lets the tests swap in a scripted wait. It declares the scripted frames late, and only then releases them from the worker. Every assertion is exact: which frames went through the stand-in, the late counts, the reasons, and the in-order output. No assertion uses a clock.
    • The slow-average test uses sleeps only as lower bounds (3 ms of work against a 1 ms budget).
    • Poll is tested on its guarantees: a result that is there is taken even after the deadline; it is late only from the deadline; it reports the thread ending.
    • I reviewed the other tests I added in #912 (the warm-up, delay and no-trace tests in deep_filter.rs). None of them is timed.

Verification

  • The denoise tests passed 40 runs out of 40 on Linux, with 4 busy CPU loops running alongside.
  • FLUXER_SCREENSHOT_TESTS=1 cargo xtask ci (under dbus-run-session): all steps passed.
  • flaxative --check-noise-suppression on Linux: 0 of 300 frames late, white noise lowered by 30.0 dB.

Not verified on Windows: the 15.6 ms tick of recv_timeout is reasoned from how Windows waits work and from the run 388 symptom, not measured. Whether Poll actually holds 8 ms there is also unmeasured; it holds by construction, since it uses no timer. The Windows CI job is the first real run.

🤖 Generated with Claude Code

Refs #910 This follows #912. Windows CI run 388 failed `sustained_slowness_gives_deep_filter_up_after_50_late_frames`: it got 60 frames where 49 were expected. ## Cause - **Product.** `Remote::process` waited with `recv_timeout(8 ms)`. A timed blocking wait in std ends on the system timer tick. On Windows that tick is 15.6 ms by default, and nothing in Flaxative raises the timer resolution: there is no `timeBeginPeriod` or high-resolution timer anywhere in `apps/`, `crates/` or `xtask/`. So the "8 ms" wait could hold the engine thread for about 15.6 ms. That thread does both capture and playback refill for 10 ms frames. It also meant a frame back after 12 ms was counted as on time. - The engine's own `TICK` has the same weakness. `recv_timeout(5 ms)` in `Engine::run` most likely wakes about every 15.6 ms on Windows, not every 5 ms. The playback ring keeps at least 30 ms, so this is tolerable. I left it out of this PR to keep the scope narrow. - **Test.** The scripted-worker tests compared the worker's sleeps against the chain's timed waits, so they depended on the timer tick. ## Fix - **The wait (`Poll`).** It polls the result channel without sleeping: a short spin, then `yield_now`. It declares a frame late exactly when `Instant` passes the deadline, so lateness is judged by the same clock everywhere. A result that is already there is always taken. - Cost: DeepFilterNet is normally back in 0.1–0.5 ms, so the engine thread is busy only that long. A late frame costs the 8 ms budget, and 50 late frames in a row still end DeepFilterNet. - Why not a fully non-blocking design (take whatever result is ready at the next frame): it needs a fixed extra pipeline delay at least as long as the capture bursts. Capture arrives in bursts of 2 frames or more: PipeWire's default ~21 ms quantum, or WASAPI periods plus Windows' 15.6 ms engine tick. With less delay than that, most frames would go out unprocessed; with that much, it adds 10–20 ms of latency to every call. - **Tests.** A `Wait` seam lets the tests swap in a scripted wait. It declares the scripted frames late, and only then releases them from the worker. Every assertion is exact: which frames went through the stand-in, the late counts, the reasons, and the in-order output. No assertion uses a clock. - The slow-average test uses sleeps only as lower bounds (3 ms of work against a 1 ms budget). - `Poll` is tested on its guarantees: a result that is there is taken even after the deadline; it is late only from the deadline; it reports the thread ending. - I reviewed the other tests I added in #912 (the warm-up, delay and no-trace tests in `deep_filter.rs`). None of them is timed. ## Verification - The `denoise` tests passed 40 runs out of 40 on Linux, with 4 busy CPU loops running alongside. - `FLUXER_SCREENSHOT_TESTS=1 cargo xtask ci` (under `dbus-run-session`): all steps passed. - `flaxative --check-noise-suppression` on Linux: `0 of 300 frames late, white noise lowered by 30.0 dB`. **Not verified on Windows:** the 15.6 ms tick of `recv_timeout` is reasoned from how Windows waits work and from the run 388 symptom, not measured. Whether `Poll` actually holds 8 ms there is also unmeasured; it holds by construction, since it uses no timer. The Windows CI job is the first real run. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
The chain waited for a frame with recv_timeout(8 ms). A timed blocking wait
ends on the system timer tick, 15.6 ms by default on Windows (nothing in the
app raises the resolution), so the engine thread could be held twice the
budget and frames counted as in time that were not; Windows CI run 388 failed
the sustained-slowness test that way. The wait now polls the result channel
against Instant (spin, then yield) and declares a frame late exactly when
Instant passes the deadline, on every system.

The scripted-worker tests no longer time anything: a scripted wait behind a
Wait seam decides which frames are late and only then releases them from the
worker, so every assertion is exact (paths, counts, reasons). Poll itself is
tested on what it guarantees (a frame that is there is taken; late only from
the deadline; the thread ending is seen).

Refs #910

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Author
Owner

Merged into textchat by hand (the merge API returns 405); the commits are in the branch.

Merged into `textchat` by hand (the merge API returns 405); the commits are in the branch.
charles closed this pull request 2026-11-18 20:35:52 +00:00
charles referenced this pull request from a commit 2026-11-18 22:34:35 +00:00

Pull request closed

Sign in to join this conversation.
No description provided.