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

Handle closed executor in ws callbacks #71

Merged
merged 1 commit into from
Oct 8, 2023
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
123 changes: 66 additions & 57 deletions centrifuge/src/main/java/io/github/centrifugal/centrifuge/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -283,52 +283,58 @@ private void _connect() {
@Override
public void onOpen(WebSocket webSocket, Response response) {
super.onOpen(webSocket, response);
Client.this.executor.submit(() -> {
try {
Client.this.handleConnectionOpen();
} catch (Exception e) {
// Should never happen.
e.printStackTrace();
Client.this.listener.onError(Client.this, new ErrorEvent(new UnclassifiedError(e)));
Client.this.processDisconnect(DISCONNECTED_BAD_PROTOCOL, "bad protocol (open)", false);
}
});
try {
Client.this.executor.submit(() -> {
try {
Client.this.handleConnectionOpen();
} catch (Exception e) {
// Should never happen.
e.printStackTrace();
Client.this.listener.onError(Client.this, new ErrorEvent(new UnclassifiedError(e)));
Client.this.processDisconnect(DISCONNECTED_BAD_PROTOCOL, "bad protocol (open)", false);
}
});
} catch (RejectedExecutionException ignored) {
}
}

@Override
public void onMessage(WebSocket webSocket, ByteString bytes) {
super.onMessage(webSocket, bytes);
Client.this.executor.submit(() -> {
if (Client.this.getState() != ClientState.CONNECTING && Client.this.getState() != ClientState.CONNECTED) {
return;
}
InputStream stream = new ByteArrayInputStream(bytes.toByteArray());
while (true) {
Protocol.Reply reply;
try {
if (stream.available() <= 0) {
try {
Client.this.executor.submit(() -> {
if (Client.this.getState() != ClientState.CONNECTING && Client.this.getState() != ClientState.CONNECTED) {
return;
}
InputStream stream = new ByteArrayInputStream(bytes.toByteArray());
while (true) {
Protocol.Reply reply;
try {
if (stream.available() <= 0) {
break;
}
reply = Protocol.Reply.parseDelimitedFrom(stream);
} catch (IOException e) {
// Should never happen. Corrupted server protocol data?
e.printStackTrace();
Client.this.listener.onError(Client.this, new ErrorEvent(new UnclassifiedError(e)));
Client.this.processDisconnect(DISCONNECTED_BAD_PROTOCOL, "bad protocol (proto)", false);
break;
}
try {
Client.this.processReply(reply);
} catch (Exception e) {
// Should never happen. Most probably indicates an unexpected exception coming from the user-level code.
// Theoretically may indicate a bug of SDK also – stack trace will help here.
e.printStackTrace();
Client.this.listener.onError(Client.this, new ErrorEvent(new UnclassifiedError(e)));
Client.this.processDisconnect(DISCONNECTED_BAD_PROTOCOL, "bad protocol (message)", false);
break;
}
reply = Protocol.Reply.parseDelimitedFrom(stream);
} catch (IOException e) {
// Should never happen. Corrupted server protocol data?
e.printStackTrace();
Client.this.listener.onError(Client.this, new ErrorEvent(new UnclassifiedError(e)));
Client.this.processDisconnect(DISCONNECTED_BAD_PROTOCOL, "bad protocol (proto)", false);
break;
}
try {
Client.this.processReply(reply);
} catch (Exception e) {
// Should never happen. Most probably indicates an unexpected exception coming from the user-level code.
// Theoretically may indicate a bug of SDK also – stack trace will help here.
e.printStackTrace();
Client.this.listener.onError(Client.this, new ErrorEvent(new UnclassifiedError(e)));
Client.this.processDisconnect(DISCONNECTED_BAD_PROTOCOL, "bad protocol (message)", false);
break;
}
}
});
});
} catch (RejectedExecutionException ignored) {
}
}

@Override
Expand All @@ -340,26 +346,29 @@ public void onClosing(WebSocket webSocket, int code, String reason) {
@Override
public void onClosed(WebSocket webSocket, int code, String reason) {
super.onClosed(webSocket, code, reason);
Client.this.executor.submit(() -> {
boolean reconnect = code < 3500 || code >= 5000 || (code >= 4000 && code < 4500);
int disconnectCode = code;
String disconnectReason = reason;
if (disconnectCode < 3000) {
if (disconnectCode == MESSAGE_SIZE_LIMIT_EXCEEDED_STATUS) {
disconnectCode = DISCONNECTED_MESSAGE_SIZE_LIMIT;
disconnectReason = "message size limit";
} else {
disconnectCode = CONNECTING_TRANSPORT_CLOSED;
disconnectReason = "transport closed";
try {
Client.this.executor.submit(() -> {
boolean reconnect = code < 3500 || code >= 5000 || (code >= 4000 && code < 4500);
int disconnectCode = code;
String disconnectReason = reason;
if (disconnectCode < 3000) {
if (disconnectCode == MESSAGE_SIZE_LIMIT_EXCEEDED_STATUS) {
disconnectCode = DISCONNECTED_MESSAGE_SIZE_LIMIT;
disconnectReason = "message size limit";
} else {
disconnectCode = CONNECTING_TRANSPORT_CLOSED;
disconnectReason = "transport closed";
}
}
}
if (Client.this.getState() != ClientState.DISCONNECTED) {
Client.this.processDisconnect(disconnectCode, disconnectReason, reconnect);
}
if (Client.this.getState() == ClientState.CONNECTING) {
Client.this.scheduleReconnect();
}
});
if (Client.this.getState() != ClientState.DISCONNECTED) {
Client.this.processDisconnect(disconnectCode, disconnectReason, reconnect);
}
if (Client.this.getState() == ClientState.CONNECTING) {
Client.this.scheduleReconnect();
}
});
} catch (RejectedExecutionException ignored) {
}
}

@Override
Expand Down