Skip to content

[Java] Don't call onClose when WebSocket connection is not open #28004

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

Merged
merged 3 commits into from
Nov 23, 2020
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class OkHttpWebSocketWrapper extends WebSocketWrapper {
private WebSocketOnClosedCallback onClose;
private CompletableSubject startSubject = CompletableSubject.create();
private CompletableSubject closeSubject = CompletableSubject.create();
private final ReentrantLock closeLock = new ReentrantLock();
private final ReentrantLock stateLock = new ReentrantLock();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A little unrelated to this pr, but do you know why we use ReentrantLock instead of synchronized?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure there is a good reason. Maybe just oversight


private final Logger logger = LoggerFactory.getLogger(OkHttpWebSocketWrapper.class);

Expand Down Expand Up @@ -82,7 +82,12 @@ public void setOnClose(WebSocketOnClosedCallback onClose) {
private class SignalRWebSocketListener extends WebSocketListener {
@Override
public void onOpen(WebSocket webSocket, Response response) {
startSubject.onComplete();
stateLock.lock();
try {
startSubject.onComplete();
} finally {
stateLock.unlock();
}
}

@Override
Expand All @@ -97,39 +102,64 @@ public void onMessage(WebSocket webSocket, ByteString bytes) {

@Override
public void onClosing(WebSocket webSocket, int code, String reason) {
onClose.invoke(code, reason);
boolean isOpen = false;
stateLock.lock();
try {
isOpen = startSubject.hasComplete();
} finally {
stateLock.unlock();
}

logger.info("WebSocket closing with status code '{}' and reason '{}'.", code, reason);

// Only call onClose if connection is open
if (isOpen) {
onClose.invoke(code, reason);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that we sometimes discard the onClosing code/reason, we should log it.

}

try {
closeLock.lock();
stateLock.lock();
closeSubject.onComplete();
}
finally {
closeLock.unlock();
stateLock.unlock();
}
checkStartFailure();
checkStartFailure(null);
}

@Override
public void onFailure(WebSocket webSocket, Throwable t, Response response) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it ever possible for both onClosing and onFailure to be called on the same WebSocketListener?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe so

logger.error("WebSocket closed from an error: {}.", t.getMessage());
logger.error("WebSocket closed from an error.", t);

boolean isOpen = false;
try {
closeLock.lock();
stateLock.lock();
if (!closeSubject.hasComplete()) {
closeSubject.onError(new RuntimeException(t));
}

isOpen = startSubject.hasComplete();
}
finally {
closeLock.unlock();
stateLock.unlock();
}
onClose.invoke(null, t.getMessage());
checkStartFailure();
// Only call onClose if connection is open
if (isOpen) {
onClose.invoke(null, t.getMessage());
}
checkStartFailure(t);
}

private void checkStartFailure() {
// If the start task hasn't completed yet, then we need to complete it
// exceptionally.
if (!startSubject.hasComplete()) {
startSubject.onError(new RuntimeException("There was an error starting the WebSocket transport."));
private void checkStartFailure(Throwable t) {
stateLock.lock();
try {
// If the start task hasn't completed yet, then we need to complete it
// exceptionally.
if (!startSubject.hasComplete()) {
startSubject.onError(new RuntimeException("There was an error starting the WebSocket transport.", t));
}
} finally {
stateLock.unlock();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ public Completable stop() {
}

void onClose(Integer code, String reason) {
logger.info("WebSocket connection stopping with " +
"code {} and reason '{}'.", code, reason);
if (code == null || code != 1000) {
onClose.invoke(reason);
}
Expand Down