[EXTERNAL] fix(events): catch FileNotFoundException in openBufferedReader to prevent TOCTOU crash (#3704) contributed by @tsushanth#3719
Merged
Conversation
…vent TOCTOU crash (#3704) ## Problem Fixes #3696. `EventsFileHelper.readFile` guards with `fileIsEmpty()` before opening the event store file, but `FileHelper.openBufferedReader` then calls `FileInputStream(file)` without re-checking existence. A concurrent flush/rotate that deletes `ad_event_store.jsonl` between `fileIsEmpty()` returning `false` and `FileInputStream(file)` throws an **uncaught `FileNotFoundException`** on the SDK background thread (`PurchasesFactory$LowPriorityThreadFactory`), crashing the app. From the reporter: this is the **#1 fatal crash** on production builds using AdMob/AppLovin ad trackers, with ~53 crashes / ~52 affected users over 30 days, reproduced on Android 14–16. Crash stack: ``` Fatal Exception: java.io.FileNotFoundException: .../event_store/ad_event_store.jsonl: open failed: ENOENT at FileHelper.openBufferedReader(FileHelper.kt:80) at FileHelper.readFilePerLines(FileHelper.kt:39) at EventsFileHelper.readFile(EventsFileHelper.kt:61) at EventsManager.getStoredEvents(EventsManager.kt:422) at EventsManager.flushNextBatch(EventsManager.kt:277) ``` ## Fix Catch `FileNotFoundException` in `openBufferedReader` and treat a missing file as empty — the same semantics `fileIsEmpty()` uses — logging at `DEBUG` level. The same pattern is already used in `removeFirstLinesFromFile` (line 58). ```kotlin } catch (e: FileNotFoundException) { debugLog { "FileHelper: file not found when trying to read: $filePath. Treating as empty." } } ``` Also adds a regression test that calls `readFilePerLines` on a nonexistent path and asserts no exception propagates and the block receives an empty sequence. ## Testing - New unit test in `FileHelperTest` covers the missing-file path. - Existing tests all still pass. <!-- CURSOR_SUMMARY --> --- > [!NOTE] > **Low Risk** > Narrow defensive change in file I/O with empty-file semantics preserved; low blast radius beyond preventing a known production crash path. > > **Overview** > Fixes a **TOCTOU race** where ad/event store reads could crash the app: callers check `fileIsEmpty()` then `readFilePerLines`, but a concurrent flush/rotate can delete the file before `FileInputStream` opens, yielding an uncaught `FileNotFoundException` on the SDK background thread. > > **`FileHelper.openBufferedReader`** now catches `FileNotFoundException`, logs at debug, and treats a missing file as empty (matching `fileIsEmpty()`), aligning with the existing pattern in `removeFirstLinesFromFile`. > > A **regression test** asserts `readFilePerLines` on a nonexistent path does not throw and leaves the consumer with no lines. > > <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 801a0b5. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).</sup> <!-- /CURSOR_SUMMARY -->
ajpallares
approved these changes
Jul 6, 2026
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3719 +/- ##
==========================================
- Coverage 80.54% 80.50% -0.04%
==========================================
Files 400 400
Lines 16595 16598 +3
Branches 2370 2370
==========================================
- Hits 13367 13363 -4
- Misses 2291 2302 +11
+ Partials 937 933 -4 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Problem
Fixes #3696.
EventsFileHelper.readFileguards withfileIsEmpty()before opening the event store file, butFileHelper.openBufferedReaderthen callsFileInputStream(file)without re-checking existence. A concurrent flush/rotate that deletesad_event_store.jsonlbetweenfileIsEmpty()returningfalseandFileInputStream(file)throws an uncaughtFileNotFoundExceptionon the SDK background thread (PurchasesFactory$LowPriorityThreadFactory), crashing the app.From the reporter: this is the #1 fatal crash on production builds using AdMob/AppLovin ad trackers, with ~53 crashes / ~52 affected users over 30 days, reproduced on Android 14–16.
Crash stack:
Fix
Catch
FileNotFoundExceptioninopenBufferedReaderand treat a missing file as empty — the same semanticsfileIsEmpty()uses — logging atDEBUGlevel. The same pattern is already used inremoveFirstLinesFromFile(line 58).Also adds a regression test that calls
readFilePerLineson a nonexistent path and asserts no exception propagates and the block receives an empty sequence.Testing
FileHelperTestcovers the missing-file path.Contributed by @tsushanth in #3704
Note
Low Risk
Narrow I/O hardening with empty-read semantics; affects event/diagnostics file reads but reduces crash risk rather than changing happy-path behavior.
Overview
Fixes a production crash when the ad/event store file disappears between an existence check and opening it for read (e.g. concurrent flush/rotate on the SDK background thread).
FileHelper.openBufferedReadernow wraps theFileInputStreamopen in atry/catchforFileNotFoundException, logs at debug, and does not invoke the read callback—same “missing file = empty” semantics asfileIsEmpty(), aligned with howremoveFirstLinesFromFilealready handled missing files at a higher layer.Adds a
FileHelperTestregression that reads a nonexistent path and asserts no exception and no lines. Removes anEventsFileHelperTestthat expected aREMOVE_LINES_EXCEPTIONdebug event when clearing a missing file, since that path no longer surfacesFileNotFoundExceptionfrom the reader.Reviewed by Cursor Bugbot for commit 9419fba. Bugbot is set up for automated code reviews on this repo. Configure here.