Skip to content

Pintal/clean up dispatcher and is built#363

Closed
polbins wants to merge 2 commits intomainfrom
pintal/clean-up-dispatcher-and-is-built
Closed

Pintal/clean up dispatcher and is built#363
polbins wants to merge 2 commits intomainfrom
pintal/clean-up-dispatcher-and-is-built

Conversation

@polbins
Copy link
Contributor

@polbins polbins commented Mar 5, 2026

Describe what this PR is addressing

Describe the solution

Steps to verify the change

Useful links and documentation (optional)

Checklist

  • Does your PR title have the correct title format?
  • Does your PR have a breaking change?

Note

Medium Risk
Concurrency/initialization ordering changes (removing isBuilt.await() and moving work to storageIODispatcher) could surface race conditions in event/session processing or storage/migration timing, though the changes are localized and covered by updated tests.

Overview
Shifts SDK coroutine execution away from amplitudeDispatcher by deprecating it in core, removing it from the Android wrapper/test helpers, and running core build() and flush() on storageIODispatcher instead.

Removes several isBuilt.await() gates (Android Timeline.start, DefaultEventUtils storage writes, core flush) and simplifies Android reset() to always regenerate the device ID via AndroidContextPlugin. Also makes AndroidContextPlugin lazily initialize its AndroidContextProvider on first use and cleans up a few minor Kotlin style issues.

Written by Cursor Bugbot for commit c8f7209. This will update automatically on new commits. Configure here.

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Bugbot Autofix prepared a fix for the issue found in the latest run.

  • ✅ Fixed: Reset crashes if called before async build completes
    • Added guard to check isBuilt.isCompleted before calling androidContextPlugin.initializeDeviceId(), with fallback to direct setDeviceId() call when build is incomplete.

Create PR

Or push these changes by commenting:

@cursor push 8015690ab3
Preview (8015690ab3)
diff --git a/android/src/main/java/com/amplitude/android/Amplitude.kt b/android/src/main/java/com/amplitude/android/Amplitude.kt
--- a/android/src/main/java/com/amplitude/android/Amplitude.kt
+++ b/android/src/main/java/com/amplitude/android/Amplitude.kt
@@ -128,7 +128,11 @@
 
     override fun reset(): Amplitude {
         setUserId(null)
-        androidContextPlugin.initializeDeviceId(configuration as Configuration, forceRegenerate = true)
+        if (isBuilt.isCompleted) {
+            androidContextPlugin.initializeDeviceId(configuration as Configuration, forceRegenerate = true)
+        } else {
+            setDeviceId(ContextPlugin.generateRandomDeviceId())
+        }
         return this
     }
This Bugbot Autofix run was free. To enable autofix for future PRs, go to the Cursor dashboard.


override fun reset(): Amplitude {
setUserId(null)
if (isBuilt.isCompleted) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reset crashes if called before async build completes

High Severity

The old reset() guarded against calling androidContextPlugin.initializeDeviceId() before the build completed by checking isBuilt.isCompleted and falling back to setDeviceId(ContextPlugin.generateRandomDeviceId()). That guard is now removed. Since androidContextPlugin has a lateinit var amplitude that is only set during setup() (called when the plugin is added to the timeline in buildInternal()), calling reset() before the async build completes will throw an UninitializedPropertyAccessException. Every code path through initializeDeviceId() calls setDeviceId(), which accesses amplitude.

Additional Locations (1)

Fix in Cursor Fix in Web

@polbins polbins closed this Mar 5, 2026
@polbins polbins deleted the pintal/clean-up-dispatcher-and-is-built branch March 5, 2026 20:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant