























@@ -541,25 +541,59 @@ export async function runTimedWatch(options, outputDir, deps = {}) {
541541resolve({ code: null, signal: null, error: error.message });
542542});
543543});
544+const raceSpawnError = async (operation) =>
545+await Promise.race([
546+Promise.resolve(operation).then((value) => ({ type: "value", value })),
547+spawnErrorExit.then((value) => ({ type: "spawn-error", value })),
548+]);
544549545550let watchPid = null;
551+let exit = null;
546552for (let attempt = 0; attempt < 50; attempt += 1) {
547553if (fs.existsSync(pidFilePath)) {
548554watchPid = Number(fs.readFileSync(pidFilePath, "utf8").trim());
549555break;
550556}
551-await sleepMs(100);
557+const waitResult = await raceSpawnError(sleepMs(100));
558+if (waitResult.type === "spawn-error") {
559+exit = waitResult.value;
560+break;
561+}
552562}
553563554-const readyBeforeWindow = await waitReady(() => `${stdout}\n${stderr}`, options.readyTimeoutMs);
555-if (readyBeforeWindow && options.readySettleMs > 0) {
556-await sleepMs(options.readySettleMs);
564+let readyBeforeWindow = false;
565+let idleCpuStartMs = null;
566+let idleCpuEndMs = null;
567+if (!exit) {
568+const readyResult = await raceSpawnError(
569+waitReady(() => `${stdout}\n${stderr}`, options.readyTimeoutMs),
570+);
571+if (readyResult.type === "spawn-error") {
572+exit = readyResult.value;
573+} else {
574+readyBeforeWindow = readyResult.value;
575+}
576+}
577+if (!exit && readyBeforeWindow && options.readySettleMs > 0) {
578+const settleResult = await raceSpawnError(sleepMs(options.readySettleMs));
579+if (settleResult.type === "spawn-error") {
580+exit = settleResult.value;
581+}
582+}
583+if (!exit) {
584+idleCpuStartMs = watchPid ? readCpuMs(watchPid) : null;
585+const windowResult = await raceSpawnError(sleepMs(options.windowMs));
586+if (windowResult.type === "spawn-error") {
587+exit = windowResult.value;
588+} else {
589+idleCpuEndMs = watchPid ? readCpuMs(watchPid) : null;
590+}
591+}
592+if (!exit) {
593+const stopResult = await raceSpawnError(stopChild(child, watchPid, options));
594+exit = stopResult.value;
557595}
558-const idleCpuStartMs = watchPid ? readCpuMs(watchPid) : null;
559-await sleepMs(options.windowMs);
560-const idleCpuEndMs = watchPid ? readCpuMs(watchPid) : null;
561596562-const exit = await Promise.race([stopChild(child, watchPid, options), spawnErrorExit]);
563597fs.writeFileSync(stdoutPath, formatCapturedWatchLog(stdout, stdoutTruncated), "utf8");
564598fs.writeFileSync(stderrPath, formatCapturedWatchLog(stderr, stderrTruncated), "utf8");
565599const timingFileMissing = !fs.existsSync(timeFilePath);
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。