-
Notifications
You must be signed in to change notification settings - Fork 75
RUMS-5318 Fix race condition in lastFatalAnrSent #3071
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Conversation
Use lazy initialization to prevent concurrent duplicate reads ## Problem Multiple threads could concurrently read `lastFatalAnrSent` as null before any writes occurred, causing duplicate ANR reports to be sent. The property was implemented as a getter that read from disk on every access, allowing race conditions where multiple threads would all see null and attempt to report the same ANR. ## Solution Changed `lastFatalAnrSent` from a property getter to a lazy-initialized property. This ensures the file is read exactly once in a thread-safe manner, and the value is cached in memory for all subsequent accesses.
|
🎯 Code Coverage 🔗 Commit SHA: 43f5e12 | Docs | Datadog PR Page | Was this helpful? Give us feedback! |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## develop #3071 +/- ##
===========================================
+ Coverage 71.16% 71.24% +0.08%
===========================================
Files 878 878
Lines 32129 32130 +1
Branches 5432 5432
===========================================
+ Hits 22862 22889 +27
+ Misses 7714 7707 -7
+ Partials 1553 1534 -19
🚀 New features to boost your workflow:
|
|
@codex review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Codex Review
Here are some automated review suggestions for this pull request.
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| internal val lastFatalAnrSent: Long? by lazy { | ||
| val file = File(storageDir, LAST_FATAL_ANR_SENT_FILE_NAME) | ||
| if (file.existsSafe(internalLogger)) { | ||
| file.readTextSafe(Charsets.UTF_8, internalLogger)?.toLongOrNull() | ||
| } else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cache not refreshed after ANR timestamp writes
The new lazy cache for lastFatalAnrSent (CoreFeature) is only computed once and never updated when writeLastFatalAnrSent/deleteLastFatalAnrSent mutate the backing file. If handleAnrCrash is invoked again in the same process after the first write (e.g., iterating over multiple ApplicationExitInfo entries), the duplicate check will still read the stale cached value (often null) and can emit the same ANR repeatedly. The previous getter reread the file on every access, so the in-memory value always reflected the latest write.
Useful? React with 👍 / 👎.
What does this PR do?
Use lazy initialization to prevent concurrent duplicate reads
Multiple threads could concurrently read
lastFatalAnrSentas null beforeany writes occurred, causing duplicate ANR reports to be sent. The property
was implemented as a getter that read from disk on every access, allowing
race conditions where multiple threads would all see null and attempt to
report the same ANR.
Changed
lastFatalAnrSentfrom a property getter to a lazy-initializedproperty. This ensures the file is read exactly once in a thread-safe manner,
and the value is cached in memory for all subsequent accesses.