File tree Expand file tree Collapse file tree 1 file changed +11
-1
lines changed
packages/mongodb-memory-server-core/src/util/getport Expand file tree Collapse file tree 1 file changed +11
-1
lines changed Original file line number Diff line number Diff 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 }
You can’t perform that action at this time.
0 commit comments