Skip to content

Commit b70d868

Browse files
committed
fix(getport): check new port against cache
this is to workaround the VM potentially executing another instance's getport before the other one could start the binary. re #883
1 parent 80c7a8e commit b70d868

File tree

1 file changed

+11
-1
lines changed
  • packages/mongodb-memory-server-core/src/util/getport

1 file changed

+11
-1
lines changed

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

+11-1
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,21 @@ export async function getFreePort(
7373
const triedPort = await tryPort(nextPort);
7474

7575
if (triedPort > 0) {
76-
log('getFreePort: found free port', triedPort);
76+
// check if triedPort is already in the cache (ie the vm executed another instance's getport before binary startup)
77+
// and that the triedPort is not a custom port
78+
const inCacheAndNotSame = PORTS_CACHE.ports.has(triedPort) && nextPort !== triedPort;
79+
log(
80+
`getFreePort: found free port ${triedPort}, in cache and not custom: ${inCacheAndNotSame}`
81+
);
7782

7883
// returned port can be different than the "nextPort" (if net0listen)
7984
PORTS_CACHE.ports.add(nextPort);
8085

86+
// ensure that no other instance can get the same port if the vm decides to run the other instance's getport before starting the last one
87+
if (inCacheAndNotSame) {
88+
continue;
89+
}
90+
8191
return triedPort;
8292
}
8393
}

0 commit comments

Comments
 (0)