diff --git a/CHANGELOG.md b/CHANGELOG.md index 4a8bef7d688..1f56cdddbb5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,2 +1,3 @@ - Added `crashlytics:heapdumps:{enable,disable,status}` commands to manage Crashlytics Android heap dump collection. (#11225) - Removed unused dependencies `proxy-agent`, `universal-analytics`, and `@apphosting/common`. +- Fixed an issue where throttled task queues stopped processing remaining tasks when a task exhausted its retries or timed out (#6989, #11040). diff --git a/src/deploy/functions/release/executor.spec.ts b/src/deploy/functions/release/executor.spec.ts index acfe9a75f04..fb4276bebfd 100644 --- a/src/deploy/functions/release/executor.spec.ts +++ b/src/deploy/functions/release/executor.spec.ts @@ -1,6 +1,7 @@ import { expect } from "chai"; import { FirebaseError } from "../../../error"; +import RetriesExhaustedError from "../../../throttler/errors/retries-exhausted-error"; import * as executor from "./executor"; describe("Executor", () => { @@ -104,6 +105,40 @@ describe("Executor", () => { expect(result).to.equal("done"); expect(attempts).to.equal(2); }); + + it("drains remaining queued tasks when active tasks exhaust retries under bounded concurrency", async () => { + const boundedExec = new executor.QueueExecutor({ + concurrency: 2, + retries: 1, + backoff: 0, + maxBackoff: 1, + }); + const attempts: Record = {}; + const failingTask = (label: string) => (): Promise => { + attempts[label] = (attempts[label] || 0) + 1; + const err = new FirebaseError(`Quota exceeded for ${label}`, { status: 429 }); + return Promise.reject(err); + }; + + const results = await Promise.allSettled([ + boundedExec.run(failingTask("fn-1")), + boundedExec.run(failingTask("fn-2")), + boundedExec.run(() => Promise.resolve("fn-3-ok")), + boundedExec.run(() => Promise.resolve("fn-4-ok")), + ]); + + expect(attempts).to.deep.equal({ "fn-1": 2, "fn-2": 2 }); + expect(results[0].status).to.equal("rejected"); + if (results[0].status === "rejected") { + expect(results[0].reason).to.be.instanceOf(RetriesExhaustedError); + } + expect(results[1].status).to.equal("rejected"); + if (results[1].status === "rejected") { + expect(results[1].reason).to.be.instanceOf(RetriesExhaustedError); + } + expect(results[2]).to.deep.equal({ status: "fulfilled", value: "fn-3-ok" }); + expect(results[3]).to.deep.equal({ status: "fulfilled", value: "fn-4-ok" }); + }); }); describe("isServiceAccountPropagationError", () => { diff --git a/src/throttler/throttler.spec.ts b/src/throttler/throttler.spec.ts index f494a6266a1..d6185d1001c 100644 --- a/src/throttler/throttler.spec.ts +++ b/src/throttler/throttler.spec.ts @@ -398,6 +398,71 @@ const throttlerTest = (ThrottlerConstructor: ThrottlerConstructorType): void => expect(q.retried).to.equal(1); expect(q.total).to.equal(2); }); + + it("should continue processing queued tasks after a task fails with RetriesExhaustedError", async () => { + const handler = (task: string): Promise => { + if (task === "fail-task") { + return Promise.reject(TEST_ERROR); + } + return Promise.resolve(`completed:${task}`); + }; + const q = new ThrottlerConstructor({ + handler, + concurrency: 1, + retries: 1, + backoff: 0, + }); + + const results = await Promise.allSettled([ + q.run("fail-task"), + q.run("queued-task-1"), + q.run("queued-task-2"), + ]); + + expect(results[0].status).to.equal("rejected"); + if (results[0].status === "rejected") { + expect(results[0].reason).to.be.instanceOf(RetriesExhaustedError); + } + expect(results[1]).to.deep.equal({ status: "fulfilled", value: "completed:queued-task-1" }); + expect(results[2]).to.deep.equal({ status: "fulfilled", value: "completed:queued-task-2" }); + expect(q.complete).to.equal(3); + expect(q.success).to.equal(2); + expect(q.errored).to.equal(1); + expect(q.active).to.equal(0); + }); + + it("should continue processing queued tasks after a task fails with TimeoutError", async () => { + const handler = (task: string): Promise => { + if (task === "slow-task") { + return new Promise(() => { + // Intentionally never resolves so Throttler's timeout triggers without leaving a timer. + }); + } + return Promise.resolve(`completed:${task}`); + }; + const q = new ThrottlerConstructor({ + handler, + concurrency: 1, + retries: 0, + }); + + const results = await Promise.allSettled([ + q.run("slow-task", 10), + q.run("fast-task-1", 500), + q.run("fast-task-2", 500), + ]); + + expect(results[0].status).to.equal("rejected"); + if (results[0].status === "rejected") { + expect(results[0].reason).to.be.instanceOf(TimeoutError); + } + expect(results[1]).to.deep.equal({ status: "fulfilled", value: "completed:fast-task-1" }); + expect(results[2]).to.deep.equal({ status: "fulfilled", value: "completed:fast-task-2" }); + expect(q.complete).to.equal(3); + expect(q.success).to.equal(2); + expect(q.errored).to.equal(1); + expect(q.active).to.equal(0); + }); }; describe("Throttler", () => { diff --git a/src/throttler/throttler.ts b/src/throttler/throttler.ts index 0200452aecb..9943240b20a 100644 --- a/src/throttler/throttler.ts +++ b/src/throttler/throttler.ts @@ -298,6 +298,7 @@ export abstract class Throttler { } this.cleanupTask(cursorIndex); this.finish(error); + this.process(); } private cleanupTask(cursorIndex: number): void {