Skip to content

Commit 4d5d587

Browse files
committed
fix: use singleton readline interface for stdin prompts
Replace broken Bun.stdin.stream() approach with a singleton readline interface. The previous implementation created a new stream reader on each call, which could fail on subsequent prompts due to stream state issues.
1 parent 5a7fb2f commit 4d5d587

1 file changed

Lines changed: 24 additions & 12 deletions

File tree

cli/bin/postgres-ai.ts

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,22 @@ import { applyInitPlan, buildInitPlan, connectWithSslFallback, DEFAULT_MONITORIN
1515
import * as pkce from "../lib/pkce";
1616
import * as authServer from "../lib/auth-server";
1717
import { maskSecret } from "../lib/util";
18+
import { createInterface } from "readline";
19+
20+
// Singleton readline interface for stdin prompts
21+
let rl: ReturnType<typeof createInterface> | null = null;
22+
function getReadline() {
23+
if (!rl) {
24+
rl = createInterface({ input: process.stdin, output: process.stdout });
25+
}
26+
return rl;
27+
}
28+
function closeReadline() {
29+
if (rl) {
30+
rl.close();
31+
rl = null;
32+
}
33+
}
1834

1935
// Helper functions for spawning processes using Bun APIs
2036
async function execPromise(command: string): Promise<{ stdout: string; stderr: string }> {
@@ -95,17 +111,11 @@ function spawn(cmd: string, args: string[], options?: { stdio?: "pipe" | "ignore
95111

96112
// Simple readline-like interface for prompts using Bun
97113
async function question(prompt: string): Promise<string> {
98-
process.stdout.write(prompt);
99-
const reader = Bun.stdin.stream().getReader();
100-
let result = "";
101-
while (true) {
102-
const { done, value } = await reader.read();
103-
if (done) break;
104-
result += new TextDecoder().decode(value);
105-
if (result.includes("\n")) break;
106-
}
107-
reader.releaseLock();
108-
return result.trim();
114+
return new Promise((resolve) => {
115+
getReadline().question(prompt, (answer) => {
116+
resolve(answer);
117+
});
118+
});
109119
}
110120

111121
/**
@@ -2272,5 +2282,7 @@ mcp
22722282
}
22732283
});
22742284

2275-
program.parseAsync(process.argv);
2285+
program.parseAsync(process.argv).finally(() => {
2286+
closeReadline();
2287+
});
22762288

0 commit comments

Comments
 (0)