Repository navigation
fix(throttler): keep scheduling queued tasks after a task fails #11060
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,7 @@ | ||
| - [changed] Improve formatting and user experience for Cloud Functions parameter and secret prompts. | ||
| - [fixed] Fail fast with an actionable error and remediation instructions when declarative security APIs (IAM and Cloud Resource Manager) are disabled on the project. | ||
| - Updated Pub/Sub emulator to version 0.8.36. | ||
| - [fixed] Clean up managed service accounts when opting out of declarative security alongside a filtered codebase deploy. | ||
| - ext:uninstall --immediate now warns about secrets bound to the extension that will be deleted and offers a migration path. | ||
| - [fixed] Generate deterministic unsalted ETags for declarative security roles. | ||
| - [fixed] Keep scheduling queued tasks after a task exhausts its retries instead of leaving the rest of the queue unscheduled. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -398,6 +398,56 @@ const throttlerTest = (ThrottlerConstructor: ThrottlerConstructorType): void => | |
| expect(q.retried).to.equal(1); | ||
| expect(q.total).to.equal(2); | ||
| }); | ||
| it("should keep scheduling waiting tasks after a task fails", async () => { | ||
| const handler = sinon.stub(); | ||
| handler.withArgs(1).rejects(TEST_ERROR); | ||
| handler.withArgs(2).resolves(2); | ||
| const q = new ThrottlerConstructor({ | ||
| handler, | ||
| concurrency: 1, | ||
| retries: 0, | ||
| }); | ||
|
|
||
| const first = q.run(1); | ||
| const second = q.run(2); | ||
|
|
||
| let err; | ||
| try { | ||
| await first; | ||
| } catch (e: any) { | ||
| err = e; | ||
| } | ||
| expect(err).to.be.instanceOf(RetriesExhaustedError); | ||
|
|
||
| // Task 2 was queued behind task 1. If the throttler stops scheduling after a | ||
| // failure, this promise never settles. | ||
| const result = await Promise.race([ | ||
| second, | ||
| new Promise((_, reject) => | ||
| setTimeout(() => reject(new Error("task 2 was never scheduled")), 100), | ||
| ), | ||
| ]); | ||
|
Comment on lines
+424
to
+429
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This decides pass/fail on a 100 ms wall-clock timer racing a real promise, and the timer is never cleared when
|
||
| expect(result).to.equal(2); | ||
| expect(handler.callCount).to.equal(2); | ||
| expect(q.complete).to.equal(2); | ||
| expect(q.success).to.equal(1); | ||
| expect(q.errored).to.equal(1); | ||
| }); | ||
| it("should release waiters once they are settled", async () => { | ||
| const handler = sinon.stub().rejects(TEST_ERROR); | ||
| const q = new ThrottlerConstructor({ handler, retries: 0 }); | ||
| q.add(1); | ||
| q.close(); | ||
|
|
||
| let err; | ||
| try { | ||
| await q.wait(); | ||
| } catch (e: any) { | ||
| err = e; | ||
| } | ||
| expect(err).to.be.instanceOf(RetriesExhaustedError); | ||
| expect(q.waits).to.have.length(0); | ||
| }); | ||
| }; | ||
|
|
||
| describe("Throttler", () => { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -228,7 +228,9 @@ export abstract class Throttler<T, R> { | |
| } | ||
|
|
||
| private finish(err?: TaskError): void { | ||
| this.waits.forEach((p) => { | ||
| const waits = this.waits; | ||
| this.waits = []; | ||
| waits.forEach((p) => { | ||
| if (err) { | ||
| return p.reject(err); | ||
| } | ||
|
Comment on lines
+231
to
236
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Clearing I checked with a small script: concurrency 5, task 1 rejects, task 2 resolves, then
I did not run a hosting deploy, but The clearing does not look load-bearing for the main fix, since const waits = this.waits;
this.waits = [];
if (!err) {
this.finished = true;
}
waits.forEach((p) => {
if (err) {
return p.reject(err);
}
return p.resolve();
});With that applied the probe prints |
||
|
|
@@ -298,6 +300,9 @@ export abstract class Throttler<T, R> { | |
| } | ||
| this.cleanupTask(cursorIndex); | ||
| this.finish(error); | ||
| // A failed task frees its concurrency slot just like a fulfilled one. Without this, | ||
| // tasks still waiting behind it are never scheduled and their promises never settle. | ||
| this.process(); | ||
|
Comment on lines
302
to
+305
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With the introduction of Currently, private finish(err?: TaskError): void {
this.waits.forEach((p) => {
if (err) {
return p.reject(err);
}
this.finished = true;
return p.resolve();
});
}Because
Recommendation: private finish(err?: TaskError): void {
this.waits.forEach((p) => {
if (err) {
return p.reject(err);
}
this.finished = true;
return p.resolve();
});
this.waits = [];
}
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in 428c571: finish() now hands the registered waiters off before settling them, with a test asserting they are released.
Comment on lines
+303
to
+305
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Worth a deliberate decision rather than something I would call a bug: the description reasons about From reading the callers (I have not run either deploy path), If halting is still wanted for those callers, it may be better to make that explicit rather than leave it as a side effect of this change. |
||
| } | ||
|
|
||
| private cleanupTask(cursorIndex: number): void { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these five lines belong to other PRs. This branch's merge base is 38 commits behind
origin/main, andmain's CHANGELOG was reset by the 15.31.0 release, so it currently holds just the Cloud Functions prompts line.As it stands, merging would republish entries that already shipped (declarative security APIs, Pub/Sub emulator 0.8.36, managed service account cleanup,
ext:uninstall --immediate, unsalted ETags) in the next release notes. Rebasing onorigin/mainand keeping only the last line should sort it.