-
Notifications
You must be signed in to change notification settings - Fork 10.4k
[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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(); | ||
|
||
private final Logger logger = LoggerFactory.getLogger(OkHttpWebSocketWrapper.class); | ||
|
||
|
@@ -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 | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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