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

The access log's rotate and prune leave the serving thread #17901

opened this

Merged 26h 27m ago, live in production

Before this change, pruning the access log and shedding a full day's file ran on the serving thread inside the flush. They now run as a background job (access-keep) that hands the thread back between file calls.

Notes

opened this

Before this change, pruning the access log and shedding a full day's file ran on the serving thread inside the flush. They now run as a background job (access-keep) that hands the thread back between file calls. The append in flushAccess stays synchronous and unchanged; the function now returns a promise that settles when its keeping work is done, and it never rejects.

  • prune is now async. It makes one file call per tick: the readdirSync, then each unlinkSync or statSync. Forty day files take dozens of short ticks instead of one long one. A file another process already deleted counts as done.
  • Shedding copies the newest half of the file into <file>.tmp, 1 MB per tick. The tick that reaches the end of the file copies the rest and renames over the original without yielding. Lines this process appends during the copy go onto the old file and are carried over, and since nothing can be appended between that last copy and the rename, no line is lost or written twice. As before, a line another process appends in that same instant can still be lost.
  • Ordering: each flush's work queues behind the job already running instead of being skipped. A shed is queued at most once per file, so two jobs never cut the same file.

Tests (test/access-log.test.ts):

  • 40 day files plus a full 16 MB current file: the file calls are counted between pauses. No tick makes more than 6, over more than 40 ticks. Before the change, a single tick made about 60 calls, including a 16 MB read and an 8 MB write. The test also checks that 14 files are left and the shed file starts on a whole line.
  • Lines during a shed: a line is recorded and flushed at every pause of the shed. The file ends up with each line exactly once, in order.

Both new tests fail against the old code. I changed three existing tests from that file because they assumed keeping finished inside the flush; they now await prune and flushAccess. Those tests were wrong under the new behaviour, not the code.

Not done: I didn't move this to a worker thread. Short slices already meet the bound, and a worker would need its own file locking. I also didn't change how readers or the settings page read the log.

Checks: the gate passes, apart from a note about one comment line over 100 columns that the nightly rewrap handles. bun x tsc --noEmit passes. I ran the access log, vitals, tui-access, client-abort, spawn-host, timers-named, kernel-boundary, test-honesty, boot-no-stall, perf-loop-jobs and unused-exports suites: all pass. scripts/fast.ts named 2,818 files, too many to run here, so I picked these; the forge's check runs the full suite on the push.

Try it: send a few requests to the preview, then open /settings/access; the lines still show.

No door: no page, API route or tool changes; the log's readers get the same lines.

🤖 Generated with Claude Code

Closes #17884

commented

Before this change, pruning the access log and shedding a full day's file ran on the serving thread inside the flush. They now run as a background job (access-keep) that hands the thread back between file calls. The append in flushAccess stays synchronous and unchanged; the function now returns a promise that settles when its keeping work is done, and it never rejects.

  • prune is now async. It makes one file call per tick: the readdirSync, then each unlinkSync or statSync. Forty day files take dozens of short ticks instead of one long one. A file another process already deleted counts as done.
  • Shedding copies the newest half of the file into <file>.tmp, 1 MB per tick. The tick that reaches the end of the file copies the rest and renames over the original without yielding. Lines this process appends during the copy go onto the old file and are carried over, and since nothing can be appended between that last copy and the rename, no line is lost or written twice. As before, a line another process appends in that same instant can still be lost.
  • Ordering: each flush's work queues behind the job already running instead of being skipped. A shed is queued at most once per file, so two jobs never cut the same file.

Tests (test/access-log.test.ts):

  • 40 day files plus a full 16 MB current file: the file calls are counted between pauses. No tick makes more than 6, over more than 40 ticks. Before the change, a single tick made about 60 calls, including a 16 MB read and an 8 MB write. The test also checks that 14 files are left and the shed file starts on a whole line.
  • Lines during a shed: a line is recorded and flushed at every pause of the shed. The file ends up with each line exactly once, in order.

Both new tests fail against the old code. I changed three existing tests from that file because they assumed keeping finished inside the flush; they now await prune and flushAccess. Those tests were wrong under the new behaviour, not the code.

Not done: I didn't move this to a worker thread. Short slices already meet the bound, and a worker would need its own file locking. I also didn't change how readers or the settings page read the log.

Checks: the gate passes, apart from a note about one comment line over 100 columns that the nightly rewrap handles. bun x tsc --noEmit passes. I ran the access log, vitals, tui-access, client-abort, spawn-host, timers-named, kernel-boundary, test-honesty, boot-no-stall, perf-loop-jobs and unused-exports suites: all pass. scripts/fast.ts named 2,818 files, too many to run here, so I picked these; the forge's check runs the full suite on the push.

Try it: send a few requests to the preview, then open /settings/access; the lines still show.

No door: no page, API route or tool changes; the log's readers get the same lines.

🤖 Generated with Claude Code

commented

The access log no longer prunes or sheds on the serving thread. That work now runs in a background job (access-keep) that makes a few file calls and then yields with breathe(). The append in flushAccess is unchanged and still synchronous. flushAccess now returns a promise that settles when the work it queued is done, and it never rejects.

  • Prune (src/http/access_log.ts): it is now async and makes one file call per tick: the readdirSync, then each unlinkSync or statSync. A file another process already deleted counts as done.
  • Shed: the newest half of a full file is copied into <file>.tmp, 1 MB per tick. The tick that reaches the end copies the rest and renames over the file without yielding. Lines appended meanwhile land on the old file and are carried over, so none is lost or written twice. A line another process appends in that same instant can still be lost, as before.
  • Ordering: each flush's work queues behind the job already running, so two jobs never cut one file. A shed first re-reads the file's size and stops if an earlier job already cut it, so no module-level set is needed.

Tests:

  • New, in test/access-log.test.ts: 40 day files plus a full 16 MB current file are handled in more than 40 ticks of at most 6 file calls each. Before, one tick made about 60 calls, including a 16 MB read. Separately, lines recorded at every pause of a shed end up in the file once each, in order. Both tests fail against the old code.
  • Changed: three existing tests now await prune and flushAccess, because they assumed the work finished inside the flush. The tests were wrong under the new behaviour, not the code.
  • Rule tests: access-keep is named in UNTIMED in test/job-slices.test.ts with its reason. The access log's row in test/request-path-sync.test.ts drops writeFileSync, since the shed no longer calls it; statSync stays at 4, with reasons updated. That test arrived on main after I branched, so I merged origin/main to get it.

Passing here: the gate, bun x tsc --noEmit, and the three rule tests that failed the check. Also passing: the access-log, timers-named, unused-exports, vitals, tui-access and test-honesty suites, with access-log green three runs in a row. scripts/fast.ts named 2,818 files, so I picked these; the forge runs the full suite on this push.

Not done: no worker thread, since short ticks already keep each step to a few calls. No change to how readers or the settings page read the log.

Try: send a few requests to the preview, then open /settings/access; the lines still show.

No door: no page, API route or tool changes.

🤖 Generated with Claude Code

Sign in to join this conversation.

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

Keyboard shortcuts

Docs