diff --git a/js/core/tty.ts b/js/core/tty.ts index e8b6f8b..78909b6 100644 --- a/js/core/tty.ts +++ b/js/core/tty.ts @@ -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 { + // 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); } diff --git a/test/api.test.ts b/test/api.test.ts index 2eac42d..d552d54 100644 --- a/test/api.test.ts +++ b/test/api.test.ts @@ -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();