Skip to content
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

Make ServiceProvider.getService thread safe #2012

Merged
merged 3 commits into from
Mar 1, 2024

Commits on Mar 1, 2024

  1. create test to prove getService is not thread safe

    Create test to confirm that ServiceBuilder.getService() should return
    the same instance, even when accessed from two threads. To ensure
    the test is consistent we made the constructor slow so the thread both
    end on the same line of code.
    jkasten2 committed Mar 1, 2024
    Configuration menu
    Copy the full SHA
    893381b View commit details
    Browse the repository at this point in the history
  2. make ServiceProvider's serviceMap thread safe

    Add synchronized around all access to ServiceProvider's serviceMap.
    This ensures nothing can modify this map, resulting in multiple
    instance of a class being return unexpectedly.
    
    Another critical path is the call to resolve(this), where this logic
    could also take enough time that could case threading issues that is
    now wrapped in this new lock.
    
    This most likely will fix the real world problem we have been seeing
    where IApplicationService.appContext is null unexpectedly. One
    discovered path is ReceiveReceiptWorker is created from the Android OS
    on a background thread and it calls getService() without first calling
    OneSignal.initWithContext, resulting in a race condition where
    two instance of IApplicationService can be created.
    ReceiveReceiptWorker probably should call OneSignal.initWithContext
    first, but changing this is out of scope for this commit.
    jkasten2 committed Mar 1, 2024
    Configuration menu
    Copy the full SHA
    0e613e3 View commit details
    Browse the repository at this point in the history
  3. simplify ServiceProvider's serviceMap and lock

    No logic changes in this commit
    jkasten2 committed Mar 1, 2024
    Configuration menu
    Copy the full SHA
    3932338 View commit details
    Browse the repository at this point in the history