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

[Fix] NPE by getScheduleBackgroundRunIn #2212

Merged
merged 1 commit into from
Nov 7, 2024

Conversation

jinliu9508
Copy link
Contributor

@jinliu9508 jinliu9508 commented Nov 6, 2024

Description

One Line Summary

Fixes a NullPointerException issue thrown by SessionService.getScheduleBackgroundRunIn.

Details

Motivation

With the optimization introduced to initWithContext, part of the initialization is now performed in the background after initWithContext is called. This change created a rare race condition where services are accessed shortly after startup, but initialization hasn’t completed. In one scenario, when the app goes into the background before SessionService finishes initializing, a lifecycle callback fires to notify the "unfocus" event, attempting to access SessionService, whose component is null at that time. This error can crash the app and disrupt background services. This PR prevents the crash by ensuring that all major components of SessionService are available during the initWithContext phase.

Scope

This PR moves part of SessionService initialization from start() (which was previously running in the background after initWithContext) to bootstrap(), which is called and completed during the initWithContext phase.

Manual testing

I used a manual approach to reproduce the crash and verify the fix is working for the crash.

Step to reproduce:

  1. Pull the first commit and run the demo app. During initialization, swipe the app into the background. (Note: This commit includes an arbitrary 10-second delay to the session service start time for testing purposes.)
  2. Wait a few seconds and observe the crash in the background.
image

After the fix:
Following the same reproduction steps, the error no longer appears, and the session service functions as expected.

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

@jinliu9508 jinliu9508 requested a review from jkasten2 November 6, 2024 18:30
@jinliu9508 jinliu9508 changed the title Reproduce NPE by getScheduleBackgroundRunIn [Fix] NPE by getScheduleBackgroundRunIn Nov 6, 2024
@jinliu9508 jinliu9508 force-pushed the NPE-getScheduleBackgroundRunIn branch from b290262 to 2c16b3c Compare November 7, 2024 01:38
@jinliu9508
Copy link
Contributor Author

Squashing all commits into one before merge

@jinliu9508 jinliu9508 merged commit 04cfe9b into main Nov 7, 2024
2 checks passed
@jinliu9508 jinliu9508 deleted the NPE-getScheduleBackgroundRunIn branch November 7, 2024 01:48
@jinliu9508 jinliu9508 mentioned this pull request Nov 14, 2024
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