Skip to content

[SignalR] [Java] Fix NPE when closing hub connection during negotiation #62319

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Sejsel
Copy link

@Sejsel Sejsel commented Jun 11, 2025

Fix NPE when closing hub connection during negotiation

Fixes a NullPointerException thrown if HubConnection.close() or HubConnection.stop() is called on a HubConnection while the negotiate request is still ongoing, and eventually fails.

The cause and the effect

The following must happen for the NPE to trigger:

  • start is called
  • negotiate request is still ongoing, state is HubConnectionState.CONNECTING
  • stop is called
    • the early return if HubConnectionState is DISCONNECTED is skipped because state is still connecting
  • negotiate finishes with an error, never populating connectionState.transport
  • last part of stop now triggers, trying to call connectionState.transport.stop(), but connectionState.transport is null.

This NPE is really insidious, because it cannot be caught as usual. RxJava will directly call Thread.uncaughtException() (through RxJavaPlugins.onError()), and the HubConnection.stop() Completable will never complete.
Alternatively HubConnection.close() will block forever (it calls stop().blockingAwait() with no timeout).

The fix

Simply checking if transport is null before trying to close it will avoid this from happening. The start task which errored is guaranteed to be finished at that point, so there should be no leaks of the transport either, it won't be set after the error was thrown.

I also added a test for this specific scenario, it was failing before this fix, and now works as expected.
It unfortunately relies on timing of concurrently running start and stop, I chose a 100ms time window, which should hopefully not be flaky, and is in line with some other tests which also wait for 100 ms, so the test should not run for an unnecessarily long time.

Fixes #52907

@github-actions github-actions bot added the area-signalr Includes: SignalR clients and servers label Jun 11, 2025
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Jun 11, 2025
Copy link
Contributor

Thanks for your PR, @@Sejsel. Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-signalr Includes: SignalR clients and servers community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Nullpointer exception in SignalR Java SDK 8.0.0 HubConnection class
1 participant