{"record":{"id":"7d18da4c33290fa0","repo":"microsoft/autogen","slug":"subscription-with-id-subscription-id-already-exi","errorCode":null,"errorMessage":"Subscription with id {subscription.Id} already exists.","messagePattern":"Subscription with id (.+?) already exists\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"dotnet/src/Microsoft.AutoGen/Core.Grpc/GrpcAgentRuntime.cs","lineNumber":68,"sourceCode":"        return agentId;\n    }\n\n    public AgentType RegisterAgentFactory(AgentType type, Func<Contracts.AgentId, IAgentRuntime, ValueTask<IHostableAgent>> factoryFunc)\n    {\n        if (this.agentFactories.ContainsKey(type))\n        {\n            throw new Exception($\"Agent factory with type {type} already exists.\");\n        }\n\n        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}","sourceCodeStart":50,"sourceCodeEnd":86,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/src/Microsoft.AutoGen/Core.Grpc/GrpcAgentRuntime.cs#L50-L86","documentation":"GrpcAgentTypesHost.AddSubscription throws when a subscription with the same Id is already present in the Subscriptions dictionary. Subscription ids are expected to be unique per host; adding a semantically equal subscription twice is treated as a programming error rather than an idempotent no-op.","triggerScenarios":"Calling AddSubscription(subscription) twice with the same subscription.Id — e.g. re-adding a TypePrefixSubscription/TopicSubscription on restart-in-place, or two components subscribing to the same topic producing identical ids.","commonSituations":"Auto-generated subscription ids colliding (e.g. deterministic ids derived from topic prefix that are identical by design); startup code that re-runs on reconnect or hot reload; registering subscriptions both in a shared extension and in Program.cs; tests reusing one host.","solutions":["Check host.Subscriptions.ContainsKey(subscription.Id) before adding, or call RemoveSubscriptionAsync first if refreshing is intended.","Fix the id-generation logic so logically distinct subscriptions get distinct ids.","De-duplicate subscription registration in startup code paths."],"exampleFix":"// before\nhost.AddSubscription(new TypePrefixSubscription(\"events\", id: \"sub1\"));\nhost.AddSubscription(new TypePrefixSubscription(\"events\", id: \"sub1\")); // throws\n\n// after\nif (!host.Subscriptions.ContainsKey(\"sub1\"))\n{\n    host.AddSubscription(new TypePrefixSubscription(\"events\", id: \"sub1\"));\n}","handlingStrategy":"validation","validationCode":"if (!host.Subscriptions.ContainsKey(subscription.Id))\n{\n    host.AddSubscription(subscription);\n}","typeGuard":"static bool IsSubscriptionAdded(GrpcAgentTypesHost host, string id) =>\n    host.Subscriptions.ContainsKey(id);","tryCatchPattern":null,"preventionTips":["Keep add/remove of each subscription in the same component so ids stay balanced.","Ensure distinct subscriptions get distinct ids; review id-generation helpers for collisions."],"tags":["grpc","subscriptions","duplicate","pubsub"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}