Repository navigation
Implement exponential backoff for RTDB Desktop - #1983
Merged
Merged
Conversation
…ate, and unlocked callback destructor test
Contributor
There was a problem hiding this comment.
Code Review
This pull request introduces a new RetryHelper class to manage connection retries with exponential backoff and random jitter on the desktop client, matching the behavior of Android and iOS RTDB clients. It integrates RetryHelper into PersistentConnection, tracks token fetch attempts to ignore outdated results, and adds comprehensive unit tests for the new retry logic. There are no review comments, and we have no feedback to provide.
…ation, and move-and-clear pending futures
✅ Integration test succeeded!Requested by @dconeybe on commit 0c31588 |
…x LevelDB priority reconstruction
…t, and extract OnTokenFutureComplete
… jitter bound and warnings in retry_helper_test
Base automatically changed from
dconeybe/fix-desktop-persistence-across-instances
to
main
November 26, 2026 02:04
…cap max delay at INT_MAX, guard NaN/float-equal, update docs
…se, cache result(), release futures outside lock, log App Check errors
… tests, and add edge-case coverage
AustinBenoit
approved these changes
Nov 26, 2026
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.
This PR implements exponential backoff with random jitter for Realtime Database (RTDB) Desktop reconnection attempts, matching the Android and iOS SDK behavior to prevent tight reconnect loops. It introduces a thread-safe
RetryHelperutility, integrates it intoPersistentConnectionalongside hardened asynchronous token-fetch lifecycle handling, and adds unit tests and release notes.Fixes: #1971
Highlights
RetryHelper): AddedRetryHelperto schedule reconnection retries onscheduler::Schedulerusing exponential backoff (1 s initial delay after failure, 1.3x exponent, 30 s default cap, 0.7 jitter factor) with an immediate 0 ms attempt after a successful connection, matching Android and iOS RTDB retry behavior.PersistentConnectionIntegration: Replaced immediate reconnection scheduling inPersistentConnectionwithRetryHelper. Backoff resets on server resets or connections established for longer than 30 seconds, pending retries are cancelled and reset on interrupts, and repeated invalid auth token responses jump directly to the maximum retry delay.OnTokenFutureCompleteusingSafeReferenceand monotonic attempt counters so stale token results from interrupted or superseded attempts are ignored, futures are released outside locks, and null pointers / App Check token errors are handled safely.RetryHelperwithSafeReferencesynchronization, monotonic task IDs, unlocked callback execution and handle cancellation to avoid deadlocks or lock-order inversion, and immediate release of captured callbacks on cancellation, superseding, or destruction.RetryHelpercovering backoff curves, jitter bounds, cancellation, re-entrancy, and concurrent destruction, and added an entry to the upcoming release notes.Changelog
RetryHelperclass declaration andRetryHelper::Optionswith default backoff constants (1000 ms min delay, 30000 ms max delay, 1.3 exponent, 0.7 jitter factor),kMaxSupportedRetryDelayMs(INT_MAX) bound, and optional customRandomGenerator.Retry(),SignalSuccess(),SetMaxDelay(), andCancel()methods along withRetryHelperTestPeertest inspection accessors.RetryHelperexponential backoff and jitter calculations, including non-blocking RNG seeding (SeedRng) and defensive handling ofNaN, overflow, and closed upper-bound[0.0, 1.0]RNG outputs.SafeReference-guarded scheduling with monotonic task IDs, unlocked callback execution andRequestHandle::Cancel()calls, and immediate release of stored callbacks on cancel, supersede, or destruction.RetryHelpermember,last_connection_established_time_(steady_clock::time_point), and token attempt tracking (current_get_token_attempt_,pending_get_token_attempt_).PendingTokenstruct,ClearPendingTokenFutures(), and unifiedOnTokenFutureComplete()replacing separate Auth and App Check static callbacks.OnReady()and calledretry_helper_.SignalSuccess()inOnDisconnect()on server reset or when a connection lasted longer than 30 seconds.InterruptInternal()and~PersistentConnection()to cancel pending retries and clear pending token futures, and updatedHandleAuthTokenResponse()to callretry_helper_.SetMaxDelay()when invalid auth tokens hit the threshold.TryScheduleReconnect()to schedule attempts viaretry_helper_.Retry()withShouldReconnect()and state checks.SafeReferencecallbacks, ignore staleget_token_attemptresults, release moved futures outsidepending_token_future_mutex_, guard nullerror_message()/result()pointers, and log App Check token errors.src/desktop/connection/retry_helper.cctodesktop_SRCS.firebase_rtdb_desktop_connection_retry_helper_testtest target.SignalSuccess(),SetMaxDelay(),Cancel(), overlapping retries, re-entrantRetry()/Cancel(), safe destruction during pending or in-flight callbacks, immediate callback resource release, RNG clamping and default RNG bounds, scheduled delay timing, and empty callbacks.