Skip to content

Commit 1e5c740

Browse files
committed
fix: worker exit code, force terminate only after timeout
1 parent a2d0034 commit 1e5c740

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

packages/qwik-router/src/ssg/node/node-main.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export async function createNodeMainProcess(sys: System, opts: SsgOptions) {
1818
const ssgWorkers: SsgWorker[] = [];
1919
const sitemapBuffer: string[] = [];
2020
let sitemapPromise: Promise<any> | null = null;
21-
let hasExited = false;
21+
2222
opts = { ...opts };
2323

2424
let outDir = opts.outDir;
@@ -55,6 +55,7 @@ export async function createNodeMainProcess(sys: System, opts: SsgOptions) {
5555
const mainTasks = new Map<string, WorkerMainTask>();
5656

5757
let workerFilePath: string | URL;
58+
let terminateTimeout: number | null = null;
5859

5960
// Launch the worker using the package's index module, which bootstraps the worker thread.
6061
if (typeof __filename === 'string') {
@@ -103,17 +104,9 @@ export async function createNodeMainProcess(sys: System, opts: SsgOptions) {
103104
terminateResolve = resolve;
104105
nodeWorker.postMessage(msg);
105106
});
106-
// Wait for worker to exit naturally (it calls process.exit(0) after close)
107-
// If it doesn't exit in time, force terminate
108-
const maxWaitMs = 1000;
109-
const startTime = Date.now();
110-
while (!hasExited && Date.now() - startTime < maxWaitMs) {
111-
await new Promise((resolve) => setTimeout(resolve, 50));
112-
}
113-
114-
if (!hasExited) {
107+
terminateTimeout = setTimeout(async () => {
115108
await nodeWorker.terminate();
116-
}
109+
}, 1000) as unknown as number;
117110
},
118111
};
119112

@@ -143,7 +136,10 @@ export async function createNodeMainProcess(sys: System, opts: SsgOptions) {
143136
});
144137

145138
nodeWorker.on('exit', (code) => {
146-
hasExited = true;
139+
if (terminateTimeout) {
140+
clearTimeout(terminateTimeout);
141+
terminateTimeout = null;
142+
}
147143
if (code !== 0) {
148144
console.error(`worker exit ${code}`);
149145
}

0 commit comments

Comments
 (0)