dotnet/aspnetcore · error · RuntimeException
The 'send' method cannot be called if the connection is not…
Error message
The 'send' method cannot be called if the connection is not active.
What it means
send() is a fire-and-forget hub invocation. It acquires the connection state lock and refuses to dispatch when the HubConnectionState is anything other than CONNECTED - typically because the connection is still connecting, reconnecting, or has been stopped.
Solutions
- Await the start() Completable before calling send(): hub.start().await() or .blockingAwait() / subscribe and send in the continuation.
- Subscribe to onClosed(...) and re-run start() before retrying the send.
- Guard the call: check hub.getConnectionState() == CONNECTED immediately before send (still racy without the lock, so prefer the await approach).
Example fix
// before
HubConnection hub = builder.build();
hub.start();
hub.send("DoThing", arg); // throws - not yet CONNECTED
// after
hub.start().blockingAwait(); // or .await()
hub.send("DoThing", arg); Defensive patterns
Strategy: validation
Validate before calling
// Gate send() on the public connection state.
if (hub.getConnectionState() == HubConnectionState.CONNECTED) {
hub.send(method, args);
} else {
// queue or drop; do not call send()
} Try / catch
try {
hub.send(method, args);
} catch (RuntimeException e) {
if (e.getMessage().contains("connection is not active")) {
// enqueue for retry after onReconnected
} else throw e;
} Prevention
- Always await start() before sending.
- Maintain an AtomicBoolean connectedFlag flipped in onClosed/onReconnected.
- Queue outbound messages when the flag is false.
When it happens
Trigger: Calling hubConnection.send(method, args) before start() has completed, after stop(), during a reconnect, or after the transport dropped and the state machine moved to CONNECTING/DISCONNECTED.
Common situations: Race between start() returning the Single and the user code immediately calling send(); sending from a UI handler that does not wait on the start Completable; sending after a network drop without subscribing to onClosed(...) to re-establish.
Related errors
- The 'invoke' method cannot be called if the connection is…
- The 'stream' method cannot be called if the connection is…
- Trying to send and message while the connection is not…
- Connection is not active.
- Invocation provides argument(s) but target expects .
AI-assisted analysis of dotnet/aspnetcore@3600ca084e (2026-08-11).
Data as JSON: /api/errors/5f320d5712b930ee.
Report an issue: GitHub.
Appendix: source
Thrown at src/SignalR/clients/java/signalr/core/src/main/java/com/microsoft/signalr/HubConnection.java:598
} catch (Exception ex) {
logger.warn("Invoking 'onClosed' method failed:", ex);
}
}
}
}
/**
* Invokes a hub method on the server using the specified method name.
* Does not wait for a response from the receiver.
*
* @param method The name of the server method to invoke.
* @param args The arguments to be passed to the method.
*/
public void send(String method, Object... args) {
this.state.lock();
try {
if (this.state.getHubConnectionState() != HubConnectionState.CONNECTED) {
throw new RuntimeException("The 'send' method cannot be called if the connection is not active.");
}
sendInvocationMessage(method, args);
} finally {
this.state.unlock();
}
}
private void sendInvocationMessage(String method, Object[] args) {
sendInvocationMessage(method, args, null, false);
}
private void sendInvocationMessage(String method, Object[] args, String id, Boolean isStreamInvocation) {
List<String> streamIds = new ArrayList<>();
List<Observable> streams = new ArrayList<>();
args = checkUploadStream(args, streamIds, streams);
InvocationMessage invocationMessage;
if (isStreamInvocation) {
invocationMessage = new StreamInvocationMessage(null, id, method, args, streamIds);View on GitHub (pinned to 3600ca084e)