Fix silent exit when a query runs on a connection left idle by a prompt
An idle Bun.SQL connection does not keep the event loop alive. After an inquirer prompt released stdin, the next query let Bun exit with code 0 mid-query: `schema` never applied confirmed destructive changes, and `admin` table views exited instead of showing results. Hold the loop open for the duration of every query and transaction. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Vendored
+16
-1
@@ -16,6 +16,21 @@ export function resolveClient(config) {
|
||||
}
|
||||
return grabMariaDBClient({ config });
|
||||
}
|
||||
/**
|
||||
* Keep the event loop alive until `fn` settles.
|
||||
* An idle Bun.SQL connection does not hold the process open, so a query sent
|
||||
* on it after something else released the loop (e.g. an inquirer prompt
|
||||
* releasing stdin) lets Bun exit silently mid-query.
|
||||
*/
|
||||
export async function holdEventLoop(fn) {
|
||||
const timer = setInterval(() => { }, 1 << 30);
|
||||
try {
|
||||
return await fn();
|
||||
}
|
||||
finally {
|
||||
clearInterval(timer);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Convert driver rows to plain objects.
|
||||
* - BigInt -> number (or string when outside the safe integer range)
|
||||
@@ -54,7 +69,7 @@ export default async function dbHandler({ query, values, config, client }) {
|
||||
if (!CLIENT) {
|
||||
throw new Error(`Couldn't grab MariaDB Client.`);
|
||||
}
|
||||
const res = await CLIENT.unsafe(query, values);
|
||||
const res = await holdEventLoop(() => CLIENT.unsafe(query, values));
|
||||
const count = res.count;
|
||||
const res_array = normalizeRows(res);
|
||||
const last_insert_id = res_array?.[0] ? res_array[0]?.id : undefined;
|
||||
|
||||
Reference in New Issue
Block a user