| 1011 | |
| 1012 | let cleanedUp = false; |
| 1013 | const cleanup = async (signal?: NodeJS.Signals): Promise<void> => { |
| 1014 | // Idempotent: signal-then-natural-close (or vice-versa) must not |
| 1015 | // call `harness.close()` twice. `cleanedUp` is checked-and-set |
| 1016 | // synchronously so concurrent invocations cannot race. |
| 1017 | if (cleanedUp) return; |
| 1018 | cleanedUp = true; |
| 1019 | if (signal) { |
| 1020 | log.info('acp: received signal, draining harness', { signal }); |
| 1021 | } |
| 1022 | try { |
| 1023 | await harness.close(); |
| 1024 | } catch (err) { |
| 1025 | // The process is exiting either way; log so the diagnostic is |
| 1026 | // preserved rather than disappearing into a thrown promise. |
| 1027 | log.error('acp: harness close failed during shutdown', { |
| 1028 | error: err instanceof Error ? err.message : String(err), |
| 1029 | }); |
| 1030 | } |
| 1031 | }; |
| 1032 | |
| 1033 | const onSigint = (): void => { |
| 1034 | void cleanup('SIGINT'); |