From 63f0b128fe6bcda42204a1639fe4bdaf14d46ee1 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 28 May 2026 09:04:22 +0000 Subject: [PATCH 1/2] fix: call removeActive/clearTimeout before return in spawnNu and dumpEnv In bun v1.3.14, the `finally` block in async functions can be deferred to a separate microtask tick after the promise resolves, rather than running synchronously before the return value is yielded to the caller (contrary to the ECMAScript spec). This caused a flaky test failure: `nu.activeSet.test.ts:53` expected the active set to be empty after `await runRaw(...)` returned, but the `finally`-based `removeActive(proc)` call hadn't run yet. Fix: explicitly call `clearTimeout` + `removeActive` before the `return` in the success path of both `spawnNu` and `dumpEnv`. The `finally` block is retained for error/signal path safety; both calls are idempotent so the double-call is harmless. --- server/src/nu.ts | 9 +++++++++ 1 file changed, 9 insertions(+) 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) From 166908f8af48c1ed4204803d468a3ef8c12e652c Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 28 May 2026 09:18:41 +0000 Subject: [PATCH 2/2] fix: add afterAll(killAll) to smoke.test.ts and beforeAll(killAll) to nu.activeSet.test.ts --- server/test/nu.activeSet.test.ts | 11 +++++++++-- server/test/smoke.test.ts | 9 ++++++++- 2 files changed, 17 insertions(+), 3 deletions(-) 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