Skip to content

fix(functions): retry on HTTP 400 service account propagation errors during function creation - #11047

Merged
ajperel merged 3 commits into
ajp/fix-source-token-retriesfrom
ajp/fix-sa-propagation-errors
Sep 8, 2026
Merged

ajperel merged 3 commits into
ajp/fix-source-token-retriesfrom
ajp/fix-sa-propagation-errors

Conversation

@ajperel

@ajperel ajperel commented Sep 6, 2026

Copy link
Copy Markdown
Contributor

Description

When deploying Cloud Functions with declarative security, the CLI provisions a dedicated managed service account (firebase-fn-[0-9]+@<project>.iam.gserviceaccount.com). While the service account is created and granted roles prior to function deployment, IAM propagation is eventually consistent across Google Cloud backend services.

When creating event-triggered Cloud Functions (such as Eventarc triggers), downstream validation can fail if the newly provisioned service account has not yet replicated to the trigger service. Because the trigger validator treats an unpropagated service account as an invalid request parameter rather than a missing URL resource, the API returns HTTP 400 (INVALID_ARGUMENT: "The request was invalid: invalid service account ... provided") rather than HTTP 404 (NOT_FOUND).

Previously, executor.isServiceAccount404 only recognized HTTP 404 errors, causing function creation to fail immediately on HTTP 400 without retrying.

This change updates isServiceAccount404 in src/deploy/functions/release/executor.ts to retry HTTP 400 errors caused by service account propagation delays. To prevent false positives and avoid retrying when a developer has made a typo in a custom service account configuration, HTTP 400 retries are specifically restricted to dynamically provisioned declarative security service accounts matching firebase-fn-[0-9]+@.

Scenarios Tested

  • Unit tests in src/deploy/functions/release/executor.spec.ts verifying:
    • Retry on HTTP 404 and HTTP 400 service account propagation errors for firebase-fn-[0-9]+@ accounts
    • Immediate failure on HTTP 400 for user-configured custom service accounts without retrying
  • Unit tests in src/deploy/functions/release/fabricator.spec.ts verifying:
    • QueueExecutor retries createV2Function and succeeds when encountering HTTP 400 service account propagation errors
  • npm run test
  • Manual testing deploying and deleting functions with kits.

Sample Commands

firebase deploy --only functions:storage-resize-images

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

Gemini encountered an error creating the review. You can try again by commenting /gemini review.

@ajperel

ajperel commented Sep 6, 2026

Copy link
Copy Markdown
Contributor Author

/gemini review

@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 deployment process to prevent deadlocks and stale source tokens during Cloud Functions deployment retries by introducing a withToken wrapper in SourceTokenScraper that automatically aborts on failure. Additionally, it updates the deployment executor to retry Cloud Functions creation on HTTP 400 errors caused by service account propagation delays, specifically targeting dynamically provisioned service accounts. The reviewer suggested adding a defensive guard in isServiceAccount404 to prevent potential runtime TypeError exceptions when the error object is null or undefined.

Comment on lines +35 to +36
export const isServiceAccount404: RetryPredicate = (err: any): boolean => {
if (parseErrorCode(err) !== 404) {
const code = parseErrorCode(err);

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

To prevent potential runtime TypeError exceptions when err is null or undefined, we should add a defensive guard at the beginning of isServiceAccount404 before calling parseErrorCode(err).

Suggested change
export const isServiceAccount404: RetryPredicate = (err: any): boolean => {
if (parseErrorCode(err) !== 404) {
const code = parseErrorCode(err);
export const isServiceAccount404: RetryPredicate = (err: any): boolean => {
if (!err) {
return false;
}
const code = parseErrorCode(err);

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.

This is pre-existing problem to my change and its' also true of all the error handling classes in this file. It's good feedback, but we should instead do a separate PR that cleans this up overall and likely moves a bunch of this into error.ts as it's pretty generic. So unless someone really pushes the issue I won't do anything here.

In practice, having run 70+ deployments to test this code and flakiness removal, in practice it's always been fine and I don't think a null/undefined/non-object value is every being passed in.

@ajperel
ajperel force-pushed the ajp/fix-sa-propagation-errors branch from bb09ce9 to a50cc3b Compare September 6, 2026 21:25
@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 with some minor non-blocking comments

…during function creation

### Description
When deploying Cloud Functions with declarative security, the CLI provisions a dedicated managed service account (`firebase-fn-[0-9]+@<project>.iam.gserviceaccount.com`). While the service account is created and granted roles prior to function deployment, IAM propagation is eventually consistent across Google Cloud backend services.

When creating event-triggered Cloud Functions (such as Eventarc triggers), downstream validation can fail if the newly provisioned service account has not yet replicated to the trigger service. Because the trigger validator treats an unpropagated service account as an invalid request parameter rather than a missing URL resource, the API returns HTTP 400 (INVALID_ARGUMENT: "The request was invalid: invalid service account ... provided") rather than HTTP 404 (NOT_FOUND).

Previously, `executor.isServiceAccount404` only recognized HTTP 404 errors, causing function creation to fail immediately on HTTP 400 without retrying.

This change updates `isServiceAccount404` in `src/deploy/functions/release/executor.ts` to retry HTTP 400 errors caused by service account propagation delays. To prevent false positives and avoid retrying when a developer has made a typo in a custom service account configuration, HTTP 400 retries are specifically restricted to dynamically provisioned declarative security service accounts matching `firebase-fn-[0-9]+@`.

### Scenarios Tested
- Unit tests in `src/deploy/functions/release/executor.spec.ts` verifying:
  - Retry on HTTP 404 and HTTP 400 service account propagation errors for `firebase-fn-[0-9]+@` accounts
  - Immediate failure on HTTP 400 for user-configured custom service accounts without retrying
- Unit tests in `src/deploy/functions/release/fabricator.spec.ts` verifying:
  - QueueExecutor retries `createV2Function` and succeeds when encountering HTTP 400 service account propagation errors
- npm run test
- Manual testing deploying and deleting functions with kits.

### Sample Commands
firebase deploy --only functions:storage-resize-images
@ajperel
ajperel force-pushed the ajp/fix-sa-propagation-errors branch from a50cc3b to a6565f6 Compare September 8, 2026 22:05
@ajperel
ajperel merged commit 17d1150 into main Sep 8, 2026
51 checks passed
@ajperel
ajperel deleted the ajp/fix-sa-propagation-errors branch September 8, 2026 23:08
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