Amplitude-Kotlin icon indicating copy to clipboard operation
Amplitude-Kotlin copied to clipboard

Race condition when using multiple instances of Amplitude.

Open d-mokliakov opened this issue 2 years ago • 5 comments

Expected Behavior

Events from two or more instances of Amplitude are sent to the respective endpoints.

Current Behavior

When 2 instances of Amplitude are used in the same project, a condition occurs when both instances use StorageProviders initialized to work in the same directory. Thus, a situation arises when events from one instance go to another.

Possible Solution

Override storageProvider in configuration to use different prefixes for each instance, but it doesn't seem to be a clear solution.

or

Change internal logic of library to use instanceName from config as part of prefix to initialize storageProvider.

Steps to Reproduce

  1. Initialize 2 or more instances of Amplitude with different configurations (but keep default storageProvider).
  2. Start sending events (send events to both instances at the same time).
  3. Watch as events are sent to the wrong endpoints.

Environment

  • Device: Google Pixel 6 Pro
  • Device OS and Version: Android 13

d-mokliakov avatar Mar 10 '23 13:03 d-mokliakov

Hi @dmoklyakov, thanks for choosing amplitude. What version of the Kotlin SDKs are you using?

yuhao900914 avatar Mar 13 '23 18:03 yuhao900914

Hi @yuhao900914! I'm using version 1.7.1.

d-mokliakov avatar Mar 13 '23 18:03 d-mokliakov

Is there an update on this, or any workarounds? I believe I am also hitting this issue

kennedyjosh avatar Apr 04 '23 19:04 kennedyjosh

@kennedyjosh you can use my 1st suggested possible solution as workaround. You just need to pass storageProvider to Amplitude configuration like this:

storageProvider = object: StorageProvider {
    override fun getStorage(
        amplitude: Amplitude,
        ignored: String?
    ): Storage {
            return AndroidStorage(
                context,
                amplitude.configuration.apiKey,
                amplitude.configuration.loggerProvider.getLogger(amplitude),
                yourCustomPrefix // Should be a distinct string for each Amplitude instance.
            )
        }
    }

d-mokliakov avatar Apr 05 '23 13:04 d-mokliakov

@kennedyjosh you can use my 1st suggested possible solution as workaround

perhaps my version of the SDK (1.5.2) does not have the same API, as in this version it seems AndroidStorage only takes 2 parameters: context and apiKey.

Examining the source code for my version, it seems the prefixes are different (it uses the api key) so I don't think I am having the same issue as you, probably just a similar one. Examining each instance's StorageProvider instances using the debugger confirms they are accessing separate files in separate directories.

kennedyjosh avatar Apr 05 '23 22:04 kennedyjosh