Repository navigation
fix(functions): prevent deadlock and stale source tokens during deployment retries - #11044
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
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
force-pushed
the
ajp/fix-source-token-retries
branch
2 times, most recently
from
September 6, 2026 20:46
194b3f5 to
67ed495
Compare
joehan
approved these changes
Sep 8, 2026
…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
force-pushed
the
ajp/fix-source-token-retries
branch
from
September 8, 2026 21:53
67ed495 to
cc838e0
Compare
This was referenced Sep 25, 2026
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 ```
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
Sample Commands
npm run test
firebase deploy --only functions:storage-resize-images
firebase functions:delete storage-resize-images