{"record":{"id":"5f320d5712b930ee","repo":"dotnet/aspnetcore","slug":"the-send-method-cannot-be-called-if-the-connecti","errorCode":null,"errorMessage":"The 'send' method cannot be called if the connection is not active.","messagePattern":"The 'send' 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":598,"sourceCode":"                } catch (Exception ex) {\n                    logger.warn(\"Invoking 'onClosed' method failed:\", ex);\n                }\n            }\n        }\n    }\n\n    /**\n     * Invokes a hub method on the server using the specified method name.\n     * Does not wait for a response from the receiver.\n     *\n     * @param method The name of the server method to invoke.\n     * @param args   The arguments to be passed to the method.\n     */\n    public void send(String method, Object... args) {\n        this.state.lock();\n        try {\n            if (this.state.getHubConnectionState() != HubConnectionState.CONNECTED) {\n                throw new RuntimeException(\"The 'send' method cannot be called if the connection is not active.\");\n            }\n            sendInvocationMessage(method, args);\n        } finally {\n            this.state.unlock();\n        }\n    }\n\n    private void sendInvocationMessage(String method, Object[] args) {\n        sendInvocationMessage(method, args, null, false);\n    }\n\n    private void sendInvocationMessage(String method, Object[] args, String id, Boolean isStreamInvocation) {\n        List<String> streamIds = new ArrayList<>();\n        List<Observable> streams = new ArrayList<>();\n        args = checkUploadStream(args, streamIds, streams);\n        InvocationMessage invocationMessage;\n        if (isStreamInvocation) {\n            invocationMessage = new StreamInvocationMessage(null, id, method, args, streamIds);","sourceCodeStart":580,"sourceCodeEnd":616,"githubUrl":"https://github.com/dotnet/aspnetcore/blob/294cab2f9b2e03af6b953820c7ab497c3c8b7ad9/src/SignalR/clients/java/signalr/core/src/main/java/com/microsoft/signalr/HubConnection.java#L580-L616","documentation":"A RuntimeException thrown by send(String method, Object... args) when the HubConnection state is not CONNECTED. send() is fire-and-forget and requires an active connection to deliver the InvocationMessage; calling it while CONNECTING/DISCONNECTED would lose the message. The guard fails fast rather than silently dropping.","triggerScenarios":"Calling connection.send(\"method\", arg) before start() completes, after stop(), or while the connection is reconnecting/CONNECTING. The state check at line 597 throws immediately.","commonSituations":"Sending immediately after constructing the HubConnection without awaiting start(). Sending in a callback that fires after the connection dropped. Sending in error-handling code that runs post-disconnect. Reconnect logic that doesn't wait for CONNECTED before resuming sends.","solutions":["Await connection.start().blockingAwait() (or subscribe and only send on onComplete) before calling send().","Guard each send with a check: if (connection.getConnectionState() == HubConnectionState.CONNECTED) { connection.send(...); }.","Queue outgoing messages during reconnect and flush once state returns to CONNECTED.","Register an onClosed callback to pause sending when disconnected."],"exampleFix":"// before\nHubConnection conn = HubConnectionBuilder.create(url).build();\nconn.send(\"Greet\", \"hi\"); // throws: not started yet\nconn.start().blockingAwait();\n\n// after\nHubConnection conn = HubConnectionBuilder.create(url).build();\nconn.start().blockingAwait();\nif (conn.getConnectionState() == HubConnectionState.CONNECTED) {\n    conn.send(\"Greet\", \"hi\");\n}","handlingStrategy":"validation","validationCode":"if (connection.getConnectionState() == HubConnectionState.CONNECTED) {\n    connection.send(\"method\", arg);\n} else {\n    // queue or log; do not call send()\n}","typeGuard":"boolean canSend = connection.getConnectionState() == HubConnectionState.CONNECTED;","tryCatchPattern":"try {\n    connection.send(\"method\", arg);\n} catch (RuntimeException e) {\n    if (e.getMessage().contains(\"'send' method cannot be called\")) {\n        // not connected; queue the message and retry on reconnect\n    } else { throw e; }\n}","preventionTips":["Always gate send() on getConnectionState() == CONNECTED.","Await start() before sending, and pause sending in onClosed callbacks.","Maintain an outbound queue drained on (re)connect to avoid losing messages."],"tags":["signalr","java","lifecycle","send","state-machine"],"analyzedSha":"294cab2f9b2e03af6b953820c7ab497c3c8b7ad9","analyzedAt":"2026-08-06T20:08:02.189Z","schemaVersion":2},"datasetVersion":"2026-08-06T23:17:07.152Z"}