Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/fix-close-websocket-servers-dispose.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"miniflare": patch
---

Close WebSocket servers during dispose to prevent lingering connections

The `#liveReloadServer` and `#webSocketServer` (both created with `noServer: true`) were never explicitly closed during `Miniflare.dispose()`. Connected WebSocket clients (e.g., browser live reload, devtools) would keep sockets alive, preventing the Node.js event loop from draining cleanly.
11 changes: 11 additions & 0 deletions packages/miniflare/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3030,6 +3030,17 @@ export class Miniflare {
await this.#runtime?.dispose();

await this.#stopLoopbackServer();
// Terminate connected WebSocket clients and close the WebSocket servers.
// These are created with `noServer: true` so they aren't closed by
// stopping the loopback HTTP server.
for (const ws of this.#liveReloadServer.clients) {
ws.terminate();
}
this.#liveReloadServer.close();
for (const ws of this.#webSocketServer.clients) {
ws.terminate();
}
this.#webSocketServer.close();
// Best-effort cleanup: on Windows, workerd may not release file handles
// immediately after disposal, causing EBUSY errors. The temp directory
// lives in os.tmpdir() so the OS will clean it up eventually.
Expand Down
Loading