{"record":{"id":"931fb9a56051b891","repo":"dotnet/aspnetcore","slug":"the-invoke-method-cannot-be-called-if-the-connec","errorCode":null,"errorMessage":"The 'invoke' method cannot be called if the connection is not active.","messagePattern":"The 'invoke' method cannot be called if the 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":675,"sourceCode":"                streams.add(stream);\n            }\n        }\n\n        return params.toArray();\n    }\n\n    /**\n     * Invokes a hub method on the server using the specified method name and arguments.\n     *\n     * @param method The name of the server method to invoke.\n     * @param args The arguments used to invoke the server method.\n     * @return A Completable that indicates when the invocation has completed.\n     */\n    public Completable invoke(String method, Object... args) {\n        this.state.lock();\n        try {\n            if (this.state.getHubConnectionState() != HubConnectionState.CONNECTED) {\n                throw new RuntimeException(\"The 'invoke' method cannot be called if the connection is not active.\");\n            }\n\n            ConnectionState connectionState = this.state.getConnectionStateUnsynchronized(false);\n            String id = connectionState.getNextInvocationId();\n\n            CompletableSubject subject = CompletableSubject.create();\n            InvocationRequest irq = new InvocationRequest(null, id);\n            connectionState.addInvocation(irq);\n\n            Subject<Object> pendingCall = irq.getPendingCall();\n\n            pendingCall.subscribe(result -> subject.onComplete(),\n                    error -> subject.onError(error),\n                    () -> subject.onComplete());\n\n            // Make sure the actual send is after setting up the callbacks otherwise there is a race\n            // where the map doesn't have the callbacks yet when the response is returned\n            sendInvocationMessage(method, args, id, false);","sourceCodeStart":657,"sourceCodeEnd":693,"githubUrl":"https://github.com/dotnet/aspnetcore/blob/3600ca084e9c8b5f4174fc5e747f4c52d2100806/src/SignalR/clients/java/signalr/core/src/main/java/com/microsoft/signalr/HubConnection.java#L657-L693","documentation":"The Completable-returning invoke(method, args) overload checks that the connection is in the CONNECTED state before allocating an invocation id and dispatching. Any other state (CONNECTING, RECONNECTING, DISCONNECTED) causes an immediate RuntimeException under the state lock.","triggerScenarios":"Calling the Completable invoke() before start() finished, after stop(), or while the connection is mid-reconnect. The check happens inside this.state.lock()/unlock() so the read is consistent but the call still fails if the state is not CONNECTED.","commonSituations":"Invoking from a background thread that does not synchronize with the connection lifecycle; an onClosed handler that did not restart the connection before the next invoke; invoking from application shutdown after stop() was already called.","solutions":["Chain the invoke after start() completes: hub.start().andThen(hub.invoke(\"M\")).subscribe(...).","Reconnect on close: register onClosed(...) to call start() again, then retry the invoke.","Track connection state in your own flag updated from onClosed/onReconnected and skip or queue invocations when not connected."],"exampleFix":"// before\nhub.start();\nhub.invoke(\"Method\").await();\n\n// after\nhub.start().blockingAwait();\nhub.invoke(\"Method\").await();","handlingStrategy":"validation","validationCode":"if (hub.getConnectionState() != HubConnectionState.CONNECTED) {\n  // do not call invoke; reconnect first\n  return;\n}","typeGuard":null,"tryCatchPattern":"try {\n  hub.invoke(method, args).await();\n} catch (RuntimeException e) {\n  if (e.getMessage().contains(\"connection is not active\")) {\n    // schedule retry after reconnect\n  } else throw e;\n}","preventionTips":["Chain invokes off start(): hub.start().andThen(hub.invoke(...)).","Subscribe to onReconnected to resume business logic.","Never call invoke() from onClosed before the new connection is up."],"tags":["signalr","java","state","invocation","race-condition"],"backgroundTag":null,"analyzedSha":"3600ca084e9c8b5f4174fc5e747f4c52d2100806","analyzedAt":"2026-08-11T16:32:30.678Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}