diff --git a/tests/index.test.ts b/tests/index.test.ts index ae2bda0..c2b484d 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -254,6 +254,25 @@ describe( }); })); + test('resize after exit shouldn\'t throw', () => new Promise((done, reject) => { + const pty = new Pty({ + command: '/bin/echo', + args: ['hello'], + onExit: (err, exitCode) => { + try { + expect(err).toBeNull(); + expect(exitCode).toBe(0); + expect(() => { + pty.resize({ rows: 60, cols: 100 }); + }).not.toThrow(); + done(); + } catch (e) { + reject(e) + } + }, + }); + })); + test( 'ordering is correct', () => diff --git a/wrapper.ts b/wrapper.ts index 97c018c..e4796ea 100644 --- a/wrapper.ts +++ b/wrapper.ts @@ -45,6 +45,7 @@ type ExitResult = { export class Pty { #pty: RawPty; #fd: number; + #fdEnded: boolean = false; #socket: ReadStream; read: Readable; @@ -78,13 +79,12 @@ export class Pty { this.write = userFacingWrite; // catch end events - let handleCloseCalled = false; const handleClose = () => { - if (handleCloseCalled) { + if (this.#fdEnded) { return; } - handleCloseCalled = true; + this.#fdEnded = true; exitResult.then((result) => realExit(result.error, result.code)); userFacingRead.end(); }; @@ -122,6 +122,10 @@ export class Pty { } resize(size: Size) { + if (this.#fdEnded) { + return; + } + ptyResize(this.#fd, size); }