Skip to content

JavaScript deadlines without a runtime-wide interrupt cost

Accepted and implemented in the Go runner (wazero engine).

2026-10-20.

Every function has a deadline (timeoutMs). Something must stop a guest that runs past it. On wazero there is exactly one way to stop a call from outside, and it is a setting on the whole runtime: WithCloseOnContextDone(true) makes the compiler insert a check at every loop header and function entry. It works on any guest, however hostile, and it is expensive: measured at 3.7 to 4.8 times slower on JavaScript compute, and 4 to 10 times on compiled Go and Rust.

The original design paid that cost for every JavaScript function, although JavaScript functions all run on one engine the platform itself ships.

The engine owns two wazero runtimes, split by trust:

Runtime Stops a call by Runs
Preemptible wazero, from outside, at any instrumented point every module an author supplies (the wasm runtime)
Cooperative the guest, from inside, plus a host watchdog only the platform’s own JavaScript engine

Code the platform wrote and controls may rely on itself to stop; code an author compiled may not. This follows from ADR 0030, which makes the sandbox a fault-isolation tool and not a hostile-code boundary.

For the JavaScript engine:

  • It installs a QuickJS interrupt handler. Before each call the host sets a deadline through the engine’s own fc_js_deadline export; the handler compares a monotonic clock with it and aborts. QuickJS polls the handler from its bytecode loop, regexp matcher and long builtins, and the abort cannot be caught by try/catch/finally.
  • The host arms the guest with the remaining time rounded up to whole milliseconds (30 seconds if the context has no deadline, so nothing is unbounded), runs the call on its own goroutine, and waits for it or for a watchdog at deadline plus 2 seconds.
  • If the watchdog fires, the call is abandoned. The caller gets a deadline error (HTTP 504 FUNCTION_TIMEOUT), the instance is marked broken and discarded, and a stuck-call gauge rises.
  • At four simultaneous stuck calls the runner’s health and readiness endpoints answer 503, so the orchestrator replaces the process.

The only way onto the cooperative runtime is a dedicated compile call used for the shared JavaScript engine, and a module that does not export fc_js_deadline is refused there. fc_js_deadline is not part of ABI v1, so no author-facing contract changes.

Good:

  • JavaScript functions are about 3.5 times faster on realistic work, 5 times on the worst case and about 2 times on a trivial call (measured on arm64).
  • A broad set of runaway shapes (tight loops, swallowed aborts, catastrophic regexps, endless promise chains, large BigInt and array work) stop at the deadline in tests.
  • Stuck calls are visible and bounded.

Bad:

  • A client hang-up does not stop a JavaScript call; it runs to its deadline.
  • An abandoned call keeps its thread until the guest returns, if it does.
  • Native builtins that do not poll can overshoot the deadline (the slowest tested case was about 0.8 seconds), which is why the grace period is 2 seconds.
  • There is no per-version quarantine yet: one function that reliably defeats the handler could repeatedly push a process to its restart threshold.
  • Unbounded recursion traps instead of raising a catchable RangeError.
  • The two runtimes share a compilation cache that is safe only because of how wazero keys it; a test guards that assumption.
Alternative Why not
Keep checks on every runtime Simple and robust, but pays up to 5 times on the language most functions use.
Cooperative deadlines for all modules An author-compiled module may not cooperate; it needs outside enforcement.
A different engine with its own interrupt (V8 or wasmtime epochs) Planned for deployed runners; see ADR 0031. This change keeps wazero fast in the meantime.