Skip to content

Commit

Permalink
Fix #5794 Close socket on bind failure
Browse files Browse the repository at this point in the history
Fix #5794 Close socket on bind failure
  • Loading branch information
gregw committed Dec 12, 2020
1 parent 639cad6 commit 9349ed2
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
Expand Up @@ -42,6 +42,7 @@
import javax.rmi.ssl.SslRMIClientSocketFactory;

import org.eclipse.jetty.util.HostPort;
import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.util.component.AbstractLifeCycle;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
Expand Down Expand Up @@ -243,7 +244,15 @@ private ServerSocket createServerSocket(InetAddress address, int port) throws IO
if (_sslContextFactory == null)
{
ServerSocket server = new ServerSocket();
server.bind(new InetSocketAddress(address, port));
try
{
server.bind(new InetSocketAddress(address, port));
}
catch (Throwable e)
{
IO.close(server);
throw e;
}
return server;
}
else
Expand Down
Expand Up @@ -20,7 +20,6 @@

import java.io.Closeable;
import java.io.IOException;
import java.net.BindException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
Expand All @@ -41,6 +40,7 @@
import org.eclipse.jetty.io.ManagedSelector;
import org.eclipse.jetty.io.SelectorManager;
import org.eclipse.jetty.io.SocketChannelEndPoint;
import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.util.annotation.ManagedAttribute;
import org.eclipse.jetty.util.annotation.ManagedObject;
import org.eclipse.jetty.util.annotation.Name;
Expand Down Expand Up @@ -344,8 +344,9 @@ protected ServerSocketChannel openAcceptChannel() throws IOException
{
serverChannel.socket().bind(bindAddress, getAcceptQueueSize());
}
catch (BindException e)
catch (Throwable e)
{
IO.close(serverChannel);
throw new IOException("Failed to bind to " + bindAddress, e);
}
}
Expand Down
Expand Up @@ -272,7 +272,15 @@ private ServerSocket listen()
{
ServerSocket serverSocket = new ServerSocket();
serverSocket.setReuseAddress(true);
serverSocket.bind(new InetSocketAddress(InetAddress.getByName(host), port));
try
{
serverSocket.bind(new InetSocketAddress(InetAddress.getByName(host), port));
}
catch (Throwable e)
{
IO.close(serverSocket);
throw e;
}
if (port == 0)
{
port = serverSocket.getLocalPort();
Expand Down
Expand Up @@ -176,7 +176,15 @@ public int freePort() throws IOException
try (ServerSocket server = new ServerSocket())
{
server.setReuseAddress(true);
server.bind(new InetSocketAddress("localhost", 0));
try
{
server.bind(new InetSocketAddress("localhost", 0));
}
catch (Throwable t)
{
IO.close(server);
throw t;
}
return server.getLocalPort();
}
}
Expand Down

0 comments on commit 9349ed2

Please sign in to comment.