|
| 1 | +import { describe, expect } from "bun:test" |
| 2 | +import { Bus } from "../../src/bus" |
| 3 | +import { Config } from "../../src/config/config" |
| 4 | +import { Plugin } from "../../src/plugin" |
| 5 | +import { Pty } from "../../src/pty" |
| 6 | +import { CrossSpawnSpawner } from "@opencode-ai/core/cross-spawn-spawner" |
| 7 | +import { Effect, Layer } from "effect" |
| 8 | +import { testEffect, pollWithTimeout } from "../lib/effect" |
| 9 | +import { disposeAllInstances, provideTmpdirInstance } from "../fixture/fixture" |
| 10 | +import * as ptyNode from "../../src/pty/pty.node" |
| 11 | + |
| 12 | +const it = testEffect( |
| 13 | + Pty.layer.pipe( |
| 14 | + Layer.provideMerge(Bus.layer), |
| 15 | + Layer.provideMerge(Config.defaultLayer), |
| 16 | + Layer.provideMerge(Plugin.defaultLayer), |
| 17 | + Layer.provideMerge(CrossSpawnSpawner.defaultLayer), |
| 18 | + ), |
| 19 | +) |
| 20 | + |
| 21 | +const ptyTest = process.platform === "win32" ? it.live.skip : it.live |
| 22 | + |
| 23 | +// A child that ignores SIGHUP. `trap '' HUP` sets SIG_IGN, which persists across |
| 24 | +// exec, so the resulting `sleep` ignores SIGHUP. The default kill signal (SIGHUP |
| 25 | +// to a single pid) cannot kill it; only a process-group SIGKILL can. This makes |
| 26 | +// the test distinguish force-kill from the baseline signal semantics. |
| 27 | +const HUP_IMMUNE = ["-c", "trap '' HUP; exec sleep 300"] |
| 28 | + |
| 29 | +// Wait until the given pid no longer exists (process.kill(pid, 0) throws ESRCH). |
| 30 | +const waitDead = (pid: number) => |
| 31 | + pollWithTimeout( |
| 32 | + Effect.sync(() => { |
| 33 | + try { |
| 34 | + process.kill(pid, 0) |
| 35 | + return undefined |
| 36 | + } catch (err) { |
| 37 | + return (err as NodeJS.ErrnoException).code === "ESRCH" ? (true as const) : undefined |
| 38 | + } |
| 39 | + }), |
| 40 | + "child process still alive after dispose", |
| 41 | + ) |
| 42 | + |
| 43 | +describe("pty dispose kill", () => { |
| 44 | + ptyTest( |
| 45 | + "kills a HUP-immune child with SIGKILL when the instance is disposed (bun adapter)", |
| 46 | + () => |
| 47 | + provideTmpdirInstance(() => |
| 48 | + Effect.gen(function* () { |
| 49 | + const pty = yield* Pty.Service |
| 50 | + const info = yield* pty.create({ command: "sh", args: HUP_IMMUNE }) |
| 51 | + |
| 52 | + // Windows ConPTY assigns the pid asynchronously; on other platforms it |
| 53 | + // is already non-zero at create time. Poll until it is assigned. |
| 54 | + const pid = yield* pollWithTimeout( |
| 55 | + Effect.sync(() => (info.pid > 0 ? info.pid : undefined)), |
| 56 | + "pty pid never assigned", |
| 57 | + ) |
| 58 | + |
| 59 | + // Trigger the InstanceState finalizer (disposeAllInstances) which must |
| 60 | + // SIGKILL the running child process group. |
| 61 | + yield* Effect.promise(() => disposeAllInstances()) |
| 62 | + |
| 63 | + yield* waitDead(pid) |
| 64 | + }), |
| 65 | + ), |
| 66 | + ) |
| 67 | + |
| 68 | + ptyTest( |
| 69 | + "kills a HUP-immune child with SIGKILL via the node adapter kill()", |
| 70 | + () => |
| 71 | + Effect.gen(function* () { |
| 72 | + const proc = ptyNode.spawn("sh", HUP_IMMUNE, { name: "xterm-256color" }) |
| 73 | + const pid = proc.pid |
| 74 | + expect(pid).toBeGreaterThan(0) |
| 75 | + |
| 76 | + proc.kill("SIGKILL") |
| 77 | + |
| 78 | + yield* waitDead(pid) |
| 79 | + }), |
| 80 | + ) |
| 81 | +}) |
0 commit comments