Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct web socket closing #1227

Merged
merged 3 commits into from
Mar 28, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions src/main/java/org/java_websocket/server/WebSocketServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,10 @@ public void start() {
new Thread(this).start();
}

public void stop(int timeout) throws InterruptedException {
stop(timeout, "");
}

/**
* Closes all connected clients sockets, then closes the underlying ServerSocketChannel,
* effectively killing the server socket selectorthread, freeing the port the server was bound to
Expand All @@ -257,10 +261,11 @@ public void start() {
*
* @param timeout Specifies how many milliseconds the overall close handshaking may take
* altogether before the connections are closed without proper close
* handshaking.<br>
* handshaking.
* @param closeMessage Specifies message for remote client<br>
* @throws InterruptedException Interrupt
*/
public void stop(int timeout) throws InterruptedException {
public void stop(int timeout, String closeMessage) throws InterruptedException {
if (!isclosed.compareAndSet(false,
true)) { // this also makes sure that no further connections will be added to this.connections
return;
Expand All @@ -274,7 +279,7 @@ public void stop(int timeout) throws InterruptedException {
}

for (WebSocket ws : socketsToClose) {
ws.close(CloseFrame.GOING_AWAY);
ws.close(CloseFrame.GOING_AWAY, closeMessage);
}

wsf.close();
Expand Down Expand Up @@ -680,6 +685,17 @@ private void handleIOException(SelectionKey key, WebSocket conn, IOException ex)
private void handleFatal(WebSocket conn, Exception e) {
log.error("Shutdown due to fatal error", e);
onError(conn, e);

String causeMessage = e.getCause() != null ? " caused by " + e.getCause().getClass().getName() : "";
String errorMessage = "Got error on server side: " + e.getClass().getName() + causeMessage;
try {
stop(0, errorMessage);
} catch (InterruptedException e1) {
Thread.currentThread().interrupt();
log.error("Interrupt during stop", e);
onError(null, e1);
}

//Shutting down WebSocketWorkers, see #222
if (decoders != null) {
for (WebSocketWorker w : decoders) {
Expand All @@ -689,13 +705,6 @@ private void handleFatal(WebSocket conn, Exception e) {
if (selectorthread != null) {
selectorthread.interrupt();
}
try {
stop();
} catch (InterruptedException e1) {
Thread.currentThread().interrupt();
log.error("Interrupt during stop", e);
onError(null, e1);
}
}

@Override
Expand Down Expand Up @@ -1080,9 +1089,6 @@ public void run() {
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (VirtualMachineError | ThreadDeath | LinkageError e) {
if (ws != null) {
ws.close();
}
log.error("Got fatal error in worker thread {}", getName());
Exception exception = new Exception(e);
handleFatal(ws, exception);
Expand Down