Skip to content

Commit 682dac6

Browse files
committed
fix(getport): ignore "ports.has()" if EXP_NET0LISTEN and port == 0
because "net.listen(0)" will generate a random port. if 0 is added to the cache, the loop will exhaust the tries without trying anything
1 parent 3c53d48 commit 682dac6

File tree

1 file changed

+8
-2
lines changed
  • packages/mongodb-memory-server-core/src/util/getport

1 file changed

+8
-2
lines changed

packages/mongodb-memory-server-core/src/util/getport/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,15 @@ export async function getFreePort(
5656
PORTS_CACHE.timeSet = Date.now();
5757
}
5858

59+
const exp_net0listen = envToBool(resolveConfig(ResolveConfigVariables.EXP_NET0LISTEN));
60+
5961
let tries = 0;
6062
while (tries <= max_tries) {
6163
tries += 1;
6264

6365
let nextPort: number;
6466

65-
if (envToBool(resolveConfig(ResolveConfigVariables.EXP_NET0LISTEN))) {
67+
if (exp_net0listen) {
6668
// "0" means to use ".listen" random port
6769
nextPort = tries === 1 ? firstPort : 0;
6870
} else {
@@ -71,7 +73,8 @@ export async function getFreePort(
7173
}
7274

7375
// try next port, because it is already in the cache
74-
if (PORTS_CACHE.ports.has(nextPort)) {
76+
// unless port is "0" which will use "net.listen(0)"
77+
if (PORTS_CACHE.ports.has(nextPort) && nextPort !== 0) {
7578
continue;
7679
}
7780

@@ -81,6 +84,9 @@ export async function getFreePort(
8184

8285
const triedPort = await tryPort(nextPort);
8386

87+
// returned port can be different than the "nextPort" (if EXP_NET0LISTEN)
88+
PORTS_CACHE.ports.add(nextPort);
89+
8490
if (triedPort > 0) {
8591
return triedPort;
8692
}

0 commit comments

Comments
 (0)