{"record":{"id":"0cd4dacea4c10bc0","repo":"github/copilot-sdk","slug":"unknown-session-sessionid","errorCode":null,"errorMessage":"Unknown session {sessionId}","messagePattern":"Unknown session (.+?)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"dotnet/src/Client.cs","lineNumber":2692,"sourceCode":"            }\n\n            rpc = new JsonRpc(\n                outputStream,\n                inputStream,\n                SerializerOptionsForMessageFormatter,\n                _logger);\n\n            var handler = new RpcHandler(this);\n            rpc.SetLocalRpcMethod(\"session.event\", handler.OnSessionEvent);\n            rpc.SetLocalRpcMethod(\"session.lifecycle\", handler.OnSessionLifecycle);\n            rpc.SetLocalRpcMethod(\"userInput.request\", handler.OnUserInputRequest);\n            rpc.SetLocalRpcMethod(\"exitPlanMode.request\", handler.OnExitPlanModeRequest);\n            rpc.SetLocalRpcMethod(\"autoModeSwitch.request\", handler.OnAutoModeSwitchRequest);\n            rpc.SetLocalRpcMethod(\"hooks.invoke\", handler.OnHooksInvoke);\n            rpc.SetLocalRpcMethod(\"systemMessage.transform\", handler.OnSystemMessageTransform);\n            ClientSessionApiRegistration.RegisterClientSessionApiHandlers(rpc, sessionId =>\n            {\n                var session = GetSession(sessionId) ?? throw new ArgumentException($\"Unknown session {sessionId}\");\n                return session.ClientSessionApis;\n            });\n            if (_clientGlobalApis is not null)\n            {\n                ClientGlobalApiRegistration.RegisterClientGlobalApiHandlers(rpc, _clientGlobalApis);\n            }\n            rpc.StartListening();\n            _ = CancelExternalToolsWhenConnectionClosesAsync(rpc);\n            LoggingHelpers.LogTiming(_logger, LogLevel.Debug, null,\n                \"CopilotClient.ConnectToServerAsync transport setup complete. Elapsed={Elapsed}\",\n                setupTimestamp);\n\n            var connection = new Connection(rpc, cliProcess, networkStream, stderrPump, ffiHost);\n            _serverRpc = connection.Server;\n\n            return connection;\n        }\n        catch","sourceCodeStart":2674,"sourceCodeEnd":2710,"githubUrl":"https://github.com/github/copilot-sdk/blob/cd8cf15dc3f9e762615790aaed0a771a0f392755/dotnet/src/Client.cs#L2674-L2710","documentation":"The RPC layer resolves incoming client-session-API calls to a CopilotSession via GetSession(sessionId); if no session with that id is tracked, an ArgumentException with \"Unknown session {sessionId}\" is thrown. This is registered globally for all client-session API handlers, so any RPC arriving for a stale or foreign session id surfaces here.","triggerScenarios":"An RPC method call targeting ClientSessionApis arrives with a sessionId that is not in the client's _sessions map (GetSession returned null), e.g. after the session was disposed or the id never existed.","commonSituations":"CLI sends a notification/request for a session the client already disposed; a resumed/reconnected client receives events for sessions created by a previous client instance; a typo'd or stale sessionId cached by caller code.","solutions":["Verify the session was created by this client instance and keep a reference to its SessionId.","Don't dispose sessions (or the whole client) while the CLI may still reference them; await session teardown before disposing.","If reconnecting, recreate sessions rather than reusing ids from a previous client lifetime.","Wrap session lookups with a null check instead of assuming the id is valid."],"exampleFix":"// before\nvar session = client.GetSession(sessionId);\nawait session.SendAsync(msg); // ArgumentException: Unknown session\n\n// after\nvar session = client.GetSession(sessionId);\nif (session is null)\n{\n    session = await client.CreateSessionAsync(); // recreate instead of using stale id\n}\nawait session.SendAsync(msg);","handlingStrategy":"try-catch","validationCode":"if (client.GetSession(sessionId) is null)\n{\n    // recreate or skip\n}","typeGuard":"bool IsKnownSession(CopilotClient c, string id) => c.GetSession(id) is not null;","tryCatchPattern":"try { var s = GetOrThrow(sessionId); ... }\ncatch (ArgumentException ex) when (ex.Message.StartsWith(\"Unknown session\"))\n{ /* recreate session or drop stale request */ }","preventionTips":["Track session ids in app state only while the owning client is alive","Clear cached session ids on client restart/reconnect","Await session disposal before tearing down the client"],"tags":["session","rpc","lifecycle","dotnet"],"backgroundTag":"record-not-found","analyzedSha":"cd8cf15dc3f9e762615790aaed0a771a0f392755","analyzedAt":"2026-09-09T18:32:31.973Z","contentChangedAt":"2026-09-09T18:32:31.973Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}