Skip to content

fix(functions): prevent deadlock and stale source tokens during deployment retries - #11044

Merged
ajperel merged 2 commits into
ajp/iam-policy-retryfrom
ajp/fix-source-token-retries
Sep 8, 2026
Merged

ajperel merged 2 commits into
ajp/iam-policy-retryfrom
ajp/fix-source-token-retries

Conversation

@ajperel

@ajperel ajperel commented Sep 6, 2026 •

Copy link
Copy Markdown
Contributor

Description

During Cloud Functions deployments, the fabricator shares a single build across regional function deployments using source tokens and retries failed tasks via QueueExecutor. When an initial deployment task fails transiently (e.g. service account replication lag or quota limits), the retry pipeline previously encountered deadlocks and invalid API requests: in-flight callers waiting on the build token remained blocked on abort, and retried function objects could retain stale tokens or leak undefined properties into GCP update masks.

This change encapsulates source token acquisition in SourceTokenScraper.withToken, ensuring token lifecycle and error handling are managed atomically while keeping internal coordination methods private. It updates abort() to cleanly unblock all concurrent waiters when an in-flight build fails so subsequent retries can initiate a fresh build without hanging. In Fabricator, v1 and v2 deployment operations now run via withToken, safely clearing any stale token from previous attempts and setting it only when a valid build token is present.

This is a stacked change on top of #11043. Implementation wise they are independent, but they are both required to fix flaky kit deploys and best manually tested together if you're looking for a result of not flaky deployments.

Scenarios Tested

  • Unit tests in src/deploy/functions/release/sourceTokenScraper.spec.ts verifying:
    • Immediate token provision and retrieval after poller completion
    • Unblocking all concurrent waiters on abort without hanging
    • Initiating fresh fetches after abort
    • Token refresh after expiration
    • withToken execution on success and automatic abort on error
  • Unit tests in src/deploy/functions/release/fabricator.spec.ts verifying:
    • QueueExecutor retrying createV2Function and recovering from service account 404 errors
    • Token retrieval on abort for v2 create and update operations
    • Existing v1 and v2 deployment and lifecycle tests pass
  • npm run test
  • Manual testing deploying and deleting functions with kits.

Sample Commands

npm run test
firebase deploy --only functions:storage-resize-images
firebase functions:delete storage-resize-images

@gemini-code-assist gemini-code-assist Bot left a comment

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.

Code Review

This pull request refactors the SourceTokenScraper to prevent deadlocks and stale source tokens during Cloud Functions deployment retries. It introduces a new withToken method that wraps deployment operations, ensuring that the token fetch is automatically aborted if an operation throws, and explicitly deletes stale tokens before retrying. Additionally, abort, getToken, and isTokenExpired are made private. The reviewer suggested avoiding the use of any when defining a mock service account error in the unit tests, recommending Object.assign instead to adhere to the repository's style guide.

@ajperel
ajperel force-pushed the ajp/fix-source-token-retries branch 2 times, most recently from 194b3f5 to 67ed495 Compare September 6, 2026 20:46
@ajperel
ajperel requested a review from joehan September 8, 2026 01:04

@shettyvarun268 shettyvarun268 left a comment

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.

LGTM

…yment retries

### Description
During Cloud Functions deployments, the fabricator shares a single build across regional function deployments using source tokens and retries failed tasks via QueueExecutor. When an initial deployment task fails transiently (e.g. service account replication lag or quota limits), the retry pipeline previously encountered deadlocks and invalid API requests: in-flight callers waiting on the build token remained blocked on abort, and retried function objects could retain stale tokens or leak undefined properties into GCP update masks.

This change encapsulates source token acquisition in SourceTokenScraper.withToken, ensuring token lifecycle and error handling are managed atomically while keeping internal coordination methods private. It updates abort() to cleanly unblock all concurrent waiters when an in-flight build fails so subsequent retries can initiate a fresh build without hanging. In Fabricator, v1 and v2 deployment operations now run via withToken, safely clearing any stale token from previous attempts and setting it only when a valid build token is present.

### Scenarios Tested
- Unit tests in src/deploy/functions/release/sourceTokenScraper.spec.ts verifying:
  - Immediate token provision and retrieval after poller completion
  - Unblocking all concurrent waiters on abort without hanging
  - Initiating fresh fetches after abort
  - Token refresh after expiration
  - withToken execution on success and automatic abort on error
- Unit tests in src/deploy/functions/release/fabricator.spec.ts verifying:
  - QueueExecutor retrying createV2Function and recovering from service account 404 errors
  - Token retrieval on abort for v2 create and update operations
  - Existing v1 and v2 deployment and lifecycle tests pass
- npm run test
- Manual testing deploying and deleting functions with kits.

### Sample Commands
npm run test
firebase deploy --only functions:storage-resize-images
firebase functions:delete storage-resize-images
@ajperel
ajperel force-pushed the ajp/fix-source-token-retries branch from 67ed495 to cc838e0 Compare September 8, 2026 21:53
@ajperel
ajperel merged commit a9de625 into main Sep 8, 2026
51 checks passed
@ajperel
ajperel deleted the ajp/fix-source-token-retries branch September 8, 2026 23:08
ajperel added a commit that referenced this pull request Oct 17, 2026
…retries

### Description

Functions deployed to the same region share one source build: the first function builds from source, and the rest wait for its source token and reuse that build. Since #11044, any retryable error on the first function (for example a quota error or a service account propagation delay) abandoned that shared build. Every waiting function then built from source in parallel, even when the retry succeeded, which slows down large deploys and uses more build quota.

Waiting functions now stay on the shared build while the first function retries. Only a terminal failure of the function that is building releases them. `SourceTokenScraper.withToken` now wraps the whole retried deploy, and the token is fetched at the start of each attempt, so tokens that expire during a backoff are still refreshed. This change also:

- Stops a failed build from being treated as a finished shared build.
- Gives the v2 Cloud Run quota delete-and-recreate the executor's backoff and retry limit instead of an unbounded loop.
- Stops v2 deploys from waiting on a shared build when `functionsv2deployoptimizations` is disabled.

### Scenarios Tested

- Unit tests for `SourceTokenScraper` and `Fabricator` cover the paths that decide whether a build is shared:
  - The first function retrying and then succeeding, or exhausting its retries.
  - Failures from other functions while a build is in flight.
  - Token expiry during retries.
  - Failed operations with and without a token.
  - Cloud Run quota retries.
  - The disabled experiment.
- Checked that each new test fails when the fix it covers is reverted.
- `npm run test`.
- Manually deployed a codebase with many functions in one region.

### Sample Commands

```bash
firebase deploy --only functions
```
ajperel added a commit that referenced this pull request Oct 17, 2026
…retries

### Description

Functions deployed to the same region share one source build: the first function builds from source, and the rest wait for its source token and reuse that build. Since #11044, any retryable error on the first function (for example a quota error or a service account propagation delay) abandoned that shared build. Every waiting function then built from source in parallel, even when the retry succeeded, which slows down large deploys and uses more build quota.

Waiting functions now stay on the shared build while the first function retries. Only a terminal failure of the function that is building releases them. `SourceTokenScraper.withToken` now wraps the whole retried deploy, and the token is fetched at the start of each attempt, so tokens that expire during a backoff are still refreshed. This change also:

- Stops a failed build from being treated as a finished shared build.
- Gives the v2 Cloud Run quota delete-and-recreate the executor's backoff and retry limit instead of an unbounded loop.
- Stops v2 deploys from waiting on a shared build when `functionsv2deployoptimizations` is disabled.

### Scenarios Tested

- Unit tests for `SourceTokenScraper` and `Fabricator` cover the paths that decide whether a build is shared:
  - The first function retrying and then succeeding, or exhausting its retries.
  - Failures from other functions while a build is in flight.
  - Token expiry during retries.
  - Failed operations with and without a token.
  - Cloud Run quota retries.
  - The disabled experiment.
- Checked that each new test fails when the fix it covers is reverted.
- `npm run test`.
- Manually deployed a codebase with many functions in one region.

### Sample Commands

```bash
firebase deploy --only functions
```
ajperel added a commit that referenced this pull request Oct 17, 2026
…retries

### Description

Functions deployed to the same region share one source build: the first function builds from source, and the rest wait for its source token and reuse that build. Since #11044, any retryable error on the first function (for example a quota error or a service account propagation delay) abandoned that shared build. Every waiting function then built from source in parallel, even when the retry succeeded, which slows down large deploys and uses more build quota.

Waiting functions now stay on the shared build while the first function retries. Only a terminal failure of the function that is building releases them. `SourceTokenScraper.withToken` now wraps the whole retried deploy, and the token is fetched at the start of each attempt, so tokens that expire during a backoff are still refreshed. This change also:

- Stops a failed build from being treated as a finished shared build.
- Gives the v2 Cloud Run quota delete-and-recreate the executor's backoff and retry limit instead of an unbounded loop.
- Stops v2 deploys from waiting on a shared build when `functionsv2deployoptimizations` is disabled.

### Scenarios Tested

- Unit tests for `SourceTokenScraper` and `Fabricator` cover the paths that decide whether a build is shared:
  - The first function retrying and then succeeding, or exhausting its retries.
  - Failures from other functions while a build is in flight.
  - Token expiry during retries.
  - Failed operations with and without a token.
  - Cloud Run quota retries.
  - The disabled experiment.
- Checked that each new test fails when the fix it covers is reverted.
- `npm run test`.
- Manually deployed a codebase with many functions in one region.

### Sample Commands

```bash
firebase deploy --only functions
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants