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] Service Worker race condition, when a 2nd sw is in play, causing User not to register #1136

Merged
merged 2 commits into from
Nov 17, 2023

Conversation

jkasten2
Copy link
Member

@jkasten2 jkasten2 commented Nov 15, 2023

Description

1 Line Summary

Fix Service Worker race condition, when a 2nd sw is in play, causing User not to register.

Details

If you attempt to get the service worker registration immediately after registering it the browser can give you a different registration than expected. This is due to the fact the new registration may not be ready yet and there might be sw in scope at a higher level.

The problem noted above cause the OneSignal SDK to register for a push token on the wrong service worker. This in turn causes the SDK to attempt to create a user with a push subscription but without a push token, which results in a 400 error on POST /users.

To fix, instead of calling navigator.serviceWorker.getRegistration we are now simply using the ServiceWorkerRegistration give to us by navigator.serviceWorker.register to avoid this race condition completely

Validation

Tests

Test on Chrome 119 on Windows 11

  • Ensured after accepting push notifications I received a welcome notification. Clear the browser data and push permission, opened a new tab, and tested again 3 more times.
  • Refreshed page after accepting notifications to ensure existing service worker is found without an error.

Info

Checklist

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

Programming Checklist
Interfaces:

  • Don't use default export
  • New interfaces are in model files

Functions:

  • Don't use default export
  • All function signatures have return types
  • Helpers should not access any data but rather be given the data to operate on.

Typescript:

  • No Typescript warnings
  • Avoid silencing null/undefined warnings with the exclamation point

Other:

  • Iteration: refrain from using elem of array syntax. Prefer forEach or use map
  • Avoid using global OneSignal accessor for context if possible. Instead, we can pass it to function/constructor so that we don't call OneSignal.context

Screenshots

Info

Checklist

  • I have included screenshots/recordings of the intended results or explained why they are not needed

Related Tickets



This change is Reviewable

@jkasten2 jkasten2 requested review from rgomezp and emawby November 15, 2023 23:41
@jkasten2 jkasten2 force-pushed the fix/sw_race_condition branch 2 times, most recently from bbb2181 to 42fb865 Compare November 16, 2023 04:42
This is to test OneSignal with conflicting a root service worker. This
causes the OneSignal SDK to sometimes not include a push token
when it makes a REST API to POST /users. This is due to the OneSignal
SDK sometimes getting the registration for the root service worker due
to a race condition, instead of the OneSignal service worker.

We will fix this problem in a follow up commit.
@jkasten2 jkasten2 force-pushed the fix/sw_race_condition branch from 42fb865 to 7930f20 Compare November 16, 2023 06:12
Copy link
Contributor

@rgomezp rgomezp left a comment

Choose a reason for hiding this comment

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

Great fixes. Logic looks good. Just some nits & comment changes

src/shared/managers/ServiceWorkerManager.ts Outdated Show resolved Hide resolved
src/sw/helpers/ServiceWorkerUtilHelper.ts Outdated Show resolved Hide resolved
src/sw/helpers/ServiceWorkerUtilHelper.ts Outdated Show resolved Hide resolved
@jkasten2 jkasten2 force-pushed the fix/sw_race_condition branch from 25628d2 to c7a7d45 Compare November 16, 2023 20:40
If you attempt to get the service worker registration immediately after
registering it the browser can give you a different registration than
exepected. This is due to the fact the new registeration may not be
ready yet and there migth be sw in scope at a higher level.

The problem noted above cause the OneSignal SDK to register for a push
token on the wrong service worker. This in turn causes the SDK to
attempt to create a user with a push susbcription but without a push
token, which results in a 400 error on POST /users.

To fix, instead of calling navigator.serviceWorker.getRegistration
we are now simply using the ServiceWorkerRegistration give to us by
navigator.serviceWorker.register to avoid this race condition completely
@jkasten2 jkasten2 force-pushed the fix/sw_race_condition branch from c7a7d45 to a3798c5 Compare November 17, 2023 18:03
Base automatically changed from feat/show_ios_supported_message to main November 17, 2023 18:07
@jkasten2 jkasten2 merged commit 1fbcdc8 into main Nov 17, 2023
2 checks passed
@jkasten2 jkasten2 deleted the fix/sw_race_condition branch November 17, 2023 18:08
@jkasten2 jkasten2 mentioned this pull request Nov 27, 2023
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