{"record":{"id":"8b753241eeb98d56","repo":"dotnet/aspnetcore","slug":"connection-is-not-active","errorCode":null,"errorMessage":"Connection is not active.","messagePattern":"Connection is not active\\.","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/SignalR/clients/java/signalr/core/src/main/java/com/microsoft/signalr/HubConnection.java","lineNumber":1689,"sourceCode":"        private ConnectionState state;\n        private HubConnectionState hubConnectionState = HubConnectionState.DISCONNECTED;\n\n        public ReconnectingConnectionState(Logger logger) {\n            this.logger = logger;\n        }\n\n        public void setConnectionState(ConnectionState state) {\n            this.lock.lock();\n            try {\n                this.state = state;\n            } finally {\n                this.lock.unlock();\n            }\n        }\n\n        public ConnectionState getConnectionStateUnsynchronized(Boolean allowNull) {\n            if (allowNull != true && this.state == null) {\n                throw new RuntimeException(\"Connection is not active.\");\n            }\n            return this.state;\n        }\n\n        public ConnectionState getConnectionState() {\n            this.lock.lock();\n            try {\n                if (this.state == null) {\n                    throw new RuntimeException(\"Connection is not active.\");\n                }\n                return this.state;\n            } finally {\n                this.lock.unlock();\n            }\n        }\n\n        public HubConnectionState getHubConnectionState() {\n            return this.hubConnectionState;","sourceCodeStart":1671,"sourceCodeEnd":1707,"githubUrl":"https://github.com/dotnet/aspnetcore/blob/3600ca084e9c8b5f4174fc5e747f4c52d2100806/src/SignalR/clients/java/signalr/core/src/main/java/com/microsoft/signalr/HubConnection.java#L1671-L1707","documentation":"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.","triggerScenarios":"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().","commonSituations":"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.","solutions":["Call start() before any invoke/send/stream and do not call them after stop().","Subscribe to onClosed and treat the connection as unusable until start() is called again.","Track connection lifecycle in your own code and short-circuit public-API calls when stopped."],"exampleFix":"// before\nhub.stop();\nhub.send(\"M\"); // ConnectionState is null\n\n// after\nhub.start().blockingAwait();\nhub.send(\"M\");","handlingStrategy":"validation","validationCode":"// This is internal; users guard by checking the public state.\nif (hub.getConnectionState() != HubConnectionState.CONNECTED) {\n  // connection not active; do not invoke\n}","typeGuard":null,"tryCatchPattern":"try {\n  hub.invoke(\"M\").await();\n} catch (RuntimeException e) {\n  if (e.getMessage().equals(\"Connection is not active.\")) {\n    // start() then retry\n  } else throw e;\n}","preventionTips":["Never call public API on a stopped/never-started HubConnection.","After stop(), build a new HubConnection rather than reusing.","Track lifecycle explicitly with an enum mirrored from onClosed/onReconnected."],"tags":["signalr","java","state","lifecycle","internal-invariant"],"backgroundTag":null,"analyzedSha":"3600ca084e9c8b5f4174fc5e747f4c52d2100806","analyzedAt":"2026-08-11T16:32:30.678Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}