Skip to content

Commit 6aa3b49

Browse files
authored
[SignalR] [Java] Fix NPE when closing hub connection during negotiation (#62319)
1 parent 60572f1 commit 6aa3b49

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-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: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2783,6 +2783,32 @@ public void negotiateSentOnStart() {
27832783
assertEquals("http://example.com/negotiate?negotiateVersion=1", sentRequests.get(0).getUrl());
27842784
}
27852785

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

0 commit comments

Comments
 (0)