Files
parking_solution/wiki/concepts/park-2-event-loop-stall.md
T
julian 580412aa50 wiki: passage-confirmation design and stage-73ada89 deploy notes (2026-10-24)
Wiki pages written on 2026-10-24 but left uncommitted before the Windows to
Linux migration: passage-confirmation concept, open question 20, deploy and
query log entries, index line, incident and barrier page updates. Drops the
stray tracked __pycache__ file. Line endings normalised to LF.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-11-07 09:38:32 +01:00

92 lines
5.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
type: concept
tags: [parking, performance, incident, booth, relay, sqlite]
sources: []
updated: 2026-10-24
---
# park-2 event-loop stall → phantom relay timeouts (2026-10-24)
An incident analysis with a durable rule: **request cost must be bounded whatever the operator's
data volume**. Found while answering "why do some of a subscriber's entries have no snapshot".
## Symptom chain (as observed on park-2, stage-9659373)
1. Four signed `vehicle_entry` events for one subscriber had **no snapshot and no `sessions` row**.
2. Each coincided with `relay pulse FAILED … timeout` on the [[dingtian-relay]] and
`subscription-flow failed: timeout`. The subscription flow signed the ledger, then awaited the
pulse; the pulse helper rethrows, so the method unwound **before** firing the camera and
writing the session cache (see [[subscription]], `subscription-flow.ts`).
3. The relay was healthy: 60/60 UDP status probes from a separate process answered in 48–52 ms;
ping < 1 ms, no loss, link clean.
4. The server process was the problem. `GET /api/sessions/active` ran **~2 s, synchronously**,
2234× in 24 h (the booth polls it). `PayStation.activeSessions()` called `quote()` **per open
unpaid transient** (664 of them), each doing 3–4 **unindexed** `ledger_events` scans by
identity. Every other request queued behind it: reader pushes at 2 s, vision calls aborting,
forgive at 2 s.
5. Node runs **expired timers before pending socket I/O**. A pulse whose reply arrived during the
stall saw its 2 s timer fire first → "timeout" for a relay that had answered (and fired).
`tookMs` of the failures were 2000–2260 ms — the 2 s timer plus loop lag.
6. Growth: open unpaid transients +50–65/day since 2026-10-09 → ~120 ms/day added to the handler.
Successful-pulse median tracked it exactly (109 ms on 10-11 → 1330 ms on 10-21; p95 2290 ms on
10-24). The curve crossed the 2 s timeout around 10-19/10-22 — when the failures began.
## Why this is OUR defect (not the operator's)
The operator's choices (an unlimited card used 70×/day by a booth operator, 664 tickets that
never exited through the system) are **theirs to make**. Our defect is any code path whose cost
grows with their data. The user's stance, verbatim: "We are not running the business, we are
providing the software." Do not propose cleaning up or deciding about operator records.
## The fix (stage, uncommitted at time of writing)
- `activeSessions()` prices every row from **one** whole-ledger scan (per-identity grouping fed to
a rows-based `#quoteFromRows`) and builds the tariff resolver once. `quote(identity)` wraps the
same core with one identity-scoped read, so both paths price identically (pinned by
`pay-station.test.ts`). `validations.ts` gained `sessionValidationsFromRows`/
`liveValidationsFromRows`.
- Migration `0032_hot_path_indexes`: `ledger_events(identity, index)`,
`device_events(category, kind, occurred_at)`, `device_events(kind, occurred_at)`,
`snapshots(identity)`, `sessions(state)`, `sessions(subscription_id, state)`. Additive; the
previous image ignores them on rollback. The snapshots index costs one identity-column pass
(~50 ms measured, the blobs live in overflow pages).
- Dingtian `binaryUdp`: the timeout rejection is deferred with `setImmediate`, so a datagram that
already arrived wins over an expired timer (check phase runs after poll).
- Subscription flow: snapshot fired **before** the pulse; session cache written/closed **before**
the pulse. Evidence and the projection no longer depend on the relay call succeeding.
Expected effect on park-2's data: the handler drops from ~2 s to ~0.1–0.15 s (ledger scan 36 ms +
plate scan 78 ms, both now index-assisted), and stops growing with open-ticket count.
## The rule going forward
- **No per-row ledger reads in list views.** Fold once, price from the fold.
- **Every identity/telemetry lookup is indexed.** `ledger_events.identity`, `device_events`
(category, kind, time), `snapshots.identity`.
- **A device timeout must not race the event loop.** Defer timeout rejections one turn.
- **Evidence and projections before the pulse.** The signed event is the decision; the pulse is a
side effect that may fail.
- Watch `tookMs` on `kind:"relay"` [[device-events]]: a rising median with a healthy device is an
event-loop stall, not a network problem.
## Open follow-ups
- `GET /api/snapshots/by-identity` still loads **all** camera snapshot/read telemetry rows and
filters in JS (45 calls/day, ~50 ms, grows with telemetry). Filter by identity in SQL.
- Other `.all()` scans over `device_events`/`ledger_events` in list routes (feed, lane status)
deserve the same bounded-work review.
- Deploy: build the stage image, bump park-2's TAG (currently stage-9659373; the four commits
since touch no app code), watch the pulse `tookMs` and `/api/sessions/active` durations.
Related: [[booth-exit-flow]] (active list semantics), [[append-only-event-chain]],
[[device-events]], [[dingtian-relay]], [[appliance-provisioning]] (park-2 host history).
## Deployed and measured (2026-10-24, stage-73ada89 on park-lab then park-2)
Migration 0032 applied at container start (both sites); all six indexes present and chosen by the
planner. On park-2, same hour, same traffic: `GET /api/sessions/active` **~2.0–2.6 s → 290–390 ms**;
no other request above 300 ms; first subscription relay pulse **46 ms** (the old container had
logged 2.2 s pulses minutes earlier). The remaining ~300 ms is the whole-ledger fold + the plate
telemetry scan — index-assisted, growing slowly with total history, no longer with open tickets.
Next step for a flat handler: cache the fold keyed on the ledger head `index`.