We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 9788887 commit d7ff72eCopy full SHA for d7ff72e
dotnet/src/webdriver/Internal/PortUtilities.cs
@@ -37,17 +37,17 @@ public static class PortUtilities
37
/// </exception>
38
public static int FindFreePort()
39
{
40
- using var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
+ var tcpListener = new TcpListener(IPAddress.IPv6Any, 0);
41
42
try
43
44
- socket.Bind(new IPEndPoint(IPAddress.Any, 0));
+ tcpListener.Start();
45
46
- return ((IPEndPoint)socket.LocalEndPoint!).Port;
+ return ((IPEndPoint)tcpListener.LocalEndpoint).Port;
47
}
48
- catch (SocketException ex)
+ finally
49
50
- throw new InvalidOperationException("Unable to find a free port.", ex);
+ tcpListener.Stop();
51
52
53
0 commit comments