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

Sentinel: memory-growth was degraded 33 minutes today: The forge's own memory is 4600 MiB, over twice its first-hour 1962 MiB and past the 2176 MiB more its database caches m… #17159

opened this

Merged 37h 15m ago, live in production

SQL statements pushed out of a connection's statement cache are now finalized rather than left for the garbage collector (#17130). I can't read production's data from here, so this is the most likely cause, not one confirmed there. Why this looks like the cause.

Notes

opened this

SQL statements pushed out of a connection's statement cache are now finalized rather than left for the garbage collector (#17130). I can't read production's data from here, so this is the most likely cause, not one confirmed there.

Why this looks like the cause. On #17130, statements kept sat at 1024, the cache's limit, from the trip through to now, while memory outside every JS heap grew from 1281 to 3025 MiB. The forge has more than 1,024 distinct fixed SQL texts, so the main connection's cache keeps replacing statements. Since #16942, a statement it pushed out was only dropped, never finalized. Its compiled program stays in malloc until a full collection, and a heap of a GiB or more rarely runs one. I measured it with the real schema and about 700 MiB of live heap: cycling 60,000 texts through the cache grew RSS from 1017 to 1947 MiB. With finalization on eviction, RSS stayed between 1014 and 1043 MiB.

What changed

  • src/kernel/list_texts.ts: each connection keeps its last 64 pushed-out statements (RETIRING_KEPT) and finalizes the oldest beyond that. A caller still stepping one in the same turn, such as an iterate whose loop body runs other queries, finishes first. close finalizes whatever is left. This needs no timer, so the timer allow-list in test/timers-named.test.ts is untouched.
  • src/flows/radar.ts: this was the one place in src/ that held a statement across awaits (the registry lookups). It now calls db.query again at each write.
  • docs/features/memory-growth.md: describes the mechanism, and corrects the line saying a count stuck at 1,024 means "the growth is elsewhere". It now means the cache is turning over.

Tests

  • Three new tests in test/fixed-texts.test.ts:
    • A pushed-out statement still answers while it is among the last 64.
    • One more eviction finalizes it. Without this change it is never finalized, so this test fails.
    • List texts behave the same way, and close finalizes the waiting statements, so the -wal and -shm files go with it.
  • Two existing statement-count limits now allow for the 64 statements waiting to be finalized, because they really are still prepared:
    • test/fixed-texts.test.ts: FIXED_TEXTS_KEPT + RETIRING_KEPT + 50
    • test/pages-memory.test.ts: LIST_TEXTS_KEPT + RETIRING_KEPT + 20

How I checked held statements

  • I ran the whole suite with the caches cut to 4 fixed and 2 list texts, so statements were evicted and finalized constantly. The only use of a finalized statement was in a test file (test/changelog-api.test.ts, which holds one across awaits). That is safe at the real limit of 1,024, so I left it alone.
  • A scan of src/ found only the radar case.
  • Every other failure in that run also fails on main in this sandbox (Chromium, cells, boot timeouts) or passes when run alone.

Not done

  • I didn't raise FIXED_TEXTS_KEPT. With finalization, turnover costs a re-prepare but leaves no memory behind.
  • I didn't add a counter of evictions per minute.

bun x tsc --noEmit and bun scripts/gate.ts pass. bun scripts/fast.ts said the change is too broad to pick tests from, so beyond the full run above I ran the memory, statement-cache, radar and timer suites at the real limits: 82 pass, 0 fail.

To try it: bun test test/fixed-texts.test.ts test/pages-memory.test.ts. On production, memory outside every heap should level off while statements kept stays at 1024, and memory-growth should stay out of the degraded list for a day.

🤖 Generated with Claude Code

Closes #17130

commented

SQL statements pushed out of a connection's statement cache are now finalized rather than left for the garbage collector (#17130). I can't read production's data from here, so this is the most likely cause, not one confirmed there.

Why this looks like the cause. On #17130, statements kept sat at 1024, the cache's limit, from the trip through to now, while memory outside every JS heap grew from 1281 to 3025 MiB. The forge has more than 1,024 distinct fixed SQL texts, so the main connection's cache keeps replacing statements. Since #16942, a statement it pushed out was only dropped, never finalized. Its compiled program stays in malloc until a full collection, and a heap of a GiB or more rarely runs one. I measured it with the real schema and about 700 MiB of live heap: cycling 60,000 texts through the cache grew RSS from 1017 to 1947 MiB. With finalization on eviction, RSS stayed between 1014 and 1043 MiB.

What changed

  • src/kernel/list_texts.ts: each connection keeps its last 64 pushed-out statements (RETIRING_KEPT) and finalizes the oldest beyond that. A caller still stepping one in the same turn, such as an iterate whose loop body runs other queries, finishes first. close finalizes whatever is left. This needs no timer, so the timer allow-list in test/timers-named.test.ts is untouched.
  • src/flows/radar.ts: this was the one place in src/ that held a statement across awaits (the registry lookups). It now calls db.query again at each write.
  • docs/features/memory-growth.md: describes the mechanism, and corrects the line saying a count stuck at 1,024 means "the growth is elsewhere". It now means the cache is turning over.

Tests

  • Three new tests in test/fixed-texts.test.ts:
    • A pushed-out statement still answers while it is among the last 64.
    • One more eviction finalizes it. Without this change it is never finalized, so this test fails.
    • List texts behave the same way, and close finalizes the waiting statements, so the -wal and -shm files go with it.
  • Two existing statement-count limits now allow for the 64 statements waiting to be finalized, because they really are still prepared:
    • test/fixed-texts.test.ts: FIXED_TEXTS_KEPT + RETIRING_KEPT + 50
    • test/pages-memory.test.ts: LIST_TEXTS_KEPT + RETIRING_KEPT + 20

How I checked held statements

  • I ran the whole suite with the caches cut to 4 fixed and 2 list texts, so statements were evicted and finalized constantly. The only use of a finalized statement was in a test file (test/changelog-api.test.ts, which holds one across awaits). That is safe at the real limit of 1,024, so I left it alone.
  • A scan of src/ found only the radar case.
  • Every other failure in that run also fails on main in this sandbox (Chromium, cells, boot timeouts) or passes when run alone.

Not done

  • I didn't raise FIXED_TEXTS_KEPT. With finalization, turnover costs a re-prepare but leaves no memory behind.
  • I didn't add a counter of evictions per minute.

bun x tsc --noEmit and bun scripts/gate.ts pass. bun scripts/fast.ts said the change is too broad to pick tests from, so beyond the full run above I ran the memory, statement-cache, radar and timer suites at the real limits: 82 pass, 0 fail.

To try it: bun test test/fixed-texts.test.ts test/pages-memory.test.ts. On production, memory outside every heap should level off while statements kept stays at 1024, and memory-growth should stay out of the degraded list for a day.

🤖 Generated with Claude Code

Sign in to join this conversation.

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

Keyboard shortcuts

Docs