Skip to content

πŸŽ₯ Make videos programmatically with React

Fix twoslash workers hanging: force exit and add timeout/stderr logging

Jonny Burger8fca30615819
diff --git a/packages/docs/prewarm-twoslash.ts b/packages/docs/prewarm-twoslash.ts
index 6925cfa8c8..6a564c0a2a 100644
--- a/packages/docs/prewarm-twoslash.ts
+++ b/packages/docs/prewarm-twoslash.ts
@@ -152,6 +152,8 @@ interface WorkerResult {
timings: TimingEntry[];
}
+const WORKER_TIMEOUT_MS = 5 * 60 * 1000; // 5 minutes per worker
+
function runWorker(
workItems: TwoslashBlock[],
workerId: number,
@@ -166,6 +168,19 @@ function runWorker(
});
let lastReport: WorkerResult = {completed: 0, errors: 0, timings: []};
+ let settled = false;
+
+ const timeout = setTimeout(() => {
+ if (!settled) {
+ settled = true;
+ console.error(`Worker ${workerId} timed out after ${WORKER_TIMEOUT_MS / 1000}s, killing...`);
+ child.kill('SIGKILL');
+ try {
+ unlinkSync(tmpFile);
+ } catch {}
+ resolvePromise(lastReport);
+ }
+ }, WORKER_TIMEOUT_MS);
child.stdout.on('data', (data: Buffer) => {
const lines = data.toString().trim().split('\n');
@@ -178,20 +193,33 @@ function runWorker(
}
});
- child.stderr.on('data', () => {});
+ child.stderr.on('data', (data: Buffer) => {
+ process.stderr.write(`[worker ${workerId}] ${data}`);
+ });
- child.on('close', () => {
- try {
- unlinkSync(tmpFile);
- } catch {}
- resolvePromise(lastReport);
+ child.on('close', (code) => {
+ clearTimeout(timeout);
+ if (!settled) {
+ settled = true;
+ if (code !== 0 && code !== null) {
+ console.error(`Worker ${workerId} exited with code ${code}`);
+ }
+ try {
+ unlinkSync(tmpFile);
+ } catch {}
+ resolvePromise(lastReport);
+ }
});
child.on('error', (err) => {
- try {
- unlinkSync(tmpFile);
- } catch {}
- reject(err);
+ clearTimeout(timeout);
+ if (!settled) {
+ settled = true;
+ try {
+ unlinkSync(tmpFile);
+ } catch {}
+ reject(err);
+ }
});
});
}
diff --git a/packages/docs/twoslash-worker.ts b/packages/docs/twoslash-worker.ts
index 5487edf99e..68531f2b01 100644
--- a/packages/docs/twoslash-worker.ts
+++ b/packages/docs/twoslash-worker.ts
@@ -88,3 +88,6 @@ process.stdout.write(
timings,
}) + '\n',
);
+
+// Force exit β€” createTwoslasher holds a TS language service that keeps the process alive
+process.exit(0);