Skip to content

Commit e00cd12

Browse files
authored
Merge pull request github#1539 from github/aeisenberg/unref-delay
Avoid unref-ing timer while awaiting status upload
2 parents a25536b + a2487fb commit e00cd12

File tree

6 files changed

+43
-16
lines changed

6 files changed

+43
-16
lines changed

lib/upload-lib.js

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/upload-lib.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/util.js

Lines changed: 16 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/util.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/upload-lib.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,9 @@ export async function waitForProcessing(
463463
util.assertNever(status);
464464
}
465465

466-
await util.delay(STATUS_CHECK_FREQUENCY_MILLISECONDS);
466+
await util.delay(STATUS_CHECK_FREQUENCY_MILLISECONDS, {
467+
allowProcessExit: false,
468+
});
467469
}
468470
} finally {
469471
logger.endGroup();

src/util.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -548,10 +548,23 @@ export async function bundleDb(
548548
return databaseBundlePath;
549549
}
550550

551-
export async function delay(milliseconds: number) {
552-
// Immediately `unref` the timer such that it only prevents the process from exiting if the
553-
// surrounding promise is being awaited.
554-
return new Promise((resolve) => setTimeout(resolve, milliseconds).unref());
551+
/**
552+
* @param milliseconds time to delay
553+
* @param opts options
554+
* @param opts.allowProcessExit if true, the timer will not prevent the process from exiting
555+
*/
556+
export async function delay(
557+
milliseconds: number,
558+
{ allowProcessExit }: { allowProcessExit: boolean }
559+
) {
560+
return new Promise((resolve) => {
561+
const timer = setTimeout(resolve, milliseconds);
562+
if (allowProcessExit) {
563+
// Immediately `unref` the timer such that it only prevents the process from exiting if the
564+
// surrounding promise is being awaited.
565+
timer.unref();
566+
}
567+
});
555568
}
556569

557570
export function isGoodVersion(versionSpec: string) {
@@ -748,7 +761,7 @@ export async function withTimeout<T>(
748761
return result;
749762
};
750763
const timeoutTask = async () => {
751-
await delay(timeoutMs);
764+
await delay(timeoutMs, { allowProcessExit: true });
752765
if (!finished) {
753766
// Workaround: While the promise racing below will allow the main code
754767
// to continue, the process won't normally exit until the asynchronous
@@ -773,7 +786,7 @@ export async function checkForTimeout() {
773786
core.info(
774787
"A timeout occurred, force exiting the process after 30 seconds to prevent hanging."
775788
);
776-
await delay(30_000);
789+
await delay(30_000, { allowProcessExit: true });
777790
process.exit();
778791
}
779792
}

0 commit comments

Comments
 (0)