Files
deepseek-harness/packages/code-runtime/code-runtime-python/tests/boot-write-failure.spec.ts
T
Chinesezjc 8093d22164 fix(code-runtime-python): bound stray capture by serialized cost, chunk-scan, and flush on destroy
The line-aggregating stray capture from the previous round regressed three
ways the review caught. Rewrite it on the fd-3 reader's raw-Buffer-chunk
shape: accumulate chunks with a byte counter and split on the raw 0x0a byte,
so a large newline-free write no longer re-copies the residual and re-scans
from index 0 per chunk (both O(N^2)). Meter each admitted entry by serialized
cost through a new jsonStringCostUpTo that walks to the cap and stops, so a
near-budget control-char-dense line never allocates the sixfold-inflated
JSON.stringify result the old ledger did (the critical: ~1.6 GiB transient
under a large maxLogBytes). Flush the residual explicitly in the closeDeadline
handler before it destroys the streams, so a setsid escapee's path (which
fires no end) does not drop a leader's final newline-free diagnostic.

Harden the sync-spawn leak assertion to a set difference against a pre-run
snapshot, immune to a parallel worker's concurrent tmpdir create/delete.

Decline the round-2 request to enforce the fd-3 ceiling per-frame: the counter
check must precede Buffer.concat to prevent ~2x memory doubling (two
regression tests assert this), and the batch-edge false reject it would fix is
reachable only at a maxLogBytes/maxValueBytes configured within one pipe read
of the 256 MiB ceiling, far past the defaults. Documented at the check and in
the note Alternatives.

Add flood, NUL-flood, short-escape, and closeDeadline-flush regression tests
(restoring per-file 100% coverage); update the Agent Note and zh pair.
2026-08-31 14:21:57 +08:00

98 lines
4.9 KiB
TypeScript

import { EventEmitter } from 'node:events'
import { readdirSync } from 'node:fs'
import { PassThrough } from 'node:stream'
import { tmpdir } from 'node:os'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { Context } from 'cordis'
/**
* A synchronous `proto.write` throw on the fd-3 pipe is the one boot path a real
* subprocess cannot be coerced into from a test: the pipe accepts queued bytes
* until the kernel buffer fills, and a same-tick EPIPE needs fd 3 already closed
* before the first write. `spawn` is mocked so fd 3 throws on the boot frame,
* which is exactly the branch that regressed. The mock is confined to this file
* so the real-subprocess suite in runtime.spec.ts is untouched.
*/
const { spawnMock } = vi.hoisted(() => ({ spawnMock: vi.fn() }))
vi.mock('node:child_process', async importOriginal => ({
...(await importOriginal<typeof import('node:child_process')>()),
spawn: spawnMock,
}))
const { PythonCodeRuntime } = await import('../src/index.ts')
/** A `child_process.ChildProcess` stand-in whose fd-3 pipe rejects every write. */
function fakeChildWithThrowingFd3(): EventEmitter {
const child = new EventEmitter() as EventEmitter & {
pid?: number
stdout: PassThrough
stderr: PassThrough
stdio: unknown[]
}
// Leave `pid` absent: `finish()` still runs its `clearTimeout(wallTimer)` /
// `removeEventListener(onAbort)` prologue (the TDZ site) before short-
// circuiting on `child.pid === undefined` to `settle` instead of waiting on a
// `close` this fake never emits, so the run resolves promptly.
child.stdout = new PassThrough()
child.stderr = new PassThrough()
// A duplex whose `write` throws synchronously, standing in for an fd-3 pipe
// that fails the moment the boot frame is issued.
const proto = new PassThrough()
proto.write = () => { throw Object.assign(new Error('EPIPE: broken pipe, write'), { code: 'EPIPE' }) }
child.stdio = [new PassThrough(), child.stdout, child.stderr, proto]
return child
}
afterEach(() => {
spawnMock.mockReset()
})
describe('PythonCodeRuntime — boot-write failure', () => {
it('resolves a worker-exit when the fd-3 boot write throws (no TDZ ReferenceError)', async () => {
// Before the fix, the boot-write block ran BEFORE `wallTimer`, `onAbort`,
// and `live` were initialized, so its `finish()` (which clears `wallTimer`,
// removes `onAbort`, and — through `settle` — deletes `live`) hit the
// temporal dead zone and threw a ReferenceError. That escaped the Promise
// executor and REJECTED run() instead of resolving the worker-exit the catch
// constructs. This test would see that rejection; the fix makes it resolve.
spawnMock.mockImplementation(() => fakeChildWithThrowingFd3())
const ctx = new Context()
const fiber = await ctx.plugin(PythonCodeRuntime)
const runtime = ctx.codeRuntime as InstanceType<typeof PythonCodeRuntime>
const result = await runtime.run({ program: 'return 1', bindings: [] })
expect(result.error?.kind).toBe('worker-exit')
expect(result.error?.message).toContain('failed to boot python subprocess')
await fiber.dispose()
})
it('resolves a worker-exit and removes the staging dir when spawn throws synchronously', async () => {
// `spawn` can throw same-tick — EMFILE on a descriptor-exhausted host, or a
// libuv-level failure — before the Promise executor and its settlement path
// exist. Left uncaught it rejected run() (the seam permits rejection only for
// misuse) and stranded the staging directory materializePyScripts had just
// written, which only settle() removes. The fix catches it, unlinks the
// directory, and resolves the same `worker-exit` class as an async ENOENT.
// Snapshot as a SET, then assert no dir NEW relative to it survives. Strict
// array equality would flake: vitest's forks pool runs runtime.spec.ts in a
// sibling worker that concurrently creates and removes
// `dsh-code-runtime-python-*` dirs, so a concurrent create OR delete in the
// window would fail `toEqual`. The set difference is immune to both — it
// only asserts THIS run left nothing behind.
const before = new Set(readdirSync(tmpdir()).filter(name => name.startsWith('dsh-code-runtime-python-')))
spawnMock.mockImplementation(() => { throw Object.assign(new Error('EMFILE: too many open files'), { code: 'EMFILE' }) })
const ctx = new Context()
const fiber = await ctx.plugin(PythonCodeRuntime)
const runtime = ctx.codeRuntime as InstanceType<typeof PythonCodeRuntime>
const result = await runtime.run({ program: 'return 1', bindings: [] })
expect(result.error?.kind).toBe('worker-exit')
expect(result.error?.message).toContain('python spawn error')
const leaked = readdirSync(tmpdir()).filter(name => name.startsWith('dsh-code-runtime-python-') && !before.has(name))
expect(leaked).toEqual([])
await fiber.dispose()
})
})