Skip to content

Commit

Permalink
fix(getport): check new port against cache
Browse files Browse the repository at this point in the history
this is to workaround the VM potentially executing another instance's getport
before the other one could start the binary.

re #883
  • Loading branch information
hasezoey committed Aug 3, 2024
1 parent 80c7a8e commit b70d868
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion packages/mongodb-memory-server-core/src/util/getport/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,21 @@ export async function getFreePort(
const triedPort = await tryPort(nextPort);

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

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

// 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
if (inCacheAndNotSame) {
continue;
}

return triedPort;
}
}
Expand Down

0 comments on commit b70d868

Please sign in to comment.