File tree Expand file tree Collapse file tree 1 file changed +16
-11
lines changed Expand file tree Collapse file tree 1 file changed +16
-11
lines changed Original file line number Diff line number Diff line change @@ -30,6 +30,8 @@ public abstract class DockerSetup : IDisposable
30
30
protected readonly DockerClient docker ;
31
31
protected string containerId ;
32
32
33
+ private static object Lock = new object ( ) ;
34
+
33
35
protected DockerSetup ( )
34
36
{
35
37
if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
@@ -122,21 +124,24 @@ public void Dispose()
122
124
123
125
private int GetFreePort ( )
124
126
{
125
- const int startRange = 1000 ;
126
- const int endRange = 10000 ;
127
- var ipGlobalProperties = IPGlobalProperties . GetIPGlobalProperties ( ) ;
128
- var tcpPorts = ipGlobalProperties . GetActiveTcpListeners ( ) ;
129
- var udpPorts = ipGlobalProperties . GetActiveUdpListeners ( ) ;
127
+ lock ( Lock )
128
+ {
129
+ const int startRange = 1000 ;
130
+ const int endRange = 10000 ;
131
+ var ipGlobalProperties = IPGlobalProperties . GetIPGlobalProperties ( ) ;
132
+ var tcpPorts = ipGlobalProperties . GetActiveTcpListeners ( ) ;
133
+ var udpPorts = ipGlobalProperties . GetActiveUdpListeners ( ) ;
130
134
131
- var result = startRange ;
135
+ var result = startRange ;
132
136
133
- while ( ( ( tcpPorts . Any ( x => x . Port == result ) ) || ( udpPorts . Any ( x => x . Port == result ) ) ) && result <= endRange )
134
- result ++ ;
137
+ while ( ( ( tcpPorts . Any ( x => x . Port == result ) ) || ( udpPorts . Any ( x => x . Port == result ) ) ) && result <= endRange )
138
+ result ++ ;
135
139
136
- if ( result > endRange )
137
- throw new PortsInUseException ( ) ;
140
+ if ( result > endRange )
141
+ throw new PortsInUseException ( ) ;
138
142
139
- return result ;
143
+ return result ;
144
+ }
140
145
}
141
146
}
142
147
}
You can’t perform that action at this time.
0 commit comments