Compare commits

...

1 Commits

Author SHA1 Message Date
Spencer Tang
90fda1400e fix(core): add timeout to tree-sitter initialization to prevent hanging 2026-04-08 17:06:50 -04:00

View File

@ -108,6 +108,8 @@ export async function resolveExecutable(
let bashLanguage: Language | null = null; let bashLanguage: Language | null = null;
let treeSitterInitialization: Promise<void> | null = null; let treeSitterInitialization: Promise<void> | null = null;
let treeSitterInitializationError: Error | null = null; let treeSitterInitializationError: Error | null = null;
let parserState: 'uninitialized' | 'initializing' | 'initialized' | 'error' =
'uninitialized';
class ShellParserInitializationError extends Error { class ShellParserInitializationError extends Error {
constructor(cause: Error) { constructor(cause: Error) {
@ -163,8 +165,27 @@ async function loadBashLanguage(): Promise<void> {
} }
export async function initializeShellParsers(): Promise<void> { export async function initializeShellParsers(): Promise<void> {
if (!treeSitterInitialization) { if (parserState === 'uninitialized') {
treeSitterInitialization = loadBashLanguage().catch((error) => { parserState = 'initializing';
let timerId: NodeJS.Timeout | undefined;
const timeoutPromise = new Promise<void>((_, reject) => {
timerId = setTimeout(
() => reject(new Error('Tree-sitter initialization timed out')),
30000,
);
});
treeSitterInitialization = Promise.race([
loadBashLanguage(),
timeoutPromise,
])
.finally(() => {
if (timerId) clearTimeout(timerId);
})
.then(() => {
parserState = 'initialized';
})
.catch((error) => {
parserState = 'error';
treeSitterInitialization = null; treeSitterInitialization = null;
// Log the error but don't throw, allowing the application to fall back to safe defaults (ASK_USER) // Log the error but don't throw, allowing the application to fall back to safe defaults (ASK_USER)
// or regex checks where appropriate. // or regex checks where appropriate.
@ -172,7 +193,9 @@ export async function initializeShellParsers(): Promise<void> {
}); });
} }
if (treeSitterInitialization) {
await treeSitterInitialization; await treeSitterInitialization;
}
} }
export interface ParsedCommandDetail { export interface ParsedCommandDetail {
@ -847,7 +870,8 @@ export const spawnAsync = async (
const { program: finalCommand, args: finalArgs, env: finalEnv } = prepared; const { program: finalCommand, args: finalArgs, env: finalEnv } = prepared;
return new Promise((resolve, reject) => { try {
return await new Promise((resolve, reject) => {
const child = spawn(finalCommand, finalArgs, { const child = spawn(finalCommand, finalArgs, {
...options, ...options,
env: finalEnv, env: finalEnv,
@ -867,7 +891,9 @@ export const spawnAsync = async (
if (code === 0) { if (code === 0) {
resolve({ stdout, stderr }); resolve({ stdout, stderr });
} else { } else {
reject(new Error(`Command failed with exit code ${code}:\n${stderr}`)); reject(
new Error(`Command failed with exit code ${code}:\n${stderr}`),
);
} }
}); });
@ -875,6 +901,9 @@ export const spawnAsync = async (
reject(err); reject(err);
}); });
}); });
} finally {
prepared.cleanup?.();
}
}; };
/** /**
@ -902,6 +931,7 @@ export async function* execStreaming(
env: options?.env ?? process.env, env: options?.env ?? process.env,
}); });
try {
const { program: finalCommand, args: finalArgs, env: finalEnv } = prepared; const { program: finalCommand, args: finalArgs, env: finalEnv } = prepared;
const child = spawn(finalCommand, finalArgs, { const child = spawn(finalCommand, finalArgs, {
@ -1003,8 +1033,13 @@ export async function* execStreaming(
checkExit(child.exitCode); checkExit(child.exitCode);
} else { } else {
child.on('close', (code) => checkExit(code)); child.on('close', (code) => checkExit(code));
child.on('error', (err) => reject(err)); child.on('error', (err) => {
reject(err);
});
} }
}); });
} }
} finally {
prepared.cleanup?.();
}
} }