diff --git a/platform/platform-api/src/com/intellij/util/net/NetUtils.java b/platform/platform-api/src/com/intellij/util/net/NetUtils.java index cc36bd8264af..8f92fe6e52db 100644 --- a/platform/platform-api/src/com/intellij/util/net/NetUtils.java +++ b/platform/platform-api/src/com/intellij/util/net/NetUtils.java @@ -40,19 +40,22 @@ public class NetUtils { public static int findAvailableSocketPort() throws IOException { final ServerSocket serverSocket = new ServerSocket(0); - int port = serverSocket.getLocalPort(); - //workaround for linux : calling close() immediately after opening socket - //may result that socket is not closed - synchronized(serverSocket) { - try { - serverSocket.wait(1); - } - catch (InterruptedException e) { - LOG.error(e); + try { + int port = serverSocket.getLocalPort(); + //workaround for linux : calling close() immediately after opening socket + //may result that socket is not closed + synchronized (serverSocket) { + try { + serverSocket.wait(1); + } + catch (InterruptedException e) { + LOG.error(e); + } } + return port; + } finally { + serverSocket.close(); } - serverSocket.close(); - return port; } public static int[] findAvailableSocketPorts(int capacity) throws IOException {