Skip to content

Commit e84f4d3

Browse files
fix: close the hot transport on plugin shutdown
In plugin mode stop() skips middleware.close(), so the host's watching stays the host's, but that also left the middleware's hot transport open: a WebSocket endpoint on a port of its own kept accepting clients and sending heartbeats after compiler.close(). Close it in stop(). Closes #5781 Co-authored-by: Nirmal Siddh <nirmalsiddh2812@gmail.com>
1 parent 6421747 commit e84f4d3

3 files changed

Lines changed: 72 additions & 0 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack-dev-server": patch
3+
---
4+
5+
In plugin mode, `compiler.close()` now also closes the built-in hot transport, so a WebSocket endpoint on a port of its own no longer outlives the compiler and keeps the process running.

‎lib/Server.js‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4491,6 +4491,19 @@ class Server {
44914491
});
44924492
})
44934493
);
4494+
} else {
4495+
// The middleware's hot transport is still its own to close, or its
4496+
// endpoint outlives `compiler.close()`. A custom one is closed above
4497+
// already, and closing it again is harmless, as `close()` does too.
4498+
// TODO once the webpack-dev-middleware dependency includes
4499+
// webpack/webpack-dev-middleware#2507, call `close()` in plugin mode
4500+
// too: it then closes only the hot transport, without a warning.
4501+
const { context } = this.middleware;
4502+
4503+
if (context.hot) {
4504+
context.hot.close();
4505+
context.hot = undefined;
4506+
}
44944507
}
44954508

44964509
this.middleware = undefined;

‎test/e2e/api-plugin.test.js‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import fs from "node:fs";
22
import http from "node:http";
3+
import net from "node:net";
34
import os from "node:os";
45
import path from "node:path";
56
import { afterEach, beforeEach, describe, it } from "node:test";
@@ -134,6 +135,59 @@ describe("API (plugin)", () => {
134135
stopSpy.mockRestore();
135136
});
136137

138+
it("should close a built-in WebSocket endpoint of its own port via compiler.close()", async () => {
139+
const compiler = webpack(config);
140+
const server = new Server({
141+
port: portA,
142+
webSocketServer: { type: "ws", options: { port: portB } },
143+
});
144+
145+
server.apply(compiler);
146+
147+
await compile(compiler, portA);
148+
149+
const client = new WebSocket(`ws://127.0.0.1:${portB}/ws`, {
150+
headers: {
151+
host: `127.0.0.1:${portA}`,
152+
origin: `http://127.0.0.1:${portA}`,
153+
},
154+
});
155+
const clientClosed = new Promise((resolve) => {
156+
client.once("close", () => resolve(true));
157+
});
158+
159+
await new Promise((resolve, reject) => {
160+
client.once("open", resolve);
161+
client.once("error", reject);
162+
});
163+
164+
await new Promise((resolve) => {
165+
compiler.close(resolve);
166+
});
167+
168+
const closed = await Promise.race([
169+
clientClosed,
170+
new Promise((resolve) => {
171+
setTimeout(() => resolve(false), 2000);
172+
}),
173+
]);
174+
175+
client.terminate();
176+
expect(closed).toBe(true);
177+
178+
const refused = await new Promise((resolve) => {
179+
const socket = net.connect({ host: "127.0.0.1", port: portB });
180+
181+
socket.once("connect", () => {
182+
socket.destroy();
183+
resolve(false);
184+
});
185+
socket.once("error", (error) => resolve(error.code === "ECONNREFUSED"));
186+
});
187+
188+
expect(refused).toBe(true);
189+
});
190+
137191
it("should tear down without error when the compiler is closed", async () => {
138192
// In plugin mode the host owns `watching`, so `stop()` must not call
139193
// `middleware.close()` — otherwise `compiler.close` surfaces a TypeError.

0 commit comments

Comments
 (0)