Skip to content

Commit f75e76f

Browse files
committed
fix(wasm): dispose parser pool on CLI shutdown to prevent hang (#975)
Impact: 1 functions changed, 1 affected
1 parent fc1ebf7 commit f75e76f

1 file changed

Lines changed: 31 additions & 8 deletions

File tree

src/cli.ts

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,37 @@
11
#!/usr/bin/env node
22

33
import { run } from './cli/index.js';
4+
import { disposeParsers } from './domain/parser.js';
45
import { CodegraphError, toErrorMessage } from './shared/errors.js';
56

6-
run().catch((err: unknown) => {
7-
if (err instanceof CodegraphError) {
8-
console.error(`codegraph [${err.code}]: ${err.message}`);
9-
if (err.file) console.error(` file: ${err.file}`);
10-
} else {
11-
console.error(`codegraph: fatal error — ${toErrorMessage(err)}`);
7+
/**
8+
* After the CLI command finishes, tear down any cached WASM parsers and the
9+
* worker thread pool. The WASM parse worker (see `domain/wasm-worker-pool.ts`)
10+
* keeps the event loop alive until `worker.terminate()` is called, so without
11+
* this teardown short-lived commands like `codegraph build` would hang for
12+
* minutes before Node gives up — surfacing in CI as `spawnSync ETIMEDOUT`
13+
* even though the command's work is already complete.
14+
*
15+
* `disposeParsers` is safe to call when the pool was never instantiated
16+
* (e.g. native engine, or commands that never parse): it no-ops cleanly.
17+
*/
18+
async function shutdown(): Promise<void> {
19+
try {
20+
await disposeParsers();
21+
} catch {
22+
/* don't mask the real exit status over a teardown failure */
1223
}
13-
process.exit(1);
14-
});
24+
}
25+
26+
run()
27+
.then(shutdown)
28+
.catch(async (err: unknown) => {
29+
if (err instanceof CodegraphError) {
30+
console.error(`codegraph [${err.code}]: ${err.message}`);
31+
if (err.file) console.error(` file: ${err.file}`);
32+
} else {
33+
console.error(`codegraph: fatal error — ${toErrorMessage(err)}`);
34+
}
35+
await shutdown();
36+
process.exit(1);
37+
});

0 commit comments

Comments
 (0)