Skip to content
Open
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
1 change: 1 addition & 0 deletions src/unix/pty.cc
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ void SetupExitCallback(Napi::Env env, Napi::Function cb, pid_t pid) {
}
}
}
close(kq);
#else
while (true) {
errno = 0;
Expand Down
31 changes: 31 additions & 0 deletions src/unixTerminal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,37 @@ if (process.platform !== 'win32') {
`Leaked ${finalCount - initialCount} /dev/ptmx FDs after spawning 20 PTYs (initial: ${initialCount}, final: ${finalCount})`
);
});
it('should not leak kqueue file descriptors after pty exit', async function(): Promise<void> {
this.timeout(30000);

const getKqueueFDCount = (): number => {
try {
const output = cp.execSync(`lsof -p ${process.pid} 2>/dev/null`, { encoding: 'utf8' });
return output.split('\n').filter(line => line.includes('KQUEUE')).length;
} catch {
return 0;
}
};

const initialCount = getKqueueFDCount();
for (let i = 0; i < 20; i++) {
const term = new UnixTerminal('/bin/bash', ['-c', 'echo hello']);
await new Promise<void>(resolve => {
term.onExit(() => {
term.destroy();
resolve();
});
});
}

await new Promise(r => setTimeout(r, 500));

const finalCount = getKqueueFDCount();
assert.ok(
finalCount <= initialCount,
`Leaked ${finalCount - initialCount} kqueue FDs after spawning 20 PTYs (initial: ${initialCount}, final: ${finalCount})`
);
});
}
it('should handle exec() errors', (done) => {
const term = new UnixTerminal('/bin/bogus.exe', []);
Expand Down