mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-15 06:04:40 +00:00
fix(tui): drain stdin on exit to avoid Kitty release leak
Drain stdin for up to 1s after disabling Kitty protocol so in-flight key release events are consumed before the shell regains control. Fixes #1204
This commit is contained in:
parent
9a4d043b28
commit
8c38de0495
5 changed files with 42 additions and 22 deletions
|
|
@ -4,7 +4,7 @@
|
|||
|
||||
### Fixed
|
||||
|
||||
- Fixed Kitty key release events leaking to parent shell over slow SSH connections ([#1204](https://github.com/badlogic/pi-mono/issues/1204))
|
||||
- Fixed Kitty key release events leaking to parent shell over slow SSH connections by draining stdin for up to 1s on exit ([#1204](https://github.com/badlogic/pi-mono/issues/1204))
|
||||
|
||||
## [0.51.1] - 2026-02-02
|
||||
|
||||
|
|
|
|||
|
|
@ -2554,7 +2554,7 @@ export class InteractiveMode {
|
|||
|
||||
// Drain any in-flight Kitty key release events before stopping.
|
||||
// This prevents escape sequences from leaking to the parent shell over slow SSH.
|
||||
await this.ui.terminal.prepareForExit();
|
||||
await this.ui.terminal.drainInput(1000);
|
||||
|
||||
this.stop();
|
||||
process.exit(0);
|
||||
|
|
|
|||
|
|
@ -4,11 +4,11 @@
|
|||
|
||||
### Added
|
||||
|
||||
- Added `Terminal.prepareForExit()` method to drain Kitty key release events before exit
|
||||
- Added `Terminal.drainInput()` to drain stdin before exit (prevents Kitty key release events leaking over slow SSH)
|
||||
|
||||
### Fixed
|
||||
|
||||
- Fixed Kitty key release events leaking to parent shell over slow SSH connections ([#1204](https://github.com/badlogic/pi-mono/issues/1204))
|
||||
- Fixed Kitty key release events leaking to parent shell over slow SSH connections by draining stdin for up to 1s ([#1204](https://github.com/badlogic/pi-mono/issues/1204))
|
||||
|
||||
## [0.51.1] - 2026-02-02
|
||||
|
||||
|
|
|
|||
|
|
@ -13,12 +13,12 @@ export interface Terminal {
|
|||
stop(): void;
|
||||
|
||||
/**
|
||||
* Prepare for process exit by disabling Kitty protocol and draining stdin.
|
||||
* Call this before stop() when exiting to prevent Kitty key release events
|
||||
* from leaking to the parent shell over slow SSH connections.
|
||||
* @param drainMs - How long to drain stdin (default: 50ms)
|
||||
* Drain stdin before exiting to prevent Kitty key release events from
|
||||
* leaking to the parent shell over slow SSH connections.
|
||||
* @param maxMs - Maximum time to drain (default: 1000ms)
|
||||
* @param idleMs - Exit early if no input arrives within this time (default: 50ms)
|
||||
*/
|
||||
prepareForExit(drainMs?: number): Promise<void>;
|
||||
drainInput(maxMs?: number, idleMs?: number): Promise<void>;
|
||||
|
||||
// Write output to terminal
|
||||
write(data: string): void;
|
||||
|
|
@ -158,25 +158,45 @@ export class ProcessTerminal implements Terminal {
|
|||
process.stdout.write("\x1b[?u");
|
||||
}
|
||||
|
||||
async prepareForExit(drainMs = 50): Promise<void> {
|
||||
if (!this._kittyProtocolActive) return;
|
||||
async drainInput(maxMs = 1000, idleMs = 50): Promise<void> {
|
||||
if (this._kittyProtocolActive) {
|
||||
// Disable Kitty keyboard protocol first so any late key releases
|
||||
// do not generate new Kitty escape sequences.
|
||||
process.stdout.write("\x1b[<u");
|
||||
this._kittyProtocolActive = false;
|
||||
setKittyProtocolActive(false);
|
||||
}
|
||||
|
||||
// Disable Kitty keyboard protocol first
|
||||
process.stdout.write("\x1b[<u");
|
||||
this._kittyProtocolActive = false;
|
||||
setKittyProtocolActive(false);
|
||||
const previousHandler = this.inputHandler;
|
||||
this.inputHandler = undefined;
|
||||
|
||||
// Wait briefly to let any in-flight key release events arrive and be
|
||||
// consumed by our still-active stdin handler. This prevents Kitty
|
||||
// escape sequences from leaking to the parent shell over slow SSH.
|
||||
await new Promise((resolve) => setTimeout(resolve, drainMs));
|
||||
let lastDataTime = Date.now();
|
||||
const onData = () => {
|
||||
lastDataTime = Date.now();
|
||||
};
|
||||
|
||||
process.stdin.on("data", onData);
|
||||
const endTime = Date.now() + maxMs;
|
||||
|
||||
try {
|
||||
while (true) {
|
||||
const now = Date.now();
|
||||
const timeLeft = endTime - now;
|
||||
if (timeLeft <= 0) break;
|
||||
if (now - lastDataTime >= idleMs) break;
|
||||
await new Promise((resolve) => setTimeout(resolve, Math.min(idleMs, timeLeft)));
|
||||
}
|
||||
} finally {
|
||||
process.stdin.removeListener("data", onData);
|
||||
this.inputHandler = previousHandler;
|
||||
}
|
||||
}
|
||||
|
||||
stop(): void {
|
||||
// Disable bracketed paste mode
|
||||
process.stdout.write("\x1b[?2004l");
|
||||
|
||||
// Disable Kitty keyboard protocol if not already done by prepareForExit()
|
||||
// Disable Kitty keyboard protocol if not already done by drainInput()
|
||||
if (this._kittyProtocolActive) {
|
||||
process.stdout.write("\x1b[<u");
|
||||
this._kittyProtocolActive = false;
|
||||
|
|
|
|||
|
|
@ -36,8 +36,8 @@ export class VirtualTerminal implements Terminal {
|
|||
this.xterm.write("\x1b[?2004h");
|
||||
}
|
||||
|
||||
async prepareForExit(_drainMs?: number): Promise<void> {
|
||||
// No-op for virtual terminal - no Kitty protocol to drain
|
||||
async drainInput(_maxMs?: number, _idleMs?: number): Promise<void> {
|
||||
// No-op for virtual terminal - no stdin to drain
|
||||
}
|
||||
|
||||
stop(): void {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue