{"record":{"id":"d3be7fe6954425bd","repo":"dotnet/aspnetcore","slug":"httpconnection-stopconnection-error-was-called","errorCode":null,"errorMessage":"HttpConnection.stopConnection(${error}) was called while the connection is still in the connecting state.","messagePattern":"HttpConnection\\.stopConnection\\((.+?)\\) was called while the connection is still in the connecting state\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/SignalR/clients/ts/signalr/src/HttpConnection.ts","lineNumber":627,"sourceCode":"    }\n\n    private _stopConnection(error?: Error): void {\n        this._logger.log(LogLevel.Debug, `HttpConnection.stopConnection(${error}) called while in state ${this._connectionState}.`);\n\n        this.transport = undefined;\n\n        // If we have a stopError, it takes precedence over the error from the transport\n        error = this._stopError || error;\n        this._stopError = undefined;\n\n        if (this._connectionState === ConnectionState.Disconnected) {\n            this._logger.log(LogLevel.Debug, `Call to HttpConnection.stopConnection(${error}) was ignored because the connection is already in the disconnected state.`);\n            return;\n        }\n\n        if (this._connectionState === ConnectionState.Connecting) {\n            this._logger.log(LogLevel.Warning, `Call to HttpConnection.stopConnection(${error}) was ignored because the connection is still in the connecting state.`);\n            throw new Error(`HttpConnection.stopConnection(${error}) was called while the connection is still in the connecting state.`);\n        }\n\n        this._connectionGeneration++;\n\n        if (this._connectionState === ConnectionState.Disconnecting) {\n            // A call to stop() induced this call to stopConnection and needs to be completed.\n            // Any stop() awaiters will be scheduled to continue after the onclose callback fires.\n            this._stopPromiseResolver();\n        }\n\n        if (error) {\n            this._logger.log(LogLevel.Error, `Connection disconnected with error '${error}'.`);\n        } else {\n            this._logger.log(LogLevel.Information, \"Connection disconnected.\");\n        }\n\n        if (this._sendQueue) {\n            this._sendQueue.stop().catch((e) => {","sourceCodeStart":609,"sourceCodeEnd":645,"githubUrl":"https://github.com/dotnet/aspnetcore/blob/3600ca084e9c8b5f4174fc5e747f4c52d2100806/src/SignalR/clients/ts/signalr/src/HttpConnection.ts#L609-L645","documentation":"Thrown by HttpConnection.stopConnection when the connection-state machine is still in `Connecting` at the moment the underlying transport reports closure. stopConnection is designed to run only from the Connected/Disconnecting states; reaching it during Connecting means the transport fired onclose before the negotiate/connect handshake completed. The library treats this as an unrecoverable internal invariant violation.","triggerScenarios":"The selected transport's `connect` rejects or its `onclose` fires while HttpConnection is still awaiting the initial connect (state === Connecting). Common proxies: a LongPolling/SSE/WebSocket transport whose first request is aborted by the network or server while negotiation is in flight, or an early `stop()` colliding with a failing connect.","commonSituations":"Server returns 404/401/500 during the /negotiate POST, the access-token factory throws, a proxy resets the connection mid-handshake, or the caller invokes stop() immediately after start() races the connecting promise.","solutions":["Inspect the `error` argument carried in the message — it names the underlying transport failure that caused the early close.","Verify the negotiate endpoint is reachable and returns 200 with a valid negotiate response before the transport connects.","Ensure your access-token factory resolves cleanly and does not throw during connect.","Avoid calling connection.stop() before the start() promise has settled when running on flaky networks."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  await hubConnection.start();\n} catch (e) {\n  if (e instanceof Error && /stopConnection.*connecting state/i.test(e.message)) {\n    // inspect underlying cause, retry with backoff, or surface to user\n  } else throw e;\n}","preventionTips":["Await start() before issuing further calls.","Validate negotiate endpoint reachability and auth before connecting.","Avoid calling stop() before start() settles on unreliable networks."],"tags":["state-machine","transport","connection-lifecycle","race-condition"],"backgroundTag":null,"analyzedSha":"3600ca084e9c8b5f4174fc5e747f4c52d2100806","analyzedAt":"2026-08-11T16:32:30.678Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}