Skip to content

Commit

Permalink
Fix #5794 Close socket on bind failure (#5802)
Browse files Browse the repository at this point in the history
* Fix #5794 Close socket on bind failure

Fix #5794 Close socket on bind failure

* Fix #5794 Close socket on bind failure

Fix #5794 Close socket on bind failure

* Fix #5794 Close socket on bind failure

Update fix with IO.close()
  • Loading branch information
gregw committed Dec 14, 2020
1 parent 6a7410d commit 25ddd6e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 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 @@ -336,24 +336,16 @@ protected ServerSocketChannel openAcceptChannel() throws IOException

if (serverChannel == null)
{
serverChannel = ServerSocketChannel.open();

InetSocketAddress bindAddress = getHost() == null ? new InetSocketAddress(getPort()) : new InetSocketAddress(getHost(), getPort());
serverChannel.socket().setReuseAddress(getReuseAddress());
serverChannel = ServerSocketChannel.open();
try
{
serverChannel.socket().setReuseAddress(getReuseAddress());
serverChannel.socket().bind(bindAddress, getAcceptQueueSize());
}
catch (BindException e)
catch (Throwable e)
{
try
{
serverChannel.close();
}
catch (IOException ioe)
{
LOG.warn(ioe);
}
IO.close(serverChannel);
throw new IOException("Failed to bind to " + bindAddress, e);
}
}
Expand Down
Expand Up @@ -271,8 +271,16 @@ private ServerSocket listen()
try
{
ServerSocket serverSocket = new ServerSocket();
serverSocket.setReuseAddress(true);
serverSocket.bind(new InetSocketAddress(InetAddress.getByName(host), port));
try
{
serverSocket.setReuseAddress(true);
serverSocket.bind(new InetSocketAddress(InetAddress.getByName(host), port));
}
catch (Throwable e)
{
IO.close(serverSocket);
throw e;
}
if (port == 0)
{
port = serverSocket.getLocalPort();
Expand Down

0 comments on commit 25ddd6e

Please sign in to comment.