|
1 | 1 | #!/usr/bin/env node |
2 | 2 |
|
3 | 3 | import { run } from './cli/index.js'; |
| 4 | +import { disposeParsers } from './domain/parser.js'; |
4 | 5 | import { CodegraphError, toErrorMessage } from './shared/errors.js'; |
5 | 6 |
|
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 */ |
12 | 23 | } |
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