Skip to content
Closed
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
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.
Comment on lines +2 to +6

Copy link
Copy Markdown
Contributor

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, and main'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 on origin/main and keeping only the last line should sort it.

- [fixed] Keep scheduling queued tasks after a task exhausts its retries instead of leaving the rest of the queue unscheduled.
50 changes: 50 additions & 0 deletions src/throttler/throttler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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 second wins. On a loaded CI runner that is the shape that tends to flake.

createTask earlier in this file gives you an externally resolvable promise plus startExecutePromise, so the test could await task 2 actually starting rather than timing it. That also asserts the thing you care about more directly than a deadline does.

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", () => {
Expand Down
7 changes: 6 additions & 1 deletion src/throttler/throttler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clearing waits here has a knock-on effect on finished. this.finished = true is assigned inside the forEach body, so once finish(error) empties the array, the later no-error finish() from finishIfIdle() iterates nothing and finished stays false for the rest of the run.

I checked with a small script: concurrency 5, task 1 rejects, task 2 resolves, then close() and wait(). Task outcomes are identical either way (complete: 2 success: 1 errored: 1), but the flag differs:

  • merge base: FINISHED FLAG: true
  • this branch: FINISHED FLAG: false

I did not run a hosting deploy, but statusMessage() in src/deploy/hosting/uploader.ts branches on exactly this flag, so after a single hash/populate/upload task exhausts its retries the progress line would stay on "hashing files ..." rather than advancing to the later stages.

The clearing does not look load-bearing for the main fix, since resolve() on an already-rejected waiter was already a no-op. Hoisting the assignment out of the loop keeps your new waits.length === 0 assertion and restores the flag:

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 true again, and throttler.spec.ts, queue.spec.ts and stack.spec.ts come out at 41 passing.

Expand Down Expand Up @@ -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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

With the introduction of this.process() in onTaskFailed, the throttler will now continue to execute remaining tasks after a failure. This means this.finish() can be called multiple times (either via subsequent failures or when the queue eventually becomes idle and calls finishIfIdle()).

Currently, this.finish() iterates over this.waits but never clears the array:

  private finish(err?: TaskError): void {
    this.waits.forEach((p) => {
      if (err) {
        return p.reject(err);
      }
      this.finished = true;
      return p.resolve();
    });
  }

Because this.waits is not cleared, this leads to:

  1. Memory Leak: References to already-settled promises and their handlers are retained in this.waits indefinitely.
  2. Redundant Execution / Double Settling: Subsequent calls to finish() will attempt to resolve or reject the same already-settled promises again.
  3. Inconsistent Behavior: If a caller calls wait() after a failure has occurred but before the queue is idle, that new promise will be resolved successfully when the queue becomes idle, despite the earlier failure.

Recommendation:
Please update this.finish() to clear the this.waits array after settling them:

  private finish(err?: TaskError): void {
    this.waits.forEach((p) => {
      if (err) {
        return p.reject(err);
      }
      this.finished = true;
      return p.resolve();
    });
    this.waits = [];
  }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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 run()-style callers, but this also changes add()-style callers, where a failure previously stopped the queue from scheduling anything further.

From reading the callers (I have not run either deploy path), src/deploy/extensions/release.ts would now keep creating, updating and deleting extension instances after await deploymentPromise has already thrown and saveEtags() was skipped, and the hosting uploader would keep uploading queued files after the deploy errored. In the CLI that window is bounded by the 250 ms in errorOut(), but embedders (library, VS Code, MCP) have no such bound.

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 {
Expand Down
Loading