js: reject detachable on pipes in spawn() and exec() (#156)

Co-authored-by: Pooya Parsa <pooya@pi0.io>
This commit is contained in:
pi0x
2026-12-02 23:08:42 +09:00
committed by GitHub
parent eb6f2a2495
commit 595d90f8a6
2 changed files with 10 additions and 0 deletions

View File

@@ -1054,6 +1054,8 @@ export async function attachPty(vsock: Vsock, session: string | TtySessionSnapsh
* guest without PIPES (a kernel from before them) refuses it: EREMOTE.
*/
export async function spawnSubprocess(vsock: Vsock, file?: string, args: readonly string[] = [], opts: SpawnOptions = {}): Promise<Subprocess> {
// Not in the type, but plain JavaScript can pass it: refuse rather than open a session that does not survive a restore.
if ((opts as PtyOptions).detachable) throw new RangeError("kwtty: only a session on a pty can be detachable");
const s = await openSession(vsock, encodeTtyOpen({ ...opts, cols: 0, rows: 0, file, args, pipes: true }), opts);
return new PipeSession(s.conn, s.reader, s.writer, s.parser, s.opened);
}

View File

@@ -2049,6 +2049,14 @@ console.log(stdout.trim());
await done;
});
test("pipes cannot be detachable: spawn({ pty: false, detachable }) and exec({ detachable }) reject with a RangeError", async () => {
// Not in PipeOptions or ExecOptions: what plain JavaScript may pass.
const detachable = { detachable: true } as {};
const isPtyOnly = (e: Error) => e instanceof RangeError && /only a session on a pty can be detachable/.test(e.message);
await assert.rejects(k.vm.spawn("true", [], { pty: false, ...detachable }), isPtyOnly);
await assert.rejects(k.vm.exec("true", [], detachable), isPtyOnly);
});
test("pipes: close() hangs up the program's process group (SIGHUP), its children included", async () => {
const p = await k.vm.spawn("sh", ["-c", "sleep 1000 & echo $!; wait"], { pty: false });
const reader = p.stdout.getReader();