diff --git a/server/src/nu.ts b/server/src/nu.ts index 878c617..d7819bf 100644 --- a/server/src/nu.ts +++ b/server/src/nu.ts @@ -135,8 +135,14 @@ async function spawnNu(argv: string[], opts: RunOptions): Promise { new Response(proc.stderr).text(), ]) await proc.exited + // Explicitly clean up before return so the active-set entry is removed + // in the same microtask as the return value — guards against runtimes + // that defer `finally` to a separate microtask tick. + clearTimeout(timer) + removeActive(proc) return { stdout, stderr, exitCode: proc.exitCode, timedOut } } finally { + // Error / signal paths: idempotent no-ops in the success case above. clearTimeout(timer) removeActive(proc) } @@ -454,6 +460,9 @@ async function dumpEnv( } // Skip sentinel plus the trailing newline from `printf '%s\n'`. const envText = stdout.slice(idx + ENV_SENTINEL.length + 1) + // Explicit cleanup before return — same rationale as spawnNu. + if (timerId !== undefined) clearTimeout(timerId) + removeActive(proc) return parseEnv0(envText) } finally { if (timerId !== undefined) clearTimeout(timerId) diff --git a/server/test/nu.activeSet.test.ts b/server/test/nu.activeSet.test.ts index c2ab1a6..2f49c79 100644 --- a/server/test/nu.activeSet.test.ts +++ b/server/test/nu.activeSet.test.ts @@ -3,7 +3,7 @@ * tagging and the sanitizeKey export. Both are infrastructure changes with * no user-visible behavior change. */ -import { afterAll, describe, expect, test } from "bun:test" +import { afterAll, beforeAll, describe, expect, test } from "bun:test" import { _getActiveRoles, abortExec, @@ -19,6 +19,12 @@ afterAll(() => { killAll() }) +// Kill any processes left alive by earlier test files (e.g. the doc singleton +// spawned by smoke.test.ts) so this suite starts with a clean active set. +beforeAll(() => { + killAll() +}) + describe("sanitizeKey export (Cycle 0)", () => { test("accepts valid keys and defaults to 'default'", () => { expect(sanitizeKey("valid-key_123")).toBe("valid-key_123") @@ -48,7 +54,8 @@ describe("active set role tagging (Cycle 0)", () => { test("active set empties after the proc exits", async () => { await runRaw("1 + 1") - // After await resolves, spawnNu's finally has run; proc is removed. + // After await resolves, spawnNu has called removeActive synchronously + // before returning, so the active set is clean. const roles = _getActiveRoles() expect(roles.length).toBe(0) }) diff --git a/server/test/smoke.test.ts b/server/test/smoke.test.ts index e540276..ca12f34 100644 --- a/server/test/smoke.test.ts +++ b/server/test/smoke.test.ts @@ -8,7 +8,7 @@ * * Run with: bun test */ -import { describe, expect, test } from "bun:test" +import { afterAll, describe, expect, test } from "bun:test" import { tmpdir } from "node:os" import { _resetBashRunnerProbe, @@ -26,6 +26,13 @@ import { // (Copilot 3297050891). const TMP_DIR = tmpdir() +// Tear down the doc singleton and any other active subprocesses spawned during +// this file so they don't pollute the active-set state in subsequent test files +// (e.g. nu.activeSet.test.ts, which asserts the set is empty after runRaw). +afterAll(() => { + killAll() +}) + /** * Shared bash-runtime probe. `loadBashEnv("true")` exercises the same detect / * spawn path the real bridge uses, so a success here means the bashEnv tests