Skip to content

Commit 7a4ad6d

Browse files
committed
[SignalR] [Java] Fix NPE when closing hub connection during negotiation
1 parent 047246c commit 7a4ad6d

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2781,6 +2781,29 @@ public void negotiateSentOnStart() {
27812781
List<HttpRequest> sentRequests = client.getSentRequests();
27822782
assertEquals(1, sentRequests.size());
27832783
assertEquals("http://example.com/negotiate?negotiateVersion=1", sentRequests.get(0).getUrl());
2784+
2785+
hubConnection.stop().blockingAwait();
2786+
assertTrue(true);
2787+
}
2788+
2789+
@Test
2790+
public void closeWithPendingNegotiate() {
2791+
TestHttpClient client = new TestHttpClient()
2792+
.on("POST", (req) -> Single.just(new HttpResponse(404, "", TestUtils.emptyByteBuffer)).delay(200, TimeUnit.MILLISECONDS));
2793+
2794+
HubConnection hubConnection = HubConnectionBuilder
2795+
.create("http://example.com")
2796+
.withHttpClient(client)
2797+
.build();
2798+
2799+
Completable start = hubConnection.start();
2800+
assertEquals(HubConnectionState.CONNECTING, hubConnection.getConnectionState());
2801+
hubConnection.stop().timeout(3, TimeUnit.SECONDS).blockingAwait();
2802+
assertEquals(HubConnectionState.DISCONNECTED, hubConnection.getConnectionState());
2803+
2804+
HttpRequestException exception = assertThrows(HttpRequestException.class, () -> start.blockingAwait(10, TimeUnit.SECONDS));
2805+
assertEquals("Unexpected status code returned from negotiate: 404 .", exception.getMessage());
2806+
assertEquals(404, exception.getStatusCode());
27842807
}
27852808

27862809
@Test

0 commit comments

Comments
 (0)