github/copilot-sdk · error · InvalidOperationException
Failed to register GitHub token provider.
Error message
Failed to register GitHub token provider.
What it means
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).
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
Example fix
// before
var session = await client.CreateSessionAsync(new SessionConfig { GitHubTokenProvider = p });
// after
try { var session = await client.CreateSessionAsync(new SessionConfig { GitHubTokenProvider = p }); }
catch (InvalidOperationException ex) when (ex.Message.Contains("token provider")) { client = new CopilotClient(options); /* retry */ } Defensive patterns
Strategy: try-catch
Validate before calling
if (provider is null) throw new ArgumentNullException(nameof(provider));
Try / catch
try { session = await client.CreateSessionAsync(cfg); }
catch (InvalidOperationException ex) when (ex.Message.Contains("Failed to register GitHub token provider"))
{ client = new CopilotClient(opts); session = await client.CreateSessionAsync(cfg); } Prevention
- 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
When it happens
Trigger: Creating a session with config.GitHubTokenProvider set, during RegisterGitHubTokenProvider, when _gitHubTokenProviders.TryAdd(registrationId, provider) returns false for a brand-new GUID key.
Common situations: 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.
Understand the failure class
Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.
Related errors
- CopilotClient is in Mode = CopilotClientMode.Empty but the…
- session.create response did not include a sessionId.
- session.create returned sessionId
- Failed to delete session
- {response.Error ?? "Failed to set foreground session"}
AI-assisted analysis of github/copilot-sdk@cd8cf15dc3 (2026-09-09).
Data as JSON: /api/errors/7be77088d15cba1e.
Report an issue: GitHub.
Appendix: source
Thrown at dotnet/src/Client.cs:2079
{
throw new ArgumentException(
$"{nameof(SessionConfigBase.GitHubToken)} and {nameof(SessionConfigBase.GitHubTokenProvider)} cannot be used together.",
nameof(config));
}
}
private string? RegisterGitHubTokenProvider(
Func<GitHubTokenProviderArgs, Task<GitHubTokenProviderResult>>? provider)
{
if (provider is null)
{
return null;
}
var registrationId = Guid.NewGuid().ToString();
if (!_gitHubTokenProviders.TryAdd(registrationId, provider))
{
throw new InvalidOperationException("Failed to register GitHub token provider.");
}
return registrationId;
}
internal void UnregisterGitHubTokenProvider(string registrationId)
=> _gitHubTokenProviders.TryRemove(registrationId, out _);
private void ClearGitHubTokenProviders() => _gitHubTokenProviders.Clear();
private sealed class GitHubTokenAdapter(CopilotClient client) : IGitHubTokenHandler
{
public async Task<GitHubTokenAcquireResult> GetTokenAsync(
GitHubTokenAcquireRequest request,
CancellationToken cancellationToken = default)
{
if (!client._gitHubTokenProviders.TryGetValue(request.RegistrationId, out var provider))
{
throw new InvalidOperationException(View on GitHub (pinned to cd8cf15dc3)