{"record":{"id":"e637353fda1f24d3","repo":"github/copilot-sdk","slug":"exception-of-type-system-objectdisposedexception","errorCode":null,"errorMessage":"Exception of type 'System.ObjectDisposedException' was thrown. (ObjectDisposedException(nameof(CopilotClient)))","messagePattern":"Exception of type 'System\\.ObjectDisposedException' was thrown\\. \\(ObjectDisposedException\\(nameof\\(CopilotClient\\)\\)\\)","errorType":"exception","errorClass":"ObjectDisposedException","httpStatus":null,"severity":"error","filePath":"dotnet/src/Client.cs","lineNumber":111,"sourceCode":"\n    /// <summary>\n    /// Client-global RPC handlers (e.g. the LLM inference provider adapter),\n    /// built once at construction and registered on every connection.\n    /// </summary>\n    private readonly ClientGlobalApiHandlers? _clientGlobalApis;\n\n    private sealed record LifecycleSubscription(Type EventType, Action<SessionLifecycleEvent> Handler);\n\n    /// <summary>\n    /// Gets the typed RPC client for server-scoped methods (no session required).\n    /// </summary>\n    /// <remarks>\n    /// The client must be started before accessing this property. Call <see cref=\"StartAsync\"/> before use.\n    /// </remarks>\n    /// <exception cref=\"ObjectDisposedException\">Thrown if the client has been disposed.</exception>\n    /// <exception cref=\"InvalidOperationException\">Thrown if the client is not started.</exception>\n    public ServerRpc Rpc => _disposed\n        ? throw new ObjectDisposedException(nameof(CopilotClient))\n        : _serverRpc ?? throw new InvalidOperationException(\"Client is not started. Call StartAsync first.\");\n\n    /// <summary>\n    /// Gets the actual TCP port the runtime is listening on, if using TCP transport.\n    /// </summary>\n    public int? RuntimePort => _actualPort;\n\n    /// <summary>\n    /// Creates a new instance of <see cref=\"CopilotClient\"/>.\n    /// </summary>\n    /// <param name=\"options\">Options for creating the client. If null, default options are used.</param>\n    /// <example>\n    /// <code>\n    /// // Default options - spawns the bundled runtime using stdio\n    /// var client = new CopilotClient();\n    ///\n    /// // Connect to an existing runtime\n    /// var client = new CopilotClient(new CopilotClientOptions { Connection = RuntimeConnection.ForUri(\"localhost:3000\") });","sourceCodeStart":93,"sourceCodeEnd":129,"githubUrl":"https://github.com/github/copilot-sdk/blob/cd8cf15dc3f9e762615790aaed0a771a0f392755/dotnet/src/Client.cs#L93-L129","documentation":"The CopilotClient.Rpc property getter throws ObjectDisposedException(nameof(CopilotClient)) when the client has already been disposed. It guards access to the internal server RPC channel so callers never touch a dead connection.","triggerScenarios":"Reading the Rpc property after calling DisposeAsync/Dispose on the CopilotClient.","commonSituations":"Background tasks or event handlers outliving the client's lifetime; accessing Rpc from a finalizer or after a cancellation-triggered shutdown; holding a stale client reference across a reconnect cycle.","solutions":["Ensure all Rpc usage completes before disposing the client","Check the client's disposed state before accessing Rpc","Recreate a new CopilotClient (and StartAsync) if disposal was unintentional"],"exampleFix":"// before\nvar rpc = client.Rpc;\n// after\nif (client is { }) { var rpc = client.Rpc; } // only while client is alive/not disposed","handlingStrategy":"try-catch","validationCode":"// no public disposed flag; track it at the call site\nbool clientAlive = !disposedFlag;","typeGuard":null,"tryCatchPattern":"try { var rpc = client.Rpc; } catch (ObjectDisposedException) { client = new CopilotClient(options); await client.StartAsync(); var rpc = client.Rpc; }","preventionTips":["Scope the client so all Rpc consumers finish before disposal","Dispose the client only after in-flight work completes","Track disposal in a wrapper owning the client lifetime"],"tags":["dotnet","disposed","lifecycle"],"backgroundTag":"invalid-state-transition","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"}