Closed dianabarsan closed 2 days ago
Adding some reproduction steps using CHT Android, that could be related with this issue:
Note: Step # 1 is important to reproduce the issue, the app should be closed before changing CHT Core and open the app only after changes are deployed.
Expected behaviour: After sync finishes successfully in step 7, the update modal should appear in step 8, so user can click on "reload" and get the latest changes.
Tested in: CHT Core: 3.14.2 (running local in Mac) CHT Android: 0.11.0 Webview Phone: Samsung S10+ with Android 12.
I get the same issue when testing in: CHT Android: latest master as of 25th March 2022. Phones: Nokia 8 with Android 9 and Samsung S10+ with Android 12.
@dianabarsan I think we should prioritise fixing this to help stabalise the build (recent failures documented in #8936 ). Is there a quick fix like adding retries or something?
I agree. I'm not sure about a quick fix, I think we have to rethink service worker upgrades somehow.
Describe the bug Service worker update could happen outside of our changes listener callback: https://github.com/medic/cht-core/blob/master/webapp/src/ts/services/update-service-worker.service.ts#L24 if the browser requests the service worker resource by itself (for example, immediately after a refresh). If a service worker update is triggered before the new service worker is activated, activation is delayed. The client doesn't get a popup to reload and the new service worker is stuck in waiting.
To Reproduce Very hard to reproduce. I noticed it as an infrequent (1 in 50) failure of one of the new service worker e2e tests. To figure out what was going on, I implemented a broadcast channel between the service worker and the app, and had the service worker broadcast when it started an install, after it cached a resource, after a successful install and after activation. I also added some debug logs when the app receives the
service-worker-meta
change and on the state change listener inupdate-service-worker.service
.Logs when race condition is not triggered
Logs when race condition is triggered
When the upgrade "works", the steps are:
onupdatefound
that will show the modal after the service worker is activatedupdate
service-worker.js
When the upgrade doesn't "work":
service-worker.js
(because of refresh or anything else)onupdatefound
that will show the modal after the service worker is activatedupdate
onupdatefound
is never triggeredFor service worker lifecycle, refer to: https://developers.google.com/web/fundamentals/primers/service-workers/lifecycle#waiting The e2e test that failed waited for 15 seconds and then refreshed the page, expecting to see updated cached content, and failed because the previous content was served.
Expected behavior We should prompt the user when a new service worker is registered and activated. Otherwise, we risk the app running old code over new settings. We should probably treat the case of a new service worker already waiting separately in the UpdateServiceWorker service.
Environment
Additional context