Skip to content

Commit

Permalink
Avoid spurious Socket Closed exceptions.
Browse files Browse the repository at this point in the history
  • Loading branch information
sdedic committed Nov 11, 2022
1 parent 0d2ed24 commit dbaca3e
Showing 1 changed file with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.net.Inet4Address;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
Expand All @@ -46,7 +47,10 @@
final class ConnectionSpec implements Closeable {
private final Boolean listen;
private final int port;
// @GuardedBy (this)
private final List<Closeable> close = new ArrayList<>();
// @GuardedBy (this)
private final List<Closeable> closed = new ArrayList<>();

private ConnectionSpec(Boolean listen, int port) {
this.listen = listen;
Expand Down Expand Up @@ -110,11 +114,15 @@ public <ServerType extends LspSession.ScheduledServer> void prepare(
@Override
public void run() {
while (true) {
Socket socket = null;
try {
Socket socket = server.accept();
socket = server.accept();
close.add(socket);
connectToSocket(socket, prefix, session, serverSetter, launcher);
} catch (IOException ex) {
if (isClosed(server)) {
break;
}
Exceptions.printStackTrace(ex);
}
}
Expand Down Expand Up @@ -144,19 +152,34 @@ public void run() {
serverSetter.accept(session, connectionObject);
connectionObject.getRunningFuture().get();
} catch (IOException | InterruptedException | ExecutionException ex) {
Exceptions.printStackTrace(ex);
if (!isClosed(socket)) {
Exceptions.printStackTrace(ex);
}
} finally {
serverSetter.accept(session, null);
}
}
};
connectedThread.start();
}

private boolean isClosed(Closeable c) {
synchronized (this) {
return closed.contains(c);
}
}

@Override
public void close() throws IOException {
for (Closeable c : close) {
c.close();
synchronized (this) {
for (Closeable c : close) {
try {
c.close();
closed.add(c);
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}
}
}
}
}

0 comments on commit dbaca3e

Please sign in to comment.