File tree 1 file changed +11
-1
lines changed
packages/mongodb-memory-server-core/src/util/getport
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(
73
73
const triedPort = await tryPort ( nextPort ) ;
74
74
75
75
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
+ ) ;
77
82
78
83
// returned port can be different than the "nextPort" (if net0listen)
79
84
PORTS_CACHE . ports . add ( nextPort ) ;
80
85
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
+
81
91
return triedPort ;
82
92
}
83
93
}
You can’t perform that action at this time.
0 commit comments