Public: anyone can read this; the crew can change it.

A `[loop]` stall line names what the serving thread was doing: the route or duty in flight #17982

opened this

Merged 13h 1m ago, live in production

The [loop] stall line now says what the serving thread was running when it stopped answering. Example: ... (seen from outside the thread) in: route GET /:owner/:repo/info/refs; load 1m 4.0.

Notes

opened this

The [loop] stall line now says what the serving thread was running when it stopped answering. Example: ... (seen from outside the thread) in: route GET /:owner/:repo/info/refs; load 1m 4.0. Nothing else is added to the line: no path, query, token or user text.

How it works. A new module, src/kernel/loop_step.ts, keeps one cell in shared memory that the watchdog thread reads without touching the serving thread.

  • Names are stored once. Each step name (route METHOD pattern or duty <name>) is written once into a fixed-size hash table in that same shared memory and gets a number. Route names are written when Router.add registers a route; duty names the first time during() runs them. Names are printable ASCII only and cut at 95 characters. The table holds 4,095 names; the app has about 1,571 routes. If it ever fills, new steps show no name.
  • The per-request cost is one write. A request stores its step number with a single Atomics.exchange: no Map and no allocation on the hot path. Match now carries step.
  • Requests (src/http/server.ts). The step is set right after the route matches and set again after the auth await. The handler's synchronous part also runs inside inStep. The cell is cleared when answer finishes. Clearing it outright (instead of restoring the previous value) is safe: when a request resumes after an await, nothing else can be mid-step on the thread.
  • Duties (src/perf/jobs.ts). during() wraps the job in inStep(duty <name>) and restores the previous value afterwards. The step uses only the job's name, never the hosted child's name.
  • The line. The watchdog worker gets the shared memory in WatchdogStart.steps and puts stepWords(readStep(steps)) at the front of blockedLine's extra.

Tests (added to test/loop-watchdog.test.ts):

  • With a step set, the line names it; with none, the line is unchanged.
  • A matched URL with a token-like segment and a query string reads back as its pattern only.
  • A during duty is named while it runs and cleared when it ends.
  • Non-ASCII names are written with ? and long names are cut.

These tests, the router, module-state, unused-exports, route-timing, job-slices, kernel-boundary, security-floor and server-timing suites, tsc --noEmit and the gate all pass. scripts/fast.ts returned "too broad", so I chose those suites myself; the full suite was not run here.

Not done:

  • Work that resumes after an await outside a request's marked points is not named. For async duties, only the synchronous part is named.
  • The closing ("answered again") line is unchanged.
  • No governor.

No door: this is a log line, not a page, action, API route or MCP tool.

Try: on the preview every page answers as before. When the loop blocks for 3 s, perf/loop-watchdog.log gets a line ending in: route <METHOD> <pattern> or in: duty <name>.

🤖 Generated with Claude Code

Closes #17974

commented

The [loop] stall line now says what the serving thread was running when it stopped answering. Example: ... (seen from outside the thread) in: route GET /:owner/:repo/info/refs; load 1m 4.0. Nothing else is added to the line: no path, query, token or user text.

How it works. A new module, src/kernel/loop_step.ts, keeps one cell in shared memory that the watchdog thread reads without touching the serving thread.

  • Names are stored once. Each step name (route METHOD pattern or duty <name>) is written once into a fixed-size hash table in that same shared memory and gets a number. Route names are written when Router.add registers a route; duty names the first time during() runs them. Names are printable ASCII only and cut at 95 characters. The table holds 4,095 names; the app has about 1,571 routes. If it ever fills, new steps show no name.
  • The per-request cost is one write. A request stores its step number with a single Atomics.exchange: no Map and no allocation on the hot path. Match now carries step.
  • Requests (src/http/server.ts). The step is set right after the route matches and set again after the auth await. The handler's synchronous part also runs inside inStep. The cell is cleared when answer finishes. Clearing it outright (instead of restoring the previous value) is safe: when a request resumes after an await, nothing else can be mid-step on the thread.
  • Duties (src/perf/jobs.ts). during() wraps the job in inStep(duty <name>) and restores the previous value afterwards. The step uses only the job's name, never the hosted child's name.
  • The line. The watchdog worker gets the shared memory in WatchdogStart.steps and puts stepWords(readStep(steps)) at the front of blockedLine's extra.

Tests (added to test/loop-watchdog.test.ts):

  • With a step set, the line names it; with none, the line is unchanged.
  • A matched URL with a token-like segment and a query string reads back as its pattern only.
  • A during duty is named while it runs and cleared when it ends.
  • Non-ASCII names are written with ? and long names are cut.

These tests, the router, module-state, unused-exports, route-timing, job-slices, kernel-boundary, security-floor and server-timing suites, tsc --noEmit and the gate all pass. scripts/fast.ts returned "too broad", so I chose those suites myself; the full suite was not run here.

Not done:

  • Work that resumes after an await outside a request's marked points is not named. For async duties, only the synchronous part is named.
  • The closing ("answered again") line is unchanged.
  • No governor.

No door: this is a log line, not a page, action, API route or MCP tool.

Try: on the preview every page answers as before. When the loop blocks for 3 s, perf/loop-watchdog.log gets a line ending in: route <METHOD> <pattern> or in: duty <name>.

🤖 Generated with Claude Code

commented

When the event loop stalls, the [loop] warning now ends with what the serving thread was running: in: route GET /:owner/:repo/info/refs for a request, or in: duty maintain for a background job (#17974). The name is always a route pattern or a job name, never a path, query or token, and only printable ASCII. The watchdog reads it from shared memory on its own thread.

This round fixes the three points from review:

  • Only names what is known for sure. A step is named only while its synchronous code runs, through inStep in src/kernel/loop_step.ts. The cell is set back by compare-exchange the moment that code returns or throws. The server wraps the synchronous part of the auth read and of the handler. The unconditional enterStep(0) in answer() is gone, and so are the calls that re-entered a step after an await. The cost is that a stall in an async handler's or duty's code after an await shows no in: at all. That is now stated in the module doc. It is never blamed on another request.
  • Duty ids are interned once. Each duty's id is worked out once per name and kept on the job watch (JobWatch.stepOf), so during() no longer builds and hashes a string on every call. It still creates one small function per call.
  • Long names are no longer cut. A slot now holds 127 bytes, and a longer name gets no step rather than a shortened one, so two names with the same prefix can't collide. The longest of the 1,571 route names is 82 bytes, so every route is named today. The table holds 4,095 names and still returns 0 (no name) when full. That is documented rather than changed, because it is well above what exists.

Tests:

  • test/loop-step-server.test.ts (new): sends a request with a session cookie through forgeHttp. It checks that the auth read and the handler see route GET /:owner, that the query's token never shows up, and that the cell is empty afterwards. It fails when the server passes step 0 instead of the route's step.
  • Real watchdog: the existing test now blocks the thread inside during("watchdog-block", …) and expects in: duty watchdog-block; in the line the worker writes.
  • Unit tests check that the code after an await reads as unnamed and not as the other request that ran in between, that duty ids are interned, and that long names are kept whole or refused.

bun x tsc --noEmit and bun scripts/gate.ts pass. These test files pass: loop-step-server, loop-watchdog, perf-loop-jobs, perf-loop-lag-attribution, stall-witness, router, keys-capacity, module-state and unused-exports. The full suite was not run here; the forge's check runs it on push.

No door: this is a log line, and no page or terminal screen shows it.

🤖 Generated with Claude Code

Sign in to join this conversation.

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs