From ad3329a78b48a485c8597bc332b55584600adfcf Mon Sep 17 00:00:00 2001 From: George Kalpakas Date: Fri, 8 Jan 2021 13:59:34 +0200 Subject: [PATCH] fix(service-worker): ensure SW stays alive while notifying clients about unrecoverable state (#40234) Previously, the `Driver#notifyClientsAboutUnrecoverableState()` method would not wait for the completion of the promises created to notify the clients. Theoretically, this could result in the SW instance's getting destroyed by the browser before all clients have been notified. This is extremely unlikely to happen in practice, since the async operations are very quick, but it _is_ theoretically possible. This commit ensures that the SW instance will remain alive while notifying the clients by making `notifyClientsAboutUnrecoverableState()` await the notification promises. PR Close #40234 --- packages/service-worker/worker/src/driver.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/service-worker/worker/src/driver.ts b/packages/service-worker/worker/src/driver.ts index 006cb2cce5..30bc320f17 100644 --- a/packages/service-worker/worker/src/driver.ts +++ b/packages/service-worker/worker/src/driver.ts @@ -1026,10 +1026,10 @@ export class Driver implements Debuggable, UpdateSource { .filter(([clientId, hash]) => hash === brokenHash) .map(([clientId]) => clientId); - affectedClients.forEach(async clientId => { + await Promise.all(affectedClients.map(async clientId => { const client = await this.scope.clients.get(clientId); client.postMessage({type: 'UNRECOVERABLE_STATE', reason}); - }); + })); } async notifyClientsAboutUpdate(next: AppVersion): Promise {