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 ending an already ended session #2185

Merged
merged 2 commits into from
Sep 5, 2024

Conversation

jkasten2
Copy link
Member

@jkasten2 jkasten2 commented Aug 27, 2024

Description

One Line Summary

Fixes invalid REST API requests, when location is on, to both outcomes/measure and User updates with session_time being zero/missing in the background.

Details

This is only present when location sharing is enabled in the SDK (in addition to the app and end-user allowing it) and the end-user opens the app at any point after receiving a notification. This resulted in 400 errors in the outcomes/measure case and no-op User updates that wasting resources, the later doesn't depend on receiving notifications. The outcomes/measure is also additive as the bad requests are cached and retried on app open.

Motivation

These invalid and no op request put a lot of extra load on both the device and the OneSignal backend.

Scope

Session tracking only.

Testing

Unit testing

Added a new session test and update an existing one.

Manual testing

Tested on an Android 14 emulator with our example app.

Reproducing the issue:

  1. Allowed location
  2. Sent a notification to the device
  3. Wait until background location update fired.
    • Modified SDK to a lower time so this takes far less than 10 minutes
  4. Observed the invalid request of os__session_duration with no session_time sent and the 400 response.

Reproducing repeating bad cached values:

  1. Reproduce the root issue noted above.
  2. Cold restart the app
  3. Observer the missing session_time being relayed.
  4. Added migration logic and observed these requests are no longer replied.

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
  • 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

# What
Fixes an issue where invalid REST API requests were made to both
outcomes/measure and User updates with session_time were bing made in
the background. This is only present when location sharing is enabled
in the SDK (in addition to the app and end-user allowing it) and the
end-user opens the app at any point after receiving a notification.
This resulted in 400 errors in the outcomes/measure case and no-op User
updates that wasting resources, the later doesn't depend on receiving
notifications. The outcomes/measure is also additive as the bad
requests are cached and retried on app open.

# How
Add a state check to SessionService to prevent ending an already ended
session.

Also address a previous session never ending correctly when the app is
cold started.

Lastly, added comments pointing out the caveats of IBackgroundService
to help prevent future wrong assumptions about when backgroundRun() is
run.

# Follow up
Since there are cached invalid outcomes/measure requests a migration
needs to be created in the SDK to remove them for apps who were
affected by the bug.
@jkasten2 jkasten2 added WIP Work In Progress and removed WIP Work In Progress labels Aug 27, 2024
@jkasten2 jkasten2 assigned jinliu9508 and nan-li and unassigned jinliu9508 Aug 28, 2024
Clean up invalid cached os__session_duration outcome records
with zero session_time produced in SDK versions 5.1.15 to 5.1.20 so we
stop sending these requests to the backend.
@jkasten2 jkasten2 force-pushed the fix/outcome_measure_zero_session_time_400_errors branch from ca46e6f to 1ab8dcc Compare August 28, 2024 18:01
@jkasten2 jkasten2 assigned jinliu9508 and unassigned nan-li Sep 3, 2024
Copy link
Contributor

@jinliu9508 jinliu9508 left a comment

Choose a reason for hiding this comment

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

Code looks good and the changed comments are proper. I have also played with the test unit to verify that the results are correct.

I just have one question that since isValid is false by default and will set to true after the endSession call in onFocus(), is it expected that the first endSession() call after the very first app focus will always return early and do nothing?

@jinliu9508
Copy link
Contributor

Code looks good and the changed comments are proper. I have also played with the test unit to verify that the results are correct.

I just have one question that since isValid is false by default and will set to true after the endSession call in onFocus(), is it expected that the first endSession() call after the very first app focus will always return early and do nothing?

Please disregard the question here as it is now clear to me that the change is necessary to start a new session if app is cold started.

@jkasten2 jkasten2 merged commit baf82eb into main Sep 5, 2024
2 of 3 checks passed
@jkasten2 jkasten2 deleted the fix/outcome_measure_zero_session_time_400_errors branch September 5, 2024 17:44
@jinliu9508 jinliu9508 mentioned this pull request Sep 5, 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.

3 participants