From ad6228d64ef24afdaec77f6d3720c9aee4647be2 Mon Sep 17 00:00:00 2001 From: inkparker Date: Sun, 12 Apr 2026 10:33:07 +0800 Subject: [PATCH 1/2] fix: use listen(0) for OS auto-port-allocation instead of getPort pre-selection In WSL/Windows port environments, the two-step pattern of reserving a port with getPort() then rebinding to it caused massive EADDRINUSE failures (~96 out of 100 attempts). Replacing with server.listen(0) lets the OS auto-assign a free port in a single step, achieving 0 failures in the same environment. Fixes #1854 --- src/runners/baseRunner/BaseRunner.ts | 8 ++-- test/suite/baseRunner.test.ts | 66 ++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 test/suite/baseRunner.test.ts diff --git a/src/runners/baseRunner/BaseRunner.ts b/src/runners/baseRunner/BaseRunner.ts index 4587852b..b27c4390 100644 --- a/src/runners/baseRunner/BaseRunner.ts +++ b/src/runners/baseRunner/BaseRunner.ts @@ -1,7 +1,6 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. -import getPort from 'get-port'; import * as iconv from 'iconv-lite'; import { AddressInfo, createServer, Server, Socket } from 'net'; import * as os from 'os'; @@ -133,12 +132,15 @@ export abstract class BaseRunner implements ITestRunnerInternal { protected async startSocketServer(): Promise { this.server = createServer(); - const socketPort: number = await getPort(); await new Promise((resolve: () => void): void => { - this.server.listen(socketPort, Configurations.LOCAL_HOST, resolve); + this.server.listen(0, Configurations.LOCAL_HOST, resolve); }); } + public getServerAddress(): AddressInfo | string | null { + return this.server?.address() ?? null; + } + protected getRunnerCommandParams(): string[] { return []; } diff --git a/test/suite/baseRunner.test.ts b/test/suite/baseRunner.test.ts new file mode 100644 index 00000000..8b940846 --- /dev/null +++ b/test/suite/baseRunner.test.ts @@ -0,0 +1,66 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. + +'use strict'; + +import * as assert from 'assert'; +import { AddressInfo } from 'net'; +import { BaseRunner } from '../../src/runners/baseRunner/BaseRunner'; +import { RunnerResultAnalyzer } from '../../src/runners/baseRunner/RunnerResultAnalyzer'; +import { IRunTestContext, TestKind } from '../../src/java-test-runner.api'; + +class TestableBaseRunner extends BaseRunner { + protected getAnalyzer(): RunnerResultAnalyzer { + return {} as RunnerResultAnalyzer; + } +} + +suite('BaseRunner Tests', () => { + + suite('startSocketServer', () => { + test('server starts and listens on a valid auto-assigned port', async () => { + const runner = new TestableBaseRunner({ + kind: TestKind.JUnit, + isDebug: false, + projectName: 'test-project', + testItems: [], + testRun: {} as any, + workspaceFolder: {} as any, + } as IRunTestContext); + + await runner.setup(); + + try { + const address = runner.getServerAddress() as AddressInfo; + assert.ok(address, 'server should have an address'); + assert.ok(address.port > 0, `server port should be > 0, got ${address.port}`); + assert.strictEqual(address.family, 'IPv4', 'server should be IPv4'); + } finally { + await runner.tearDown(); + } + }); + + test('getApplicationArgs returns the auto-assigned port as first argument', async () => { + const runner = new TestableBaseRunner({ + kind: TestKind.JUnit, + isDebug: false, + projectName: 'test-project', + testItems: [], + testRun: {} as any, + workspaceFolder: {} as any, + } as IRunTestContext); + + await runner.setup(); + + try { + const args = runner.getApplicationArgs(); + assert.ok(args.length > 0, 'application args should not be empty'); + const portArg = args[0]; + const port = parseInt(portArg, 10); + assert.ok(port > 0, `port in args should be > 0, got ${port}`); + } finally { + await runner.tearDown(); + } + }); + }); +}); From 05198432726d76819f888a64a55edafaa0df4549 Mon Sep 17 00:00:00 2001 From: inkparker Date: Sat, 18 Apr 2026 02:03:15 +0800 Subject: [PATCH 2/2] fix: remove unused get-port dependency --- ThirdPartyNotices.txt | 15 --------------- package-lock.json | 9 --------- package.json | 1 - 3 files changed, 25 deletions(-) diff --git a/ThirdPartyNotices.txt b/ThirdPartyNotices.txt index 7458b648..844fc537 100644 --- a/ThirdPartyNotices.txt +++ b/ThirdPartyNotices.txt @@ -18,7 +18,6 @@ This project incorporates components from the projects listed below. The origina 11. lodash/lodash (https://github.com/lodash/lodash) 12. microsoft/vscode-languageserver-node (https://github.com/microsoft/vscode-languageserver-node) 13. ota4j-team/opentest4j (https://github.com/ota4j-team/opentest4j) -14. sindresorhus/get-port (https://github.com/sindresorhus/get-port) %% Apache Commons Lang NOTICES AND INFORMATION BEGIN HERE ========================================= @@ -1400,17 +1399,3 @@ END OF microsoft/vscode-languageserver-node NOTICES AND INFORMATION limitations under the License. ========================================= END OF ota4j-team/opentest4j NOTICES AND INFORMATION - -%% sindresorhus/get-port NOTICES AND INFORMATION BEGIN HERE -========================================= -MIT License - -Copyright (c) Sindre Sorhus (https://sindresorhus.com) - -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -========================================= -END OF sindresorhus/get-port NOTICES AND INFORMATION diff --git a/package-lock.json b/package-lock.json index 8eae9100..64a26d0f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,6 @@ "version": "0.45.0", "dependencies": { "fs-extra": "^10.1.0", - "get-port": "^4.2.0", "iconv-lite": "^0.6.3", "lodash": "^4.18.1", "lru-cache": "^7.17.0", @@ -2131,14 +2130,6 @@ "url": "https://github.com/sponsors/ljharb" } }, - "node_modules/get-port": { - "version": "4.2.0", - "resolved": "https://registry.npmjs.org/get-port/-/get-port-4.2.0.tgz", - "integrity": "sha512-/b3jarXkH8KJoOMQc3uVGHASwGLPq3gSFJ7tgJm2diza+bydJPTGOibin2steecKeOylE8oY2JERlVWkAJO6yw==", - "engines": { - "node": ">=6" - } - }, "node_modules/get-proto": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/get-proto/-/get-proto-1.0.1.tgz", diff --git a/package.json b/package.json index 09701f55..2f4b8660 100644 --- a/package.json +++ b/package.json @@ -571,7 +571,6 @@ }, "dependencies": { "fs-extra": "^10.1.0", - "get-port": "^4.2.0", "iconv-lite": "^0.6.3", "lodash": "^4.18.1", "lru-cache": "^7.17.0",