* Kill a session and wait for the OS to reap the PTY (so a follow-up `spawn` * with the same sessionId doesn't trip the "already running" guard). Falls * back to dropping the handle if the exit event doesn't arrive in time — * SIGKILL the underlying pid first to make sure we're not leaking a p
( sessionId: string, timeoutMs = 2000, )
| 318 | * SIGKILL the underlying pid first to make sure we're not leaking a process. |
| 319 | */ |
| 320 | async killAndWait( |
| 321 | sessionId: string, |
| 322 | timeoutMs = 2000, |
| 323 | ): Promise<boolean> { |
| 324 | const handle = this.handles.get(sessionId); |
| 325 | if (!handle) return false; |
| 326 | if (handle.exited) { |
| 327 | this.handles.delete(sessionId); |
| 328 | return true; |
| 329 | } |
| 330 | |
| 331 | const exited = await new Promise<boolean>(resolve => { |
| 332 | const onExit = () => { |
| 333 | clearTimeout(timer); |
| 334 | resolve(true); |
| 335 | }; |
| 336 | handle.exitSubscribers.add(onExit); |
| 337 | const timer = setTimeout(() => { |
| 338 | handle.exitSubscribers.delete(onExit); |
| 339 | resolve(false); |
| 340 | }, timeoutMs); |
| 341 | this.kill(sessionId, 'SIGTERM'); |
| 342 | }); |
| 343 | |
| 344 | if (!exited) { |
| 345 | try { |
| 346 | handle.pty.kill('SIGKILL'); |
| 347 | } catch { |
| 348 | /* ignore */ |
| 349 | } |
| 350 | } |
| 351 | this.handles.delete(sessionId); |
| 352 | return true; |
| 353 | } |
| 354 | |
| 355 | killAll(): void { |
| 356 | for (const sessionId of this.handles.keys()) { |