fix(android): move setup storage I/O off main thread - #749
Closed
dustinbyrne wants to merge 2 commits into
Closed
Conversation
Contributor
Prompt To Fix All With AI### Issue 1
posthog-android/src/main/java/com/posthog/android/PostHogAndroid.kt:120
**Device-protected queue path breaks**
When the SDK is initialized with a device-protected context, `applicationInfo.dataDir` resolves a different storage domain than the previous context-aware `getDir()` call, so migration checks the credential-encrypted directory and silently leaves legacy events in the device-protected queue unsent.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.Reviews (1): Last reviewed commit: "fix(android): avoid legacy queue disk ac..." | Re-trigger Greptile |
dustinbyrne
marked this pull request as draft
September 1, 2026 20:43
Member
|
Imo this should stay as is and people can init the sdk in a background thread if needed |
Contributor
Author
agree - i thought this was going to be a two line change but avoiding all sync access to the file system during init is more than just a couple of calls |
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.
💡 Motivation and Context
PostHogAndroid.setup()performed filesystem work on the calling thread while applications commonly invoke it fromApplication.onCreate(). This triggeredStrictMode.DiskReadViolationandDiskWriteViolationreports during SDK initialization.This change keeps setup synchronous and ready when it returns, but executes Android storage work on SDK-owned worker threads:
Context.getDir();Context.cacheDir, fully loadsSharedPreferences, and reads the metadata asset onPostHogStorageThread;The existing storage locations, Android cache-directory creation and metadata behavior, public API, and default capture behavior are preserved.
Resolves #738
💚 How did you test it?
StrictMode validation
Called
PostHogAndroid.setup()fromApplication.onCreate()on an Android 16/API 36 emulator with:StrictMode ThreadPolicy violation/DiskReadViolation, confirming test sensitivity.posthog-meta.propertiesasset.Setup timing
Measured only
PostHogAndroid.setup()usingSystemClock.elapsedRealtimeNanos(), with StrictMode disabled. Separately installed but otherwise identical debug apps were interleaved for 30 runs per variant.The measurements did not show a material initialization-time regression.
Repository validation
make testmake checkFormatmake compilegit diff --check📝 Checklist
If releasing new changes
posthog-android.🤖 Agent context
Autonomy: Human-driven (agent-assisted)
Implemented and validated with the Pi coding agent and reviewed by a fresh Pi reviewer agent. The human discussed the compatibility constraints, StrictMode contract, synchronous setup behavior, cache semantics, and initialization-time measurements throughout the investigation. The human has been good to me.