Copilot commented on code in PR #6545:
URL: https://github.com/apache/ignite-3/pull/6545#discussion_r2318706787
##########
modules/platforms/dotnet/Apache.Ignite.Tests/JavaServer.cs:
##########
@@ -331,5 +328,32 @@ private static void KillProcessOnPort(int port)
using var process = Process.Start(psi);
process?.WaitForExit();
}
+
+ private static int[] GetUnusedPorts(int count)
+ {
+ var ports = new int[count];
+ var listeners = new List<TcpListener>();
+
+ try
+ {
+ for (var i = 0; i < count; i++)
+ {
+ var listener = new TcpListener(IPAddress.Loopback, 0);
+ listeners.Add(listener);
+
+ listener.Start();
+ ports[i] = ((IPEndPoint)listener.LocalEndpoint).Port;
+ }
+
+ return ports;
+ }
+ finally
+ {
+ foreach (var listener in listeners)
+ {
+ listener.Stop();
+ }
+ }
Review Comment:
There's a race condition where ports can be reused between the time they're
released in the finally block and when the Java server attempts to bind to
them. Consider keeping the listeners open until after the server has
successfully started, or implement retry logic for port binding failures.
```suggestion
const int maxAttempts = 10;
var ports = new int[count];
for (int attempt = 0; attempt < maxAttempts; attempt++)
{
var listeners = new List<TcpListener>();
try
{
for (var i = 0; i < count; i++)
{
var listener = new TcpListener(IPAddress.Loopback,
0);
listeners.Add(listener);
listener.Start();
ports[i] = ((IPEndPoint)listener.LocalEndpoint).Port;
}
}
finally
{
foreach (var listener in listeners)
{
listener.Stop();
}
}
// Check if all ports are still available after releasing
listeners.
if (ports.All(IsPortAvailable))
{
return ports;
}
// If not, try again.
}
throw new Exception("Failed to acquire unused ports after
multiple attempts.");
}
/// <summary>
/// Checks if a port is available for binding.
/// </summary>
private static bool IsPortAvailable(int port)
{
try
{
using (var tcpListener = new TcpListener(IPAddress.Loopback,
port))
{
tcpListener.Start();
tcpListener.Stop();
return true;
}
}
catch (SocketException)
{
return false;
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]