Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions server/src/nu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,14 @@ async function spawnNu(argv: string[], opts: RunOptions): Promise<RawResult> {
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)
}
Comment thread
zaynram marked this conversation as resolved.
Resolved
Expand Down Expand Up @@ -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)
Comment thread
zaynram marked this conversation as resolved.
Resolved
return parseEnv0(envText)
} finally {
if (timerId !== undefined) clearTimeout(timerId)
Expand Down
11 changes: 9 additions & 2 deletions server/test/nu.activeSet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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()
})
Comment on lines +22 to +26

describe("sanitizeKey export (Cycle 0)", () => {
test("accepts valid keys and defaults to 'default'", () => {
expect(sanitizeKey("valid-key_123")).toBe("valid-key_123")
Expand Down Expand Up @@ -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)
})
Expand Down
9 changes: 8 additions & 1 deletion server/test/smoke.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
Loading