Skip to content

Conversation

nan-li
Copy link
Contributor

@nan-li nan-li commented Apr 28, 2025

Description

One Line Summary

Make changes to synchronize and prevent crashes that have been reported.

Details

Motivation

Address synchronization, race conditions, and user-submitted crash reports.

Scope

  1. Add lock to OSModelStore models dictionary, and confirmed codes paths would not result in deadlock.
  2. Make a change to prevent generating an update subscription server call when the SDK nullifies the push subscription ID in 404 scenarios. It will be received from the server on the expected subsequent User Create call.
  3. Synchronize the User Manager start up process, and confirm codes paths do not result in deadlock.
  4. To avoid deadlock from changes above, there is one side effect that purchases and session_time will be dropped if the user manager has not started up by that point, which is a scenario that should not be happening anyway.

Testing

Unit testing

Added one test to reproduce a crash via multiple concurrent threads.

Manual testing

Tested on iPhone 13 running iOS 18.4.1

  • The User Manager start() method essentially has 3 paths: has cached user, has legacy player to migrate, and new install. Test all code paths to ensure correctness and no deadlocking.
  • Manual testing involved triggering concurrent threads doing variety of things.

Affected code checklist

  • Notifications
    • Display
    • Open
    • Push Processing
    • Confirm Deliveries
  • Outcomes
  • Sessions
  • In-App Messaging
  • REST API requests
  • Public API changes
  • Initialization

Checklist

Overview

  • I have filled out all REQUIRED sections above
  • PR does one thing
  • Any Public API changes are explained in the PR details and conform to existing APIs

Testing

  • I have included test coverage for these changes, or explained why they are not needed
  • All automated tests pass, or I explained why that is not possible
  • I have personally tested this on my device, or explained why that is not possible

Final pass

  • Code is as readable as possible.
  • I have reviewed this PR myself, ensuring it meets each checklist item

This change is Reviewable

@nan-li nan-li force-pushed the fix/various_user_module_crashes branch 2 times, most recently from 1d54216 to 4adc282 Compare April 29, 2025 14:26
nan-li added 7 commits April 29, 2025 07:53
* Add test to start user manager from multiple threads
* The models dictionary could be mutated from multiple threads
* Don't update the subscription ID to the server. Locally, we may set it to null if we get 404s and later we will get a value when user create is called.
* We were sending an Update Subscription request with "subscriptionId" = null which isn't even a valid property anyway and no-ops thus far
* The OneSignalUserManagerImpl `start()` method sets up initializers and local state.
* Now prevent any race conditions that could occur with multiple threads trying to start up the manager simultaneously by making it thread-safe, atomic, and properly handle concurrent access.
* We must now be careful of producing a deadlock situation within the start() method.
* Some code paths use _login to create an anonymous user but this is overly complicated when they can just directly call createNewUser.
* createNewUser will set the _user instance so there is no need to set it after the method returns
* Now, _login is only used by the public login method, so remove this helper _login method.
* Start the user manager before making updates for session time, purchases
@nan-li nan-li force-pushed the fix/various_user_module_crashes branch from 4adc282 to 735e755 Compare April 29, 2025 14:55
@nan-li nan-li changed the title Fix crashes in the user manager Synchronize to fix crashes in the user module Apr 29, 2025
@nan-li
Copy link
Contributor Author

nan-li commented Apr 29, 2025

The test testClearBadgesWhenAppEntersForeground is always failing. Need to fix in another PR, but I think some of the involved APIs might be async is why?

@nan-li nan-li requested review from jkasten2 and jinliu9508 April 29, 2025 22:26
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.

2 participants