{"record":{"id":"aa2ca0c050883ee3","repo":"microsoft/autogen","slug":"subscription-with-id-subscriptionid-does-not-exi","errorCode":null,"errorMessage":"Subscription with id {subscriptionId} does not exist.","messagePattern":"Subscription with id (.+?) does not exist\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"dotnet/src/Microsoft.AutoGen/Core.Grpc/GrpcAgentRuntime.cs","lineNumber":78,"sourceCode":"        this.agentFactories.Add(type, factoryFunc);\n        return type;\n    }\n\n    public void AddSubscription(ISubscriptionDefinition subscription)\n    {\n        if (this.Subscriptions.ContainsKey(subscription.Id))\n        {\n            throw new Exception($\"Subscription with id {subscription.Id} already exists.\");\n        }\n\n        this.Subscriptions.Add(subscription.Id, subscription);\n    }\n\n    public bool RemoveSubscriptionAsync(string subscriptionId)\n    {\n        if (!this.Subscriptions.ContainsKey(subscriptionId))\n        {\n            throw new Exception($\"Subscription with id {subscriptionId} does not exist.\");\n        }\n\n        return this.Subscriptions.Remove(subscriptionId);\n    }\n\n    public HashSet<AgentType> RegisteredAgentTypes => this.agentFactories.Keys.ToHashSet();\n    public IEnumerable<IHostableAgent> LiveAgents => this.agentInstances.Values;\n}\n\npublic sealed class GrpcAgentRuntime : IHostedService, IAgentRuntime, IMessageSink<Message>, IDisposable\n{\n    public GrpcAgentRuntime(AgentRpc.AgentRpcClient client,\n                            IHostApplicationLifetime hostApplicationLifetime,\n                            IServiceProvider serviceProvider,\n                            ILogger<GrpcAgentRuntime> logger,\n                            bool strictMessageDeserialization = false)\n    {\n        this._client = client;","sourceCodeStart":60,"sourceCodeEnd":96,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/src/Microsoft.AutoGen/Core.Grpc/GrpcAgentRuntime.cs#L60-L96","documentation":"GrpcAgentTypesHost.RemoveSubscriptionAsync throws when removing a subscription id that is not in the Subscriptions dictionary. Despite the name (and bool return), it fails fast on unknown ids instead of returning false — the guard precedes the Remove call.","triggerScenarios":"Calling RemoveSubscriptionAsync(id) after the subscription was already removed, was never added on this host instance, or when the id string does not exactly match (case/whitespace/encoding differences).","commonSituations":"Cleanup/teardown code paired with startup that partially failed; duplicate disposal paths (Dispose plus explicit teardown); passing a subscription id obtained from a different host/process; re-creating hosts across reconnects while tracking ids externally.","solutions":["Guard with host.Subscriptions.ContainsKey(id) before removal.","Track which ids you actually added (add/remove symmetrically in the same component).","Normalize id strings (trim, exact casing) at both registration and removal sites."],"exampleFix":"// before\nhost.RemoveSubscriptionAsync(\"sub1\"); // throws if never added / already removed\n\n// after\nif (host.Subscriptions.ContainsKey(\"sub1\"))\n{\n    host.RemoveSubscriptionAsync(\"sub1\");\n}","handlingStrategy":"validation","validationCode":"if (host.Subscriptions.ContainsKey(subscriptionId))\n{\n    host.RemoveSubscriptionAsync(subscriptionId);\n}","typeGuard":"static bool CanRemoveSubscription(GrpcAgentTypesHost host, string id) =>\n    host.Subscriptions.ContainsKey(id);","tryCatchPattern":null,"preventionTips":["Track the ids you added and remove exactly those; make teardown idempotent.","Use the exact same string (casing included) for add and remove."],"tags":["grpc","subscriptions","pubsub","cleanup"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}