Skip to content

fix(node): avoid open handles for infinite delays#2669

Merged
kettanaito merged 5 commits into
mswjs:mainfrom
felmonon:fix/infinite-delay-open-handle
Mar 15, 2026
Merged

fix(node): avoid open handles for infinite delays#2669
kettanaito merged 5 commits into
mswjs:mainfrom
felmonon:fix/infinite-delay-open-handle

Conversation

@felmonon

Copy link
Copy Markdown
Contributor

Summary

  • unref the Node.js timer used by delay('infinite') so it no longer keeps the event loop alive on its own
  • add a unit test covering the unref() behavior on infinite delays
  • add a Node regression that spawns a child process and verifies server.close() can exit cleanly after an infinite delay request

Closes #778.

Testing

  • pnpm build
  • pnpm exec vitest run src/core/delay.test.ts
  • pnpm exec vitest run --config=./test/node/vitest.config.ts test/node/msw-api/context/delay.node.test.ts
  • pnpm exec eslint src/core/delay.ts src/core/delay.test.ts test/node/msw-api/context/delay.node.test.ts

Comment thread test/node/msw-api/context/delay.node.test.ts
Comment thread src/core/delay.ts Outdated
Comment thread src/core/delay.ts Outdated

@kettanaito kettanaito left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Oh wow, this looks extremely promising! Thank you for working on this, @FelmonFekadu. I left a few comments, please let me know what you think. Excited to get this merged, this has been one of the long-standing issues with infinite delays.

@felmonon

Copy link
Copy Markdown
Contributor Author

Addressed the review comments in b76a8a9. Changes: moved the child-process scenario into a dedicated fixture, extracted a shared hasRefCounted() helper reused by delay and ws, and reran the focused build/test/lint set. Verified with: pnpm build; pnpm exec vitest run src/core/delay.test.ts src/core/utils/internal/hasRefCounted.test.ts; pnpm exec vitest run --config=./test/node/vitest.config.ts test/node/msw-api/context/delay.node.test.ts; pnpm exec eslint src/core/delay.ts src/core/ws.ts src/core/delay.test.ts src/core/utils/internal/hasRefCounted.ts src/core/utils/internal/hasRefCounted.test.ts test/node/msw-api/context/delay.node.test.ts.

@felmonon

Copy link
Copy Markdown
Contributor Author

@kettanaito Thanks for the review and the direction here. I addressed the inline comments in b76a8a9c: moved the spawned scenario into a dedicated fixture, extracted a shared hasRefCounted() helper reused by both delay and ws, and reran the focused build/test/lint set locally. Let me know if you want me to tighten anything else before merge.

kettanaito
kettanaito previously approved these changes Mar 14, 2026
@kettanaito kettanaito merged commit 3ccc8a3 into mswjs:main Mar 15, 2026
18 checks passed
@kettanaito

Copy link
Copy Markdown
Member

Released: v2.12.11 🎉

This has been released in v2.12.11.

Get these changes by running the following command:

npm i msw@latest

Predictable release automation by Release.

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.

Using delay('infinite') leaves unresolved promises which make jest warn

2 participants