You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
MSW sometimes still handles requests after worker is stoppedΒ #2597
worker.stop();// This expect sometimes passes, sometimes fails.awaitexpect(async()=>{awaitfetch(`${baseURL}/users`);}).rejects.toThrowError(TypeError);
From my tests, I did not see this issue happening with server.close(), neither with worker.stop() and server.close() before 2.11.2 (I tested 2.11.1).
Expected behavior
I expected all requests made after worker.stop() was called to not be handled.
changed the title [-]MSW sometimes still handles requests after worker was stopped[/-][+]MSW sometimes still handles requests after worker is stopped[/+]on Apr 15, 2026
Hi, @kettanaito. I'm not sure that this issue is fixed.
I upgraded the reproduction repository to msw@2.13.4 and the problem persists.
Also, the new tests in #2668 still fail non-deterministically even against main:
pnpm test:browser in-flight-request.test.ts
...
3 failed
[chromium] βΊ test/browser/msw-api/setup-worker/stop/in-flight-request.test.ts:89:3 βΊ bypasses requests immediately made after the worker was stopped, considering no default response (2)
[chromium] βΊ test/browser/msw-api/setup-worker/stop/in-flight-request.test.ts:89:3 βΊ bypasses requests immediately made after the worker was stopped, considering no default response (12)
[chromium] βΊ test/browser/msw-api/setup-worker/stop/in-flight-request.test.ts:89:3 βΊ bypasses requests immediately made after the worker was stopped, considering no default response (23)
99 passed (22.6s)
Let me know if you have any recommendations about how to approach this problem. I can help implement/validate/review the fix if you are interested. #2683 looks related.
@diego-aquino, yeah, you are right. The test is flaky. I've run it multiple times locally before the release, but I can verify that it fails 1/10 times for me. There has to be some race still happening somewhere. I really hope it's just an incorrectly orchestrated test.
You're saying it's still happening for you. Is that still reproducible on the repo you shared above?
You're saying it's still happening for you. Is that still reproducible on the repo you shared above?
@kettanaito, unfortunately yes. I updated the repo yesterday and the tests still fail non-deterministically between 1/2 and 2/3 of the time.
The test itself is very simple:
for(lettestNumber=1;testNumber<=100;testNumber++){it(`bypasses requests made after the worker was stopped (test ${testNumber})`,async()=>{awaitworker.start({quiet: true});letresponse=awaitfetch(`${baseURL}/users`);expect(response.status).toBe(200);expect(awaitresponse.json()).toEqual([]);worker.stop();// This expect sometimes passes, sometimes fails.awaitexpect(async()=>{awaitfetch(`${baseURL}/users`);}).rejects.toThrowError(TypeError);});}
Would it help if you moved worker.start() outside of the loop in the example above?
The complexity of properly managing worker.stop() (disabled mode) is starting to annoy me, to be honest. I wonder if all that method should do is prepend a passthrough request handler and be done with it. That would solve any timing issues and produce the same behavior, with the only implication is that requests performed after worker.stop() would still route entirely through the worker (request -> worker -> client -- passthrough --> worker -> response).
I quite dislike the semantics of .stop() since, despite its name, it's not an opposite action to .start(). Stopping the worker does not, in fact stop the worker as that's a costly procedure and nobody should be starting/stopping a service worker repeatedly anyway. What it was always meant to do (and what it does, in-flight requests aside) is to pause the interception. These are just thoughts for the future, but perhaps this would make more sense:
await worker.start(), register and activate the worker;
worker.pause(), pause the request interception;
await worker.stop(), unregister the worker.
This could help us communicate behavioral expectations to the end users:
pausing the worker handles the in-flight requests as per your handlers;
stopping the worker aborts in-flight requests entirely (not a thing yet, might be a thing).
Since our interception is worker-first, there's no way to know when a request happens on the page and whether it happened before or after worker.stop() was called. I've introduced the interceptedAt timestamp in the worker, but there's a race condition:
It takes time for the request to reach the worker. The interceptedAt timestamp doesn't accurately reflect the request initiator time in relation to worker.stop(). It's possible that a request performed before stop receives a timestamp that's higher than stoppedAt, making MSW think that request has to be passthrough instead of handled.
The same timing issue applies to the stop before request flow.
Tracking requests client-side would be against MSW's principles and I'm strongly against that. It makes me think that the actual issue here is the undefined expectations of what is supposed to happen when you call worker.stop(). The original assumption was:
handle in-flight requests performed before stopping;
passthrough the requests performed after stopping.
This does make sense, but as I mentioned is impossible to implement reliably in a worker-first architecture. I've also considered the following approaches, but none of them solve the problem:
worker.stop() returning a promise with CLIENT_CLOSED_ACK;
I found a way to reliably solve this in #2724, but it includes breaking changes in the worker, which means the fix will be available in the next major releaseβ3.0. You can always use a WIP build of #2692 if this is blocking you though. That branch is experimental and although I will try my best to keep it stable, things might break so keep that in mind.
This issue will close when the mentioned fix will merge into feat/3.0 without it being released. There's no reason to keep it open if it cannot be addressed as a patch.
Activity
test(in-flight): add flaky in-flight request tests
[-]MSW sometimes still handles requests after worker was stopped[/-][+]MSW sometimes still handles requests after worker is stopped[/+]kettanaito commentedon Apr 16, 2026
Released: v2.13.4 π
This has been released in v2.13.4.
Get these changes by running the following command:
Predictable release automation by Release.
diego-aquino commentedon Apr 16, 2026
Hi, @kettanaito. I'm not sure that this issue is fixed.
I upgraded the reproduction repository to
msw@2.13.4and the problem persists.Also, the new tests in #2668 still fail non-deterministically even against
main:Let me know if you have any recommendations about how to approach this problem. I can help implement/validate/review the fix if you are interested. #2683 looks related.
kettanaito commentedon Apr 17, 2026
@diego-aquino, yeah, you are right. The test is flaky. I've run it multiple times locally before the release, but I can verify that it fails 1/10 times for me. There has to be some race still happening somewhere. I really hope it's just an incorrectly orchestrated test.
You're saying it's still happening for you. Is that still reproducible on the repo you shared above?
diego-aquino commentedon Apr 17, 2026
@kettanaito, unfortunately yes. I updated the repo yesterday and the tests still fail non-deterministically between 1/2 and 2/3 of the time.
The test itself is very simple:
kettanaito commentedon Apr 20, 2026
Would it help if you moved
worker.start()outside of the loop in the example above?The complexity of properly managing
worker.stop()(disabled mode) is starting to annoy me, to be honest. I wonder if all that method should do is prepend a passthrough request handler and be done with it. That would solve any timing issues and produce the same behavior, with the only implication is that requests performed afterworker.stop()would still route entirely through the worker (request -> worker -> client -- passthrough --> worker -> response).I quite dislike the semantics of
.stop()since, despite its name, it's not an opposite action to.start(). Stopping the worker does not, in fact stop the worker as that's a costly procedure and nobody should be starting/stopping a service worker repeatedly anyway. What it was always meant to do (and what it does, in-flight requests aside) is to pause the interception. These are just thoughts for the future, but perhaps this would make more sense:await worker.start(), register and activate the worker;worker.pause(), pause the request interception;await worker.stop(), unregister the worker.This could help us communicate behavioral expectations to the end users:
kettanaito commentedon Apr 20, 2026
Root cause
Since our interception is worker-first, there's no way to know when a request happens on the page and whether it happened before or after
worker.stop()was called. I've introduced theinterceptedAttimestamp in the worker, but there's a race condition:The same timing issue applies to the stop before request flow.
Tracking requests client-side would be against MSW's principles and I'm strongly against that. It makes me think that the actual issue here is the undefined expectations of what is supposed to happen when you call
worker.stop(). The original assumption was:This does make sense, but as I mentioned is impossible to implement reliably in a worker-first architecture. I've also considered the following approaches, but none of them solve the problem:
worker.stop()returning a promise withCLIENT_CLOSED_ACK;kettanaito commentedon Apr 20, 2026
Update
I found a way to reliably solve this in #2724, but it includes breaking changes in the worker, which means the fix will be available in the next major releaseβ3.0. You can always use a WIP build of #2692 if this is blocking you though. That branch is experimental and although I will try my best to keep it stable, things might break so keep that in mind.
This issue will close when the mentioned fix will merge into
feat/3.0without it being released. There's no reason to keep it open if it cannot be addressed as a patch.kettanaito commentedon Apr 20, 2026
Closed by #2724.
diego-aquino commentedon Apr 20, 2026
Hi, @kettanaito. Thanks a lot for the detailed investigation.
I agree. My initial assumption was that
worker.stop()reverted the results ofworker.start(), which is not the case as you've explained.Great! This API looks good to me.
Thank you. This is non-blocking for me. I raised it as a separate issue because I was wondering if it was the expected behavior.
chore(interceptor): upgrade `msw` (#1066)