Skip to content

Commit 1993182

Browse files
committed
[SignalR] [Java] Fix NPE when closing hub connection during negotiation
1 parent 4d23e51 commit 1993182

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/SignalR/clients/java/signalr/core/src/main/java/com/microsoft/signalr/HubConnection.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,8 @@ private Completable stop(String errorMessage) {
445445
CompletableSubject subject = CompletableSubject.create();
446446
startTask.onErrorComplete().subscribe(() ->
447447
{
448-
Completable stop = connectionState.transport.stop();
448+
Transport transport = connectionState.transport;
449+
Completable stop = (transport != null) ? transport.stop() : Completable.complete();
449450
stop.subscribe(() -> subject.onComplete(), e -> subject.onError(e));
450451
});
451452

src/SignalR/clients/java/signalr/test/src/main/java/com/microsoft/signalr/HubConnectionTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2783,6 +2783,26 @@ public void negotiateSentOnStart() {
27832783
assertEquals("http://example.com/negotiate?negotiateVersion=1", sentRequests.get(0).getUrl());
27842784
}
27852785

2786+
@Test
2787+
public void closeWithPendingNegotiate() {
2788+
TestHttpClient client = new TestHttpClient()
2789+
.on("POST", (req) -> Single.just(new HttpResponse(404, "", TestUtils.emptyByteBuffer)).delay(200, TimeUnit.MILLISECONDS));
2790+
2791+
HubConnection hubConnection = HubConnectionBuilder
2792+
.create("http://example.com")
2793+
.withHttpClient(client)
2794+
.build();
2795+
2796+
Completable start = hubConnection.start();
2797+
assertEquals(HubConnectionState.CONNECTING, hubConnection.getConnectionState());
2798+
hubConnection.stop().timeout(3, TimeUnit.SECONDS).blockingAwait();
2799+
assertEquals(HubConnectionState.DISCONNECTED, hubConnection.getConnectionState());
2800+
2801+
HttpRequestException exception = assertThrows(HttpRequestException.class, () -> start.blockingAwait(10, TimeUnit.SECONDS));
2802+
assertEquals("Unexpected status code returned from negotiate: 404 .", exception.getMessage());
2803+
assertEquals(404, exception.getStatusCode());
2804+
}
2805+
27862806
@Test
27872807
public void negotiateThatRedirectsForeverFailsAfter100Tries() {
27882808
TestHttpClient client = new TestHttpClient().on("POST", "http://example.com/negotiate?negotiateVersion=1",

0 commit comments

Comments
 (0)