-
-
Notifications
You must be signed in to change notification settings - Fork 528
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
Request made immediately after calling stop on the service worker doesn't always passthrough properly #2323
Comments
Hi, @spocke. Thanks for reporting this. What
|
Given the context above, can you rephrase the actual/expected behaviors so it's more clear? |
Sorry for any confusion here but I'm not talking about unregister the service worker. From my understanding it remains registered but goes into an inactive state so that all requests that gets served though the worker just passes though. However the issue here is that once you post a message to the worker the stop processing requests that code assumes that the postMessage to the worker is instant that is not the case it takes a few milliseconds then it removes any local handlers for it. So it's in this limbo state for a while. |
So from my understanding it does this: msw/src/browser/setupWorker/setupWorker.ts Line 185 in 020161e
So unregisters all local handlers and postMessage a MOCK_DEACTIVATE Line 70 in ca6cb7e
But since it's sync this will now be in a limbo state where the client has not been removed by the service worker yet but all the handlers have been removed on the parent page so there is nothing that can serve the requests anymore until the message has been processed by the worker and the client is properly removed then it goes into the pass though state in Line 109 in ca6cb7e
|
Thanks for a more detailed explanation. I think I got it now. So the issue is the window of an unexpected behavior between calling Making
Basically, if we arrive at that problematic window, and the worker sends a request to the client while removing its ID from the set, the client can ignore that request event if It would be nice to design a reliable test case for this. |
The thing with that window is that even if a request happens within it, the client doesn't remove any handlers. It will still process that request. The only layer that is affected by Line 109 in ca6cb7e
But this check happens in response to any request immediately. So upon an intercepted request, there's either some client to handle it, or not. If there's a client, the worker will message it and get the result back (no matter if Perhaps there's something else to this issue I'm missing. |
@spocke, can you trace what's going on if you put a |
Ok so I added logging in the So from that I can see that the failing ones have the Other observations Safari logs |
Prerequisites
Environment check
msw
versionBrowsers
Chromium (Chrome, Brave, etc.), Safari
Reproduction repository
https://github.com/spocke/mswjs-stop-bug
Reproduction steps
npm i
npm start
http://localhost:3000
Current behavior
Resources are still served though an active client rather than passed though the service worker even if you call
worker.stop()
sometimes that even fails to serve the request since likely because of the handlers being removed for it on the parent page side.It's likely flakey like this because the post message from the browser to the service worker takes a while to process so it can't be a fire and forget it needs to be async and send a ACK back when it has properly intercepted the message to stop intercepting requests.
Expected behavior
No requests should be processed though an active client on the service worker once you call stop on it. The stop function probably needs to be async to know when it's safe to do new requests and expect them to be passed though by
msw/src/mockServiceWorker.js
Line 109 in ca6cb7e
The text was updated successfully, but these errors were encountered: