Skip to content

Commit

Permalink
[Java] Catch exceptions from users and change close state correctly (#…
Browse files Browse the repository at this point in the history
…29249)

* [Java] Catch exceptions from users and change close state correctly

* log

* link
  • Loading branch information
BrennanConroy committed Jan 15, 2021
1 parent fe37d39 commit 264e185
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -517,33 +517,44 @@ private void stopConnection(String errorMessage) {
RuntimeException exception = null;
this.state.lock();
try {
ConnectionState connectionState = this.state.getConnectionStateUnsynchronized(true);

if (connectionState == null)
{
this.logger.error("'stopConnection' called with a null ConnectionState. This is not expected, please file a bug. https://github.com/dotnet/aspnetcore/issues/new?assignees=&labels=&template=bug_report.md");
return;
}

// errorMessage gets passed in from the transport. An already existing stopError value
// should take precedence.
if (this.state.getConnectionStateUnsynchronized(false).stopError != null) {
errorMessage = this.state.getConnectionStateUnsynchronized(false).stopError;
if (connectionState.stopError != null) {
errorMessage = connectionState.stopError;
}
if (errorMessage != null) {
exception = new RuntimeException(errorMessage);
logger.error("HubConnection disconnected with an error {}.", errorMessage);
}

ConnectionState connectionState = this.state.getConnectionStateUnsynchronized(true);
if (connectionState != null) {
connectionState.cancelOutstandingInvocations(exception);
connectionState.close();
this.state.setConnectionState(null);
}
this.state.setConnectionState(null);
connectionState.cancelOutstandingInvocations(exception);
connectionState.close();

logger.info("HubConnection stopped.");
this.state.changeState(HubConnectionState.CONNECTED, HubConnectionState.DISCONNECTED);
// We can be in the CONNECTING or CONNECTED state here, depending on if the handshake response was received or not.
// connectionState.close() above will exit the Start call with an error if it's still running
this.state.changeState(HubConnectionState.DISCONNECTED);
} finally {
this.state.unlock();
}

// Do not run these callbacks inside the hubConnectionStateLock
if (onClosedCallbackList != null) {
for (OnClosedCallback callback : onClosedCallbackList) {
callback.invoke(exception);
try {
callback.invoke(exception);
} catch (Exception ex) {
logger.warn("Invoking 'onClosed' method failed:", ex);
}
}
}
}
Expand Down Expand Up @@ -1519,6 +1530,16 @@ public void changeState(HubConnectionState from, HubConnectionState to) {
}
}

public void changeState(HubConnectionState to) {
this.lock.lock();
try {
logger.debug("The HubConnection is transitioning from the {} state to the {} state.", this.hubConnectionState, to);
this.hubConnectionState = to;
} finally {
this.lock.unlock();
}
}

public void lock() {
this.lock.lock();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ public void onClosing(WebSocket webSocket, int code, String reason) {
stateLock.unlock();
}
checkStartFailure(null);

// Send the close frame response if this was a server initiated close, otherwise noops
webSocket.close(1000, "");
}

@Override
Expand Down

0 comments on commit 264e185

Please sign in to comment.