Repository navigation
fix(database): serialize push ID generation - #16768
Conversation
Using Gemini Code AssistThe 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
Customization To customize the 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 Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. 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. |
|
Thanks for the contribution! A few housekeeping items:
|
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces thread safety to the generation of Realtime Database push IDs in FNextPushId.m by utilizing an NSLock, and adds a concurrent unit test to verify that push IDs generated concurrently for the same timestamp remain unique. The review feedback suggests a minor improvement to use [id copy] instead of [NSString stringWithString:id] for better idiomatic efficiency in Objective-C.
paulb777
left a comment
There was a problem hiding this comment.
Thanks, this is a real race: childByAutoId calls +[FNextPushId get:] synchronously on the caller's thread, and the static state isn't synchronized, so concurrent calls can produce duplicate IDs (silently overwriting data) or even an out-of-range characterAtIndex:. Android already makes this method synchronized.
Optional: the CHANGELOG entry could describe the user-visible effect, e.g. - [fixed] Fixed a race where calling childByAutoId() concurrently from multiple threads could generate duplicate push IDs. (#16768)
|
Replaced the manual NSLock/dispatch_once setup with @synchronized([FNextPushId class]) around the whole shared-state update and ID construction, so an Objective-C exception releases the monitor. The fixed class monitor also covers subclasses sharing this static state. Removed the redundant dispatch header and added a bounded regression that verifies a generation exception does not block a later valid call; the 1000-call same-timestamp uniqueness test remains. The CHANGELOG now uses Unreleased and the suggested user-visible description with #16768. Both changed Objective-C files pass clang-format 23.1.2 validation and git diff --check. Apple tests have not run locally; current-head native CI must validate the regression. |
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request fixes a race condition in childByAutoId() by wrapping the push ID generation logic in FNextPushId.m within a @synchronized block, and adds corresponding unit tests to verify thread safety. Feedback on the changes suggests optimizing the ID generation by avoiding the reserved keyword id as a variable name and replacing the inefficient NSMutableString loop with a stack-allocated unichar array.
|
Current-head macOS CI exposed an overly specific assumption in the new exception-release test: constant NSString raises NSInvalidArgumentException for the negative index, while the test named NSRangeException. The regression now asserts NSException and retains the background generation completion timeout and 20-character output check. This tests monitor release without depending on Foundation's exception-name choice. Concurrent uniqueness passed in the captured macOS run; the whole DatabaseUnit run was 431 tests with this one failure. Formatter/diff checks pass for the one-line correction; new-head native CI still needs to finish. |
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request addresses a race condition in childByAutoId() by wrapping the push ID generation logic in FNextPushId.m within a @synchronized block, and adds corresponding unit tests to verify thread safety and exception handling. Feedback on the changes highlights a critical buffer underflow risk when incrementing the random character array, the discouraged use of the reserved keyword 'id' as a variable name in Objective-C, and redundant floor function calls on integer values.
|
Thanks @dajiaohuang! |
Summary
childByAutoId invokes the generator synchronously on the caller's thread. Concurrent calls could interleave reads and updates of process-wide state, so the whole state transition and ID construction now run under one lock. The exception test accepts Foundation's NSException hierarchy rather than assuming one specific exception name.
Validation