Skip to content

fix(gcp): retry on IAM replication lag and etag conflicts in resourceManager - #11043

Merged
ajperel merged 4 commits into
mainfrom
ajp/iam-policy-retry
Sep 8, 2026
Merged

ajperel merged 4 commits into
mainfrom
ajp/iam-policy-retry

Conversation

@ajperel

@ajperel ajperel commented Sep 5, 2026

Copy link
Copy Markdown
Contributor

Description

Resource Manager setIamPolicy calls can fail transiently during service account provisioning due to replication lag (HTTP 400 INVALID_ARGUMENT with message "Service account ... does not exist.") and optimistic concurrency control conflicts (HTTP 409 ABORTED).

This change adds retryWithBackoff to src/utils.ts (migrating backoff calculation utilities from throttler to avoid circular dependencies) and wraps addServiceAccountToRoles in src/gcp/resourceManager.ts with exponential backoff retries. Retrying with backoff allows IAM replication to settle for newly created service accounts (resolving 400 errors) and re-fetches the policy to obtain a fresh etag (resolving 409 conflicts).

Scenarios Tested

  • Unit tests in src/utils.spec.ts verifying timeToWait and retryWithBackoff with successful runs, predicate retries, fast failures, and retry exhaustion.
  • Unit tests in src/gcp/resourceManager.spec.ts verifying:
    • Retry on HTTP 400 "does not exist"
    • Retry on HTTP 409 conflict with etag refresh
    • Fast failure on HTTP 403 permission errors without retrying
    • Fast failure on HTTP 404 not found errors without retrying
  • Existing throttler and run tests pass.
  • npm run test
  • Manual testing with repeated deploy and delete of kits verifying consistent success.

Sample Commands

npx mocha src/utils.spec.ts src/throttler/throttler.spec.ts src/gcp/resourceManager.spec.ts
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 introduces a generic retryWithBackoff utility in src/utils.ts (migrating exponential backoff helpers from the throttler module) and applies it to addServiceAccountToRoles in src/gcp/resourceManager.ts to handle transient IAM replication delays (HTTP 400) and concurrency conflicts (HTTP 409). The review feedback suggests optimizing addServiceAccountToRoles by moving the service account lookup (getServiceAccount) outside of the retry loop to prevent redundant API calls on retry.

Comment thread src/gcp/resourceManager.ts Resolved

@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.

Clean fix Andy! LGTM

Comment thread src/gcp/resourceManager.ts Resolved
@ajperel
ajperel requested a review from joehan September 8, 2026 01:03

@joehan joehan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Logic seems fine to me, but a few minor improvments to make before merging.

Comment thread src/gcp/iam.spec.ts
};

describe("iam", () => {
afterEach(() => {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Note since I had to sanity check this - afterEach works downwards thru nested describes, so this still executes after every case in each inner describe

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.

Yes. This change is actually independent of my other changes. While running these tests a lot Jetski root caused some flakiness because some of the nested describe's had forgotten to call nock.cleanAll() in afterEach and could pollute future tests. So this fixes some small amount of the general CLI test flakiness.

Comment thread src/gcp/resourceManager.ts Resolved
Comment thread src/gcp/resourceManager.ts Outdated
for (const roleName of roles) {
let binding = projectPolicy.bindings.find((b: Binding) => b.role === roleName);

// create a new binding if the role doesn't exist in the policy yet

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we use the existing helper function iam.mergeBindings here instead of reimplementing this create or update logic?

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! I guess Thomas's AI didn't find that and mine didn't think to fix it. But I love using helpers where they exist.

…Manager

Resource Manager setIamPolicy calls can fail transiently during service account provisioning due to replication lag (HTTP 400 INVALID_ARGUMENT with message "Service account ... does not exist.") and optimistic concurrency control conflicts (HTTP 409 ABORTED).

This change adds retryWithBackoff to src/utils.ts (migrating backoff calculation utilities from throttler to avoid circular dependencies) and wraps addServiceAccountToRoles in src/gcp/resourceManager.ts with exponential backoff retries. Retrying with backoff allows IAM replication to settle for newly created service accounts (resolving 400 errors) and re-fetches the policy to obtain a fresh etag (resolving 409 conflicts).

- Unit tests in src/utils.spec.ts verifying timeToWait and retryWithBackoff with successful runs, predicate retries, fast failures, and retry exhaustion.
- Unit tests in src/gcp/resourceManager.spec.ts verifying:
  - Retry on HTTP 400 "does not exist"
  - Retry on HTTP 409 conflict with etag refresh
  - Fast failure on HTTP 403 permission errors without retrying
  - Fast failure on HTTP 404 not found errors without retrying
- Existing throttler and run tests pass.
- npm run test
- Manual testing with repeated deploy and delete of kits verifying consistent success.

npx mocha src/utils.spec.ts src/throttler/throttler.spec.ts src/gcp/resourceManager.spec.ts
npm run test
firebase deploy --only functions:storage-resize-images
firebase functions:delete storage-resize-images
…llution

### Description
Move nock.cleanAll() to the top-level describe("iam") afterEach hook to ensure mock interceptors from all suites (including testIamPermissions) are cleaned up, preventing test flakiness and unconsumed mock pollution in subsequent tests.

### Scenarios Tested
- Unit tests in src/gcp/iam.spec.ts pass.
- npm run lint:changed-files passes with 0 errors.
@ajperel
ajperel force-pushed the ajp/iam-policy-retry branch from e5841ac to 69762a6 Compare September 8, 2026 21:43
@ajperel
ajperel merged commit 67297e8 into main Sep 8, 2026
51 checks passed
@ajperel
ajperel deleted the ajp/iam-policy-retry branch September 8, 2026 23:08

@ajperel ajperel left a comment •

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.

[Automated review generated in error during local evaluation harness testing — retracted]

@ajperel ajperel left a comment •

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.

[Automated review generated in error during local evaluation harness testing — retracted]

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