Repository navigation
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. |
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request addresses a thread-safety issue in Crashlytics where stored on-demand exception paths could be accessed concurrently from multiple queues when data collection is disabled. It introduces thread-safe accessors (takeStoredActiveReportPaths, storedActiveReportPathsSnapshot, and storeActiveReportPath:) synchronized on the stored paths array, and updates the existing report manager to properly clean up these reports in deleteUnsentReports. Additionally, unit tests and mocks have been updated and hardened for concurrency, including adding thread synchronization to FIRCLSMockFileManager. Feedback on the changes suggests clearing self.removeExpectation after capturing it in FIRCLSMockFileManager to prevent potential test failures from over-fulfilling the expectation.
Fixes #16805
Summary
When Crashlytics data collection is disabled,
FIRCLSOnDemandModelstores recorded on-demand exception paths instoredActiveReportPathson thecom.google.firebase.crashlytics.on.demandqueue, whileFIRCLSExistingReportManagerenumerates and clearsstoredActiveReportPathson thecom.google.firebase.crashlytics.startupqueue without synchronization (#16805).While that race is currently unreachable in production due to
#16837(wherecontextInitPromiseblocks public API calls untilstartWithProfilingfinishes), fixing#16837in a follow-up PR will allowrecordOnDemandExceptionModel:andsendUnsentReports/deleteUnsentReportsto run concurrently. Landing this fix first ensuresmainremains thread-safe.This PR also addresses two related edge cases in the same pre-consent on-demand path:
sleep():FIRCrashlyticspreviously evaluated[self.dataArbiter isCrashlyticsCollectionEnabled]upfront before enqueueing ontoFIRCLSOnDemandModel.operationQueue, which callssleep(uploadDelay)for backoff. If a user enabled collection while operations were queued behindsleep(), waking operations saw the staleNOvalue and appended reports tostoredActiveReportPathsafterstartuphad already drained it.deleteUnsentReportscleanup:-[FIRCLSExistingReportManager deleteUnsentReports]previously deleted regular active, processing, and prepared reports, without draining or deletingstoredActiveReportPaths.Changes
FIRCLSOnDemandModel(FIRCLSOnDemandModel_Private.h,FIRCLSOnDemandModel.m):_storedActiveReportPathsprivate andreadonly(allocated once ininit), replacing the exposedNSMutableArrayproperty with-takeStoredActiveReportPathsand-storedActiveReportPathsSnapshot.@synchronized(_storedActiveReportPaths). This acts strictly as an in-memory leaf lock (never held acrosssleep(), disk file deletion, stack unwinding, or cross-queue calls), avoiding stalls or deadlocks while guaranteeing exclusive ownership of each report path on disk.FIRCLSDataCollectionArbiter *inrecordOnDemandExceptionIfQuota:withDataArbiter:usingExistingReportManager:(FIRCrashlytics.m) so each queued operation checks live consent when it executes.FIRCLSExistingReportManager(FIRCLSExistingReportManager.m):sendUnsentReportsWithToken:asUrgent:anddeleteUnsentReportsto atomically drain[self.onDemandModel takeStoredActiveReportPaths].Note on Supporting Changes in Existing Tests & Mocks
While validating the new multi-queue concurrency tests under Thread Sanitizer (
-enableThreadSanitizer YES), a few pre-existing test/mock issues surfaced and are cleaned up in this PR so the TSan pass runs cleanly:FIRCLSMockFileManager.m: Synchronized_fileSystemDict(NSMutableDictionary) andremoveCountwith@synchronized(self)to prevent mock-only dictionary data races whenon.demandeviction andstartupreport processing access the mock file manager concurrently.FIRCLSMockOnDemandModel.m: Switched mock report filename suffixes fromarc4random_uniform(10000)toNSUUIDto prevent filename collisions (birthday paradox) during high-iteration concurrency tests.FIRCLSOnDemandModelTests.m(setUp/tearDown):[contextManager setupContextWithReport:...]call insetUp. All tests in this suite useFIRCLSMockOnDemandModel(which does not read_firclsContext), and callingsetupContextWithReport:per test registered duplicate_dyld_register_func_for_add_imagecallbacks (whichdyldhas no API to unregister) that raced on_firclsContextreallocation between tests under TSan.onDemandModel.operationQueue,managerData.dispatchQueue,existingReportManager.operationQueue),FIRCLSUserDefaultscleanup (so stickysetCrashlyticsCollectionEnabled:settings do not leak into subsequent test suites), andFIRCLSContextBaseDeinit()intearDown.FIRCrashlyticsTests.m:FIRCLSThreadTrackingOnDemandModelfor thewithDataArbiter:selector signature.__block BOOL initSettledrunloop spin-wait insetUpwithXCTestExpectation, resolving a TSan data race oninitSettled.Testing
FIRCLSOnDemandModelTestsandFIRCLSReportManagerTestscovering:recordOnDemandExceptionIfQuota:(with> FIRCLSMaxUnsentReportsevictions) alongsidesendUnsentReportsWithToken:on thestartupqueue (#16805).sleep(), verifying queued operations upload immediately on wakeup instead of stranding instoredActiveReportPaths.deleteUnsentReportsdeleting stored on-demand report paths both directly and viaFIRCLSReportManager.takeStoredActiveReportPaths.FirebaseCrashlyticsUnittest suite (282 tests, 0 failures).FIRCLSOnDemandModelTests,FIRCLSReportManagerTests, andFIRCrashlyticsTestswith Thread Sanitizer (-enableThreadSanitizer YES) (42 tests, 0 failures, 0 TSan warnings).API Changes
None (internal
FIRCLSOnDemandModelprivate category only).