{"record":{"id":"7be77088d15cba1e","repo":"github/copilot-sdk","slug":"failed-to-register-github-token-provider","errorCode":null,"errorMessage":"Failed to register GitHub token provider.","messagePattern":"Failed to register GitHub token provider\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"dotnet/src/Client.cs","lineNumber":2079,"sourceCode":"        {\n            throw new ArgumentException(\n                $\"{nameof(SessionConfigBase.GitHubToken)} and {nameof(SessionConfigBase.GitHubTokenProvider)} cannot be used together.\",\n                nameof(config));\n        }\n    }\n\n    private string? RegisterGitHubTokenProvider(\n        Func<GitHubTokenProviderArgs, Task<GitHubTokenProviderResult>>? provider)\n    {\n        if (provider is null)\n        {\n            return null;\n        }\n\n        var registrationId = Guid.NewGuid().ToString();\n        if (!_gitHubTokenProviders.TryAdd(registrationId, provider))\n        {\n            throw new InvalidOperationException(\"Failed to register GitHub token provider.\");\n        }\n        return registrationId;\n    }\n\n    internal void UnregisterGitHubTokenProvider(string registrationId)\n        => _gitHubTokenProviders.TryRemove(registrationId, out _);\n\n    private void ClearGitHubTokenProviders() => _gitHubTokenProviders.Clear();\n\n    private sealed class GitHubTokenAdapter(CopilotClient client) : IGitHubTokenHandler\n    {\n        public async Task<GitHubTokenAcquireResult> GetTokenAsync(\n            GitHubTokenAcquireRequest request,\n            CancellationToken cancellationToken = default)\n        {\n            if (!client._gitHubTokenProviders.TryGetValue(request.RegistrationId, out var provider))\n            {\n                throw new InvalidOperationException(","sourceCodeStart":2061,"sourceCodeEnd":2097,"githubUrl":"https://github.com/github/copilot-sdk/blob/cd8cf15dc3f9e762615790aaed0a771a0f392755/dotnet/src/Client.cs#L2061-L2097","documentation":"Thrown as InvalidOperationException when the client fails to record a GitHub token provider in its internal concurrent registry, i.e. the ConcurrentDictionary.TryAdd of a freshly generated GUID registration ID unexpectedly fails. Since the ID is a new Guid.NewGuid() string, this practically indicates an internal state problem (ID collision or corrupted registry).","triggerScenarios":"Creating a session with config.GitHubTokenProvider set, during RegisterGitHubTokenProvider, when _gitHubTokenProviders.TryAdd(registrationId, provider) returns false for a brand-new GUID key.","commonSituations":"Extremely rare in practice — would require a GUID collision or a manually seeded registry with the same key; may appear if client internals were modified or if the client instance is being used in an unexpected/disposed state.","solutions":["Retry the session creation — a GUID collision is effectively impossible to repeat","If reproducible, inspect whether custom code touched the client's internal registry or subclassed the client","Create a fresh CopilotClient instance and retry; report a bug if it persists","Ensure the provider delegate itself is not null/throwing before registration"],"exampleFix":"// before\nvar session = await client.CreateSessionAsync(new SessionConfig { GitHubTokenProvider = p });\n// after\ntry { var session = await client.CreateSessionAsync(new SessionConfig { GitHubTokenProvider = p }); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"token provider\")) { client = new CopilotClient(options); /* retry */ }","handlingStrategy":"try-catch","validationCode":"if (provider is null) throw new ArgumentNullException(nameof(provider));","typeGuard":null,"tryCatchPattern":"try { session = await client.CreateSessionAsync(cfg); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Failed to register GitHub token provider\"))\n{ client = new CopilotClient(opts); session = await client.CreateSessionAsync(cfg); }","preventionTips":["Ensure the provider delegate is non-null before config","Don't subclass/tamper with client internals","Retry session creation once on this rare error","Report persistent occurrences as SDK bugs"],"tags":["internal-state","github-token","session","dotnet"],"backgroundTag":"internal-invariant-violation","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"}