Skip to content

clear the global timeout once an operation is done in the Storage SDK #5703

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tame-beans-study.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@firebase/storage": patch
---

Clear the global timeout once an operation is done in the Storage SDK. Otherwise it may prevent Node.js from exiting.
24 changes: 18 additions & 6 deletions packages/storage/src/implementation/backoff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ export function start(
// Would type this as "number" but that doesn't work for Node so ¯\_(ツ)_/¯
// TODO: find a way to exclude Node type definition for storage because storage only works in browser
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let timeoutId: any = null;
let retryTimeoutId: any = null;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let globalTimeoutId: any = null;
let hitTimeout = false;
let cancelState = 0;

Expand All @@ -58,22 +60,31 @@ export function start(
}

function callWithDelay(millis: number): void {
timeoutId = setTimeout(() => {
timeoutId = null;
retryTimeoutId = setTimeout(() => {
retryTimeoutId = null;
f(handler, canceled());
}, millis);
}

function clearGlobalTimeout(): void {
if (globalTimeoutId) {
clearTimeout(globalTimeoutId);
}
}

function handler(success: boolean, ...args: any[]): void {
if (triggeredCallback) {
clearGlobalTimeout();
return;
}
if (success) {
clearGlobalTimeout();
triggerCallback.call(null, success, ...args);
return;
}
const mustStop = canceled() || hitTimeout;
if (mustStop) {
clearGlobalTimeout();
triggerCallback.call(null, success, ...args);
return;
}
Expand All @@ -97,14 +108,15 @@ export function start(
return;
}
stopped = true;
clearGlobalTimeout();
if (triggeredCallback) {
return;
}
if (timeoutId !== null) {
if (retryTimeoutId !== null) {
if (!wasTimeout) {
cancelState = 2;
}
clearTimeout(timeoutId);
clearTimeout(retryTimeoutId);
callWithDelay(0);
} else {
if (!wasTimeout) {
Expand All @@ -113,7 +125,7 @@ export function start(
}
}
callWithDelay(0);
setTimeout(() => {
globalTimeoutId = setTimeout(() => {
hitTimeout = true;
stop(true);
}, timeout);
Expand Down