dotnet/aspnetcore · error · RuntimeException

Connection is not active.

Error message

Connection is not active.

What it means

getConnectionStateUnsynchronized(allowNull) is the unsynchronized accessor used by call sites that already hold the state lock. When allowNull is not true (false or null) and the internal ConnectionState field is null - meaning the connection was never started or has been fully torn down - it throws.

Solutions

  1. Call start() before any invoke/send/stream and do not call them after stop().
  2. Subscribe to onClosed and treat the connection as unusable until start() is called again.
  3. Track connection lifecycle in your own code and short-circuit public-API calls when stopped.

Example fix

// before
hub.stop();
hub.send("M"); // ConnectionState is null

// after
hub.start().blockingAwait();
hub.send("M");
Defensive patterns

Strategy: validation

Validate before calling

// This is internal; users guard by checking the public state.
if (hub.getConnectionState() != HubConnectionState.CONNECTED) {
  // connection not active; do not invoke
}

Try / catch

try {
  hub.invoke("M").await();
} catch (RuntimeException e) {
  if (e.getMessage().equals("Connection is not active.")) {
    // start() then retry
  } else throw e;
}

Prevention

When it happens

Trigger: An internal code path requests the ConnectionState without allowing null after the connection has been stopped (state set to null) but before a new start(); or before the very first start().

Common situations: Calling invoke/send/stream immediately after stop(); a stale callback firing after teardown; incorrect library usage where the public API is called against a never-started HubConnection.

Related errors


AI-assisted analysis of dotnet/aspnetcore@3600ca084e (2026-08-11). Data as JSON: /api/errors/8b753241eeb98d56. Report an issue: GitHub.

Appendix: source

Thrown at src/SignalR/clients/java/signalr/core/src/main/java/com/microsoft/signalr/HubConnection.java:1689

        private ConnectionState state;
        private HubConnectionState hubConnectionState = HubConnectionState.DISCONNECTED;

        public ReconnectingConnectionState(Logger logger) {
            this.logger = logger;
        }

        public void setConnectionState(ConnectionState state) {
            this.lock.lock();
            try {
                this.state = state;
            } finally {
                this.lock.unlock();
            }
        }

        public ConnectionState getConnectionStateUnsynchronized(Boolean allowNull) {
            if (allowNull != true && this.state == null) {
                throw new RuntimeException("Connection is not active.");
            }
            return this.state;
        }

        public ConnectionState getConnectionState() {
            this.lock.lock();
            try {
                if (this.state == null) {
                    throw new RuntimeException("Connection is not active.");
                }
                return this.state;
            } finally {
                this.lock.unlock();
            }
        }

        public HubConnectionState getHubConnectionState() {
            return this.hubConnectionState;

View on GitHub (pinned to 3600ca084e)