Skip to content
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

fix: clean 'close' event listeners on socket server after generating new proxy config. #5001

Merged
merged 3 commits into from
Jan 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2295,6 +2295,10 @@

if (newProxyConfig !== proxyConfig) {
proxyConfig = newProxyConfig;
const server = (req.socket ?? req.connection)?.server;

Check failure on line 2298 in lib/Server.js

View workflow job for this annotation

GitHub Actions / Test - ubuntu-latest - Node v12.x, Webpack latest (4/4)

Property 'server' does not exist on type 'Socket'.

Check failure on line 2298 in lib/Server.js

View workflow job for this annotation

GitHub Actions / Test - ubuntu-latest - Node v14.x, Webpack latest (4/4)

Property 'server' does not exist on type 'Socket'.

Check failure on line 2298 in lib/Server.js

View workflow job for this annotation

GitHub Actions / Test - ubuntu-latest - Node v14.x, Webpack latest (2/4)

Property 'server' does not exist on type 'Socket'.

Check failure on line 2298 in lib/Server.js

View workflow job for this annotation

GitHub Actions / Test - ubuntu-latest - Node v14.x, Webpack latest (3/4)

Property 'server' does not exist on type 'Socket'.

Check failure on line 2298 in lib/Server.js

View workflow job for this annotation

GitHub Actions / Test - ubuntu-latest - Node v12.x, Webpack latest (1/4)

Property 'server' does not exist on type 'Socket'.

Check failure on line 2298 in lib/Server.js

View workflow job for this annotation

GitHub Actions / Test - ubuntu-latest - Node v14.x, Webpack latest (1/4)

Property 'server' does not exist on type 'Socket'.

Check failure on line 2298 in lib/Server.js

View workflow job for this annotation

GitHub Actions / Test - ubuntu-latest - Node v12.x, Webpack latest (3/4)

Property 'server' does not exist on type 'Socket'.

Check failure on line 2298 in lib/Server.js

View workflow job for this annotation

GitHub Actions / Test - ubuntu-latest - Node v12.x, Webpack latest (2/4)

Property 'server' does not exist on type 'Socket'.

Check failure on line 2298 in lib/Server.js

View workflow job for this annotation

GitHub Actions / Test - ubuntu-latest - Node v16.x, Webpack latest (4/4)

Property 'server' does not exist on type 'Socket'.

Check failure on line 2298 in lib/Server.js

View workflow job for this annotation

GitHub Actions / Test - ubuntu-latest - Node v19.x, Webpack latest (3/4)

Property 'server' does not exist on type 'Socket'.

Check failure on line 2298 in lib/Server.js

View workflow job for this annotation

GitHub Actions / Test - ubuntu-latest - Node v16.x, Webpack latest (3/4)

Property 'server' does not exist on type 'Socket'.

Check failure on line 2298 in lib/Server.js

View workflow job for this annotation

GitHub Actions / Test - ubuntu-latest - Node v18.x, Webpack latest (4/4)

Property 'server' does not exist on type 'Socket'.

Check failure on line 2298 in lib/Server.js

View workflow job for this annotation

GitHub Actions / Test - ubuntu-latest - Node v18.x, Webpack latest (2/4)

Property 'server' does not exist on type 'Socket'.

Check failure on line 2298 in lib/Server.js

View workflow job for this annotation

GitHub Actions / Test - ubuntu-latest - Node v16.x, Webpack 4 ()

Property 'server' does not exist on type 'Socket'.

Check failure on line 2298 in lib/Server.js

View workflow job for this annotation

GitHub Actions / Test - ubuntu-latest - Node v18.x, Webpack latest (3/4)

Property 'server' does not exist on type 'Socket'.

Check failure on line 2298 in lib/Server.js

View workflow job for this annotation

GitHub Actions / Test - ubuntu-latest - Node v19.x, Webpack latest (1/4)

Property 'server' does not exist on type 'Socket'.

Check failure on line 2298 in lib/Server.js

View workflow job for this annotation

GitHub Actions / Test - ubuntu-latest - Node v19.x, Webpack latest (4/4)

Property 'server' does not exist on type 'Socket'.

Check failure on line 2298 in lib/Server.js

View workflow job for this annotation

GitHub Actions / Test - ubuntu-latest - Node v16.x, Webpack latest (1/4)

Property 'server' does not exist on type 'Socket'.

Check failure on line 2298 in lib/Server.js

View workflow job for this annotation

GitHub Actions / Test - ubuntu-latest - Node v19.x, Webpack latest (2/4)

Property 'server' does not exist on type 'Socket'.

Check failure on line 2298 in lib/Server.js

View workflow job for this annotation

GitHub Actions / Test - ubuntu-latest - Node v18.x, Webpack latest (1/4)

Property 'server' does not exist on type 'Socket'.

Check failure on line 2298 in lib/Server.js

View workflow job for this annotation

GitHub Actions / Test - ubuntu-latest - Node v16.x, Webpack latest (2/4)

Property 'server' does not exist on type 'Socket'.

Check failure on line 2298 in lib/Server.js

View workflow job for this annotation

GitHub Actions / Lint - ubuntu-latest - Node v16.x

Property 'server' does not exist on type 'Socket'.
if (server) {
server.removeAllListeners('close');
}
proxyMiddleware =
/** @type {RequestHandler} */
(getProxyMiddleware(proxyConfig));
Expand Down
10 changes: 10 additions & 0 deletions test/server/proxy-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,14 @@ const proxyOption = {
target: `http://localhost:${port1}`,
};

let maxServerListeners = 0;
const proxyOptionOfArray = [
{ context: "/proxy1", target: proxyOption.target },
function proxy(req, res, next) {
const server = (req?.socket ?? req?.connection)?.server;
if (server) {
maxServerListeners = Math.max(maxServerListeners, server.listeners('close').length)
}
return {
context: "/api/proxy2",
target: `http://localhost:${port2}`,
Expand Down Expand Up @@ -436,6 +441,11 @@ describe("proxy option", () => {
expect(response.statusCode).toEqual(200);
expect(response.text).toEqual("foo+next+function");
});

it("should not exist multiple close events registered", async () => {
expect(maxServerListeners).toBeLessThanOrEqual(1);
});

});

describe("as an array without the `route` option", () => {
Expand Down
Loading