fix(serve): release memos when the confirming collection fails

A collector that threw made the pressure check reject, which failed the
request instead of shedding memory. A failed collection now keeps the first
heap verdict and releases. A test also pins that the resident check goes
through the confirming helper.
This commit is contained in:
Resham Joshi 2026-11-06 19:15:06 -08:00
parent e26de634f1
commit d5c4a8eb93
2 changed files with 23 additions and 2 deletions

View file

@ -95,7 +95,9 @@ export function serveMemoryPressure(
/// serveMemoryPressure, with a heap verdict confirmed after a collection: used
/// heap counts unreachable objects until the next one, and right after a large
/// request that garbage alone can cross the line and drop a healthy child's
/// memos. Returns the reading the verdict rests on.
/// memos. A collection that fails keeps the first verdict, so the memos are
/// released rather than the request failed. Returns the reading the verdict
/// rests on.
export async function serveMemoryPressureAfterCollect(
read: () => ServeMemoryReading,
lastRssClear: number,
@ -105,7 +107,11 @@ export async function serveMemoryPressureAfterCollect(
let reading = read()
let pressure = serveMemoryPressure(reading, lastRssClear, heapFraction)
if (pressure === 'heap') {
await collect()
try {
await collect()
} catch {
return { pressure, reading }
}
reading = read()
pressure = serveMemoryPressure(reading, lastRssClear, heapFraction)
}

View file

@ -1,5 +1,6 @@
import { describe, it, expect, beforeAll, afterAll } from 'vitest'
import { spawn, type ChildProcess } from 'child_process'
import { readFileSync } from 'fs'
import { appendFile, mkdir, mkdtemp, readFile, rm, writeFile } from 'fs/promises'
import { tmpdir } from 'os'
import { join } from 'path'
@ -64,6 +65,20 @@ it('collects garbage before a heap reading drops the memos', async () => {
expect(await run([3.8 * GB, 3.5 * GB])).toEqual({ pressure: 'heap', collected: 1 })
expect(await run([1 * GB])).toEqual({ pressure: null, collected: 0 })
expect(await run([1 * GB], 7 * GB)).toEqual({ pressure: 'rss', collected: 0 })
// A collector that fails keeps the first verdict: release, never fail the request.
const failing = await serveMemoryPressureAfterCollect(() => ({ usedHeap: 3.8 * GB, heapLimit: 4.2 * GB, rss: 3 * GB }), 0, 0.7, async () => { throw new Error('no gc') })
expect(failing.pressure).toBe('heap')
})
it('the resident pressure check confirms a heap reading after a collection', () => {
// Structural pin: the guard inside runStdioServe cannot be driven to a real
// near-limit heap in a test, so pin that it goes through the confirming helper.
const source = readFileSync(join(__dirname, '..', 'src', 'serve.ts'), 'utf-8')
const start = source.indexOf('const shedIfPressured = ')
const body = source.slice(start, source.indexOf('\n }\n', start))
expect(start).toBeGreaterThan(-1)
expect(body).toContain('serveMemoryPressureAfterCollect(readMemory,')
expect(body).not.toMatch(/serveMemoryPressure\(/)
})
describe('day-scoped invalidation', () => {