Skip to content

[Auth] Fix a race condition - #15951

Merged
paulb777 merged 16 commits into
mainfrom
pb-auth-race
Mar 20, 2026
Merged

[Auth] Fix a race condition#15951
paulb777 merged 16 commits into
mainfrom
pb-auth-race

Conversation

@paulb777

@paulb777 paulb777 commented Mar 19, 2026

Copy link
Copy Markdown
Member

Fix #15950

Gemini Summary

Overview
This PR successfully addresses a thread-safety issue in the FirebaseAuth module. By introducing a private serial dispatch queue (propertyAccessQueue), it ensures mutually exclusive read and write access to critical User properties (like providerDataRaw, uid, email, etc.), effectively mitigating race conditions during concurrent operations such as profile updates, linking, or token unlinking.

What Looks Great
Solid Synchronization Strategy: Utilizing a dedicated serial DispatchQueue (.sync) is a standard, robust Swift pattern for protecting shared mutable state. It cleanly ensures that property access is atomic without adding excessive complexity.

Comprehensive Coverage: The synchronization has been applied methodically across all necessary touchpoints, including read accessors (providerData), mutating functions (update(withGetAccountInfoResponse:)), and serialization (encode(with coder:)).

Strong Testing: The addition of UserThreadSafetyTests.swift is excellent. Firing 500 concurrent reads and writes is a great way to simulate the race condition environment and prove the fix works.

@gemini-code-assist

Copy link
Copy Markdown
Contributor
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

@paulb777

Copy link
Copy Markdown
Member 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 effectively addresses a race condition on the User.providerData property by introducing a serial DispatchQueue to synchronize all read and write access to the underlying providerDataRaw dictionary. The changes are applied consistently across all access points in User.swift and UserProfileUpdate.swift. The addition of UserThreadSafetyTests.swift with a concurrent read/write test is a great way to validate the fix. I have one minor suggestion to make the new test even more robust.

@paulb777

Copy link
Copy Markdown
Member 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 introduces a serial dispatch queue to address a race condition on the User.providerData getter, which is a solid approach to improving thread safety. The protection has been applied to the getter and several methods that modify user properties. While this is a good step, the application of this protection seems incomplete, as some property accesses remain unsynchronized, potentially leading to other race conditions. I've left a couple of comments highlighting these areas. The addition of a new thread safety test is excellent for verifying the fix.

Comment thread FirebaseAuth/Sources/Swift/User/UserProfileUpdate.swift Outdated
Comment thread FirebaseAuth/Sources/Swift/User/User.swift Outdated
@paulb777

Copy link
Copy Markdown
Member 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 effectively addresses a race condition by introducing a serial dispatch queue to protect access to User properties. The synchronization has been applied to various read and write paths, and a new test suite validates the fix under concurrent access. However, the implementation has a minor flaw: the encode(with:) method's synchronization is incomplete, as some properties are still accessed outside the protected block, leaving a potential for race conditions during serialization. I've left a specific comment on this.

Comment thread FirebaseAuth/Sources/Swift/User/User.swift
@paulb777

Copy link
Copy Markdown
Member 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 addresses a race condition in the User class by introducing a serial dispatch queue to synchronize access to its properties. The changes correctly wrap write operations and some read operations in synchronized blocks, which is a solid step towards making the class thread-safe. The addition of UserThreadSafetyTests is also excellent for verifying the fix under concurrent load.

My review includes two main points. The most critical one is that while write access to many properties is now synchronized, read access for most of them remains unsynchronized, which can still lead to data races. I've provided a detailed comment on how to make the class fully thread-safe. I've also included a minor suggestion for a small performance optimization by combining synchronized blocks.

Overall, this is a good fix, but completing the thread-safety implementation is important for correctness.

Comment thread FirebaseAuth/Sources/Swift/User/User.swift
Comment thread FirebaseAuth/Sources/Swift/User/User.swift
@paulb777

Copy link
Copy Markdown
Member 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 effectively addresses a race condition in the User class by introducing a serial dispatch queue to synchronize access to its properties. The changes are well-implemented across property accessors, mutating functions, and serialization. The addition of UserThreadSafetyTests is a great way to validate the fix. I have one suggestion to ensure complete thread safety for all property modifications.

Comment thread FirebaseAuth/Sources/Swift/User/User.swift Outdated
@paulb777

Copy link
Copy Markdown
Member 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 does a good job of addressing a race condition in the User class by introducing a serial dispatch queue to protect property access. The changes are applied consistently across property getters, setters, and serialization methods, and the new thread safety tests are a valuable addition. However, I've identified a critical issue where a nested synchronous dispatch to the same serial queue will cause a deadlock. Please address this to ensure the stability of the fix.

Comment thread FirebaseAuth/Sources/Swift/User/User.swift Outdated
@paulb777

Copy link
Copy Markdown
Member 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 effectively addresses a race condition in the User class by introducing a serial dispatch queue to synchronize access to its properties. The changes are comprehensive, covering property getters, mutation points, and serialization logic. The introduction of UserThreadSafetyTests is a great addition to validate the fix.

I have one suggestion regarding the access level of the new backing properties to further improve encapsulation and guarantee thread safety.

Comment thread FirebaseAuth/Sources/Swift/User/User.swift Outdated
Comment thread FirebaseAuth/Sources/Swift/User/User.swift Outdated
@paulb777

Copy link
Copy Markdown
Member 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 effectively addresses a race condition in the FirebaseAuth module by introducing a private serial dispatch queue (propertyAccessQueue) to synchronize access to critical User properties. The implementation correctly converts properties to computed properties with synchronized getters and setters, and updates all relevant methods to use this queue for thread-safe access. The addition of UserThreadSafetyTests.swift provides excellent validation for the fix under concurrent read/write conditions, demonstrating a thorough approach to ensuring thread safety.

@paulb777
paulb777 marked this pull request as ready for review March 20, 2026 20:34
@paulb777
paulb777 requested a review from morganchen12 March 20, 2026 20:34
@paulb777

Copy link
Copy Markdown
Member Author

Note the ObjC integration test failure is in the nightlies

@paulb777
paulb777 requested a review from ncooke3 March 20, 2026 20:43

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

2 things I'm thinking about:

  • forward/backwards compatibility of data encodings
  • priority inversions/main queue blocking from the sync dispatches

@morganchen12 morganchen12 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 a few suggestions.

Comment thread FirebaseAuth/Sources/Swift/User/User.swift Outdated
Comment thread FirebaseAuth/Sources/Swift/User/User.swift Outdated
Comment thread FirebaseAuth/Sources/Swift/User/User.swift
Comment thread FirebaseAuth/Sources/Swift/User/User.swift

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

LGTM. The coder logic looks safe, and I'm not seeing any nested syncs. I suppose the main thread will block depending on where these User APIs are called from, but such pauses should be very quick.

@paulb777
paulb777 merged commit 25d946b into main Mar 20, 2026
83 checks passed
@paulb777
paulb777 deleted the pb-auth-race branch March 20, 2026 21:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FirebaseAuth.User.providerData getter crashes with SIGTRAP in 12.9.0

3 participants