| @@ -0,0 +1,109 @@ |
| 1 | +# ADR 0055: Opt-in virtual time with sockets (`LOUISE_VIRTUAL_TIME`) |
| 2 | + |
| 3 | +**Status:** Proposed (2026-10-22); needs engineering review (#44). |
| 4 | + |
| 5 | +## Context |
| 6 | + |
| 7 | +- [ADR 0004](0004-actor-model.md) makes time virtual — when no actor can |
| 8 | + run, the clock jumps to the next deadline — until the program opens its |
| 9 | + first socket, which switches the clock to real time for good (Revision |
| 10 | + 3). The rationale stands for production: a program talking to the |
| 11 | + outside world cannot be replayed, and its timeouts (an idle |
| 12 | + connection's) must expire even while other actors keep every thread |
| 13 | + busy. |
| 14 | +- That switch makes watchdog tests wall-clock tests. |
| 15 | + `http_body_limit_and_write_timeout` (`crates/louise/tests/cli.rs`, |
| 16 | + issue #293) checked that a client that stops reading a 32 MiB response |
| 17 | + is disconnected after the server's 300 ms write timeout: it slept a |
| 18 | + fixed 1000 ms *assuming* the server's write had blocked by then, and |
| 19 | + asserted the close happened within 2 s. Under machine load the write |
| 20 | + had not always blocked after 1000 ms, and the 300 ms timeout against a |
| 21 | + 2 s bound was at the mercy of scheduling. The test failed |
| 22 | + intermittently in CI and passed standalone. |
| 23 | +- All the margins were in the test; `std/http`'s watchdog has none. What |
| 24 | + the test needs is determinism: the timeout must fire when the blocked |
| 25 | + write is the only thing left, however loaded the machine is. |
| 26 | + |
| 27 | +## Decision |
| 28 | + |
| 29 | +An explicit testing knob, **`LOUISE_VIRTUAL_TIME=1`**, read once when the |
| 30 | +scheduler starts (`Shared::new`, like `LOUISE_PARKED_EPOCH_MS`). With it: |
| 31 | + |
| 32 | +1. **The one-way switch to real time is disabled.** `io_token()` does not |
| 33 | + set `real_start`; the clock stays virtual despite open sockets. |
| 34 | +2. **The idle jump's gate is relaxed.** When no actor can run and no |
| 35 | + blocking FFI call is in flight, the idle thread first polls the |
| 36 | + reactor with a *zero* timeout. If no socket is ready, jumping is safe |
| 37 | + — socket readiness does not depend on the clock — so the clock jumps |
| 38 | + to the next timer deadline and fires it, **even while actors are |
| 39 | + parked on sockets**. If a socket is ready, its events are delivered |
| 40 | + and there is no jump. |
| 41 | +3. **The poll never sleeps waiting for a virtual deadline** — the clock |
| 42 | + jumps instead. With no timers pending either, the thread falls through |
| 43 | + to the existing park-on-the-reactor path, so deadlock detection works |
| 44 | + exactly as today (actors parked on sockets are still no deadlock). |
| 45 | +4. **The default is unchanged.** With the variable unset, not one |
| 46 | + behaviour differs: the first socket still switches to real time, and |
| 47 | + the jump still requires that nobody waits on a socket. |
| 48 | + |
| 49 | +The effect on the migrated test: the server's 32 MiB write blocks (the |
| 50 | +client is not reading), the connection actor parks on writable, the |
| 51 | +watchdog parks on its 300 ms timer, and the scheduler is otherwise idle; |
| 52 | +the zero-time poll finds the socket not writable, the clock jumps, the |
| 53 | +watchdog fires and closes the connection. Deterministic under any load, |
| 54 | +and fast: the fixture's 4 s shutdown sleep jumps too, so the test runs in |
| 55 | +milliseconds. The test drops its fixed sleep and wall-clock assertions |
| 56 | +for a deterministic protocol: the client sends `GET /big` and then waits |
| 57 | +for the server's `done` line *without reading* (reading would drain the |
| 58 | +buffers and unblock the write — a race the 50-run soak caught); `done` is |
| 59 | +printed only after the fourth connection — this one — has ended, so the |
| 60 | +close has already happened when the client reads what little arrived |
| 61 | +(`< 32 MiB`), and the server exits 0 with no leaks. A 10 s read timeout |
| 62 | +and a 15 s kill watchdog remain as backstops so a regression cannot wedge |
| 63 | +CI. The fixture disables its *idle* timeout (which the test does not |
| 64 | +exercise): virtual time would fire it the moment the server goes idle, |
| 65 | +while a loaded client's request is still in flight — the soak caught |
| 66 | +exactly that too, before the change. |
| 67 | + |
| 68 | +## Consequences |
| 69 | + |
| 70 | +- **Opt-in only, for tests.** A program run with the variable set can |
| 71 | + observe timeouts expiring while a peer's bytes are still in flight: the |
| 72 | + jump fires a due timer the moment no socket is *ready*, which a |
| 73 | + real-time run would reach only after the timeout's worth of wall clock. |
| 74 | + That is precisely what the watchdog test wants, and precisely why the |
| 75 | + variable must never be set in production — ADR 0004 Revision 3's |
| 76 | + rationale is untouched. |
| 77 | +- **Virtual-time programs without sockets are unaffected**; the new gate |
| 78 | + only widens the jump condition when socket waiters exist, and only |
| 79 | + under the variable. |
| 80 | +- **The interpreter is untouched** (it has virtual time but no real |
| 81 | + sockets), and the loom models are unchanged: the change adds a branch |
| 82 | + to `idle()`'s decision, not to any hand-off they model. |
| 83 | +- **Runtime tests** (`louise_rt::sched::tests`) pin both sides: with the |
| 84 | + opt-in, a socket does not flip the clock to real time and a timer fires |
| 85 | + via the idle jump while an actor is parked on a socket; without it, the |
| 86 | + first socket still switches to real time. The scheduler's test helper |
| 87 | + sets the flag explicitly so the tests do not depend on the environment. |
| 88 | + |
| 89 | +## Verification |
| 90 | + |
| 91 | +- `cargo test -p louise_rt`: the two new tests, on 1 and 4 scheduler |
| 92 | + threads, plus the unchanged neighbours. |
| 93 | +- The loom models (`handoff`, `timers`, `otp`, `inbox`) still pass. |
| 94 | +- `http_body_limit_and_write_timeout` migrated to the opt-in passes a |
| 95 | + 50-run soak under full CPU load (`yes` on every core); it failed under |
| 96 | + the same load before the migration. |
| 97 | + |
| 98 | +## Alternatives considered |
| 99 | + |
| 100 | +- **Bigger margins in the test** (longer sleep, wider bound). Rejected: |
| 101 | + margins are what made it flaky; no margin is right under enough load. |
| 102 | +- **A test-only build flag.** Rejected: the shipped runtime and the |
| 103 | + tested runtime would differ; an environment variable keeps one binary |
| 104 | + and makes the opt-in visible at the spawn site. |
| 105 | +- **Virtual time whenever every socket waiter also has a timer.** That |
| 106 | + is, jump only if doing so wakes someone. Rejected as a half-measure: |
| 107 | + the watchdog's timer is exactly such a timer, so the special case buys |
| 108 | + nothing over the general rule, and the general rule is simpler to |
| 109 | + state: jump when nothing is runnable and no socket is ready. |