Skip to content

Conversation

jinliu9508
Copy link
Contributor

@jinliu9508 jinliu9508 commented Apr 10, 2025

Description

One Line Summary

Fix the ANR that can caused by model.add(), model.initializeFromJson(), or modelstore.load()

Details

Motivation

Fix the ANR that may potentially interrupt the normal usage of apps.

Scope

OperationRepo.enqueue() now performs internal enqueue in a specific coroutine and the operation may not be added instantly even the method returns immediately. The purpose of it is to prevent a potential deadlock situation when enqueuing is running into loading. This deadlock currently only happens to OperationModelStore because it is loaded in a background thread to optimize the starting process. When operations like addTag or optIn is called immediately after initWithContext, there is a chance to lock some specific resource that is needed for the loading to complete.

Testing

Unit testing

Was not able to make a targeted unit test because the deadlock situation involves instances not only in modeling but also in the app level.

Manual testing

The first commit simulates a deadlock situation by calling addTag shortly after initWithContext and purposely delay model.initializeFromJson for addTag in the main thread to be blocked. Now if we run the test app, it will freeze on the startup screen.

After the fix, we can see that app does not freeze anymore.

Affected code checklist

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

Checklist

Overview

  • I have filled out all REQUIRED sections above
  • PR does one thing
    • If it is hard to explain how any codes changes are related to each other then it most likely needs to be more than one PR
  • 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.
    • Simplify with less code, followed by splitting up code into well named functions and variables, followed by adding comments to the code.
  • I have reviewed this PR myself, ensuring it meets each checklist item
    • WIP (Work In Progress) is ok, but explain what is still in progress and what you would like feedback on. Start the PR title with "WIP" to indicate this.

This change is Reviewable

Copy link
Member

@jkasten2 jkasten2 left a comment

Choose a reason for hiding this comment

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

Logic changes themselves look good.

The changes done to Model.kt need to be reverted before merging though. I can approve once that last thing is done.

@jinliu9508 jinliu9508 force-pushed the anr-model-add branch 2 times, most recently from 42f213c to 99266f1 Compare April 10, 2025 18:40
@jinliu9508
Copy link
Contributor Author

Squashing the commits for test codes before merging.

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