Skip to content

Commit de66eae

Browse files
inkparkinkparkerchagong
authored
fix: use listen(0) for OS auto-port-allocation instead of getPort (#1855)
* 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 * fix: remove unused get-port dependency --------- Co-authored-by: inkparker <inkparker@inkers-pc.localdomain> Co-authored-by: Changyong Gong <chagon@microsoft.com>
1 parent 77844e7 commit de66eae

5 files changed

Lines changed: 71 additions & 28 deletions

File tree

ThirdPartyNotices.txt

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ This project incorporates components from the projects listed below. The origina
1818
11. lodash/lodash (https://github.com/lodash/lodash)
1919
12. microsoft/vscode-languageserver-node (https://github.com/microsoft/vscode-languageserver-node)
2020
13. ota4j-team/opentest4j (https://github.com/ota4j-team/opentest4j)
21-
14. sindresorhus/get-port (https://github.com/sindresorhus/get-port)
2221

2322
%% Apache Commons Lang NOTICES AND INFORMATION BEGIN HERE
2423
=========================================
@@ -1400,17 +1399,3 @@ END OF microsoft/vscode-languageserver-node NOTICES AND INFORMATION
14001399
limitations under the License.
14011400
=========================================
14021401
END OF ota4j-team/opentest4j NOTICES AND INFORMATION
1403-
1404-
%% sindresorhus/get-port NOTICES AND INFORMATION BEGIN HERE
1405-
=========================================
1406-
MIT License
1407-
1408-
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
1409-
1410-
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:
1411-
1412-
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
1413-
1414-
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.
1415-
=========================================
1416-
END OF sindresorhus/get-port NOTICES AND INFORMATION

package-lock.json

Lines changed: 0 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,6 @@
571571
},
572572
"dependencies": {
573573
"fs-extra": "^10.1.0",
574-
"get-port": "^4.2.0",
575574
"iconv-lite": "^0.6.3",
576575
"lodash": "^4.18.1",
577576
"lru-cache": "^7.17.0",

src/runners/baseRunner/BaseRunner.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

4-
import getPort from 'get-port';
54
import * as iconv from 'iconv-lite';
65
import { AddressInfo, createServer, Server, Socket } from 'net';
76
import * as os from 'os';
@@ -133,12 +132,15 @@ export abstract class BaseRunner implements ITestRunnerInternal {
133132

134133
protected async startSocketServer(): Promise<void> {
135134
this.server = createServer();
136-
const socketPort: number = await getPort();
137135
await new Promise<void>((resolve: () => void): void => {
138-
this.server.listen(socketPort, Configurations.LOCAL_HOST, resolve);
136+
this.server.listen(0, Configurations.LOCAL_HOST, resolve);
139137
});
140138
}
141139

140+
public getServerAddress(): AddressInfo | string | null {
141+
return this.server?.address() ?? null;
142+
}
143+
142144
protected getRunnerCommandParams(): string[] {
143145
return [];
144146
}

test/suite/baseRunner.test.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
'use strict';
5+
6+
import * as assert from 'assert';
7+
import { AddressInfo } from 'net';
8+
import { BaseRunner } from '../../src/runners/baseRunner/BaseRunner';
9+
import { RunnerResultAnalyzer } from '../../src/runners/baseRunner/RunnerResultAnalyzer';
10+
import { IRunTestContext, TestKind } from '../../src/java-test-runner.api';
11+
12+
class TestableBaseRunner extends BaseRunner {
13+
protected getAnalyzer(): RunnerResultAnalyzer {
14+
return {} as RunnerResultAnalyzer;
15+
}
16+
}
17+
18+
suite('BaseRunner Tests', () => {
19+
20+
suite('startSocketServer', () => {
21+
test('server starts and listens on a valid auto-assigned port', async () => {
22+
const runner = new TestableBaseRunner({
23+
kind: TestKind.JUnit,
24+
isDebug: false,
25+
projectName: 'test-project',
26+
testItems: [],
27+
testRun: {} as any,
28+
workspaceFolder: {} as any,
29+
} as IRunTestContext);
30+
31+
await runner.setup();
32+
33+
try {
34+
const address = runner.getServerAddress() as AddressInfo;
35+
assert.ok(address, 'server should have an address');
36+
assert.ok(address.port > 0, `server port should be > 0, got ${address.port}`);
37+
assert.strictEqual(address.family, 'IPv4', 'server should be IPv4');
38+
} finally {
39+
await runner.tearDown();
40+
}
41+
});
42+
43+
test('getApplicationArgs returns the auto-assigned port as first argument', async () => {
44+
const runner = new TestableBaseRunner({
45+
kind: TestKind.JUnit,
46+
isDebug: false,
47+
projectName: 'test-project',
48+
testItems: [],
49+
testRun: {} as any,
50+
workspaceFolder: {} as any,
51+
} as IRunTestContext);
52+
53+
await runner.setup();
54+
55+
try {
56+
const args = runner.getApplicationArgs();
57+
assert.ok(args.length > 0, 'application args should not be empty');
58+
const portArg = args[0];
59+
const port = parseInt(portArg, 10);
60+
assert.ok(port > 0, `port in args should be > 0, got ${port}`);
61+
} finally {
62+
await runner.tearDown();
63+
}
64+
});
65+
});
66+
});

0 commit comments

Comments
 (0)