{"record":{"id":"70f56df14c585ea5","repo":"apache/pulsar","slug":"cannot-use-shareconfigured-when-resourcetypes-ha","errorCode":null,"errorMessage":"Cannot use shareConfigured() when resourceTypes has already been set","messagePattern":"Cannot use shareConfigured\\(\\) when resourceTypes has already been set","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"pulsar-client/src/main/java/org/apache/pulsar/client/impl/PulsarClientSharedResourcesBuilderImpl.java","lineNumber":259,"sourceCode":"            throw new IllegalStateException(\"Cannot set resourceTypes when shareConfigured() has already been called\");\n        }\n        return resourceTypes(List.of(sharedResource));\n    }\n\n    @Override\n    public PulsarClientSharedResourcesBuilder resourceTypes(\n            Collection<PulsarClientSharedResources.SharedResource> sharedResource) {\n        if (shareConfigured) {\n            throw new IllegalStateException(\"Cannot set resourceTypes when shareConfigured() has already been called\");\n        }\n        sharedResources.addAll(sharedResource);\n        return this;\n    }\n\n    @Override\n    public PulsarClientSharedResourcesBuilder shareConfigured() {\n        if (!sharedResources.isEmpty()) {\n            throw new IllegalStateException(\"Cannot use shareConfigured() when resourceTypes has already been set\");\n        }\n        shareConfigured = true;\n        return this;\n    }\n\n    @SuppressWarnings(\"unchecked\")\n    private <T extends ResourceConfig> T getOrCreateConfig(PulsarClientSharedResources.SharedResource sharedResource) {\n        return (T) resourceConfigs.computeIfAbsent(sharedResource, k -> {\n            switch (sharedResource.getType()) {\n                case EventLoopGroup:\n                    return new EventLoopResourceConfig();\n                case DnsResolver:\n                    return new DnsResolverResourceConfig();\n                case ThreadPool:\n                    return new ThreadPoolResourceConfig();\n                case Timer:\n                    return new TimerResourceConfig();\n                case MemoryLimitController:","sourceCodeStart":241,"sourceCodeEnd":277,"githubUrl":"https://github.com/apache/pulsar/blob/820761864ed8e2a7d2e52dd9763ad2ae117c1395/pulsar-client/src/main/java/org/apache/pulsar/client/impl/PulsarClientSharedResourcesBuilderImpl.java#L241-L277","documentation":"The inverse guard of the resourceTypes checks: shareConfigured() (share-everything mode) cannot be used once specific resource types were already registered, since the builder cannot represent both an exclusive list and share-all mode at once.","triggerScenarios":"Calling builder.resourceTypes(...) (either overload) and then builder.shareConfigured() on the same builder instance.","commonSituations":"Fluent chains that append shareConfigured() 'for good measure' after listing resources; merged configuration from two code modules each adding resources then finalizing with shareConfigured(); framework code that always finalizes with shareConfigured().","solutions":["Remove the shareConfigured() call when you are explicitly listing resourceTypes; the listed types already define what is shared.","Call shareConfigured() before any resourceTypes(...) call if share-all mode is intended, and skip resourceTypes entirely.","Use a new builder instance if the two configurations must coexist for different clients."],"exampleFix":"// before\nbuilder.resourceTypes(SharedResource.EVENT_LOOP_GROUP).shareConfigured(); // throws\n// after\nbuilder.resourceTypes(SharedResource.EVENT_LOOP_GROUP); // explicit list only","handlingStrategy":"validation","validationCode":"if (!resourcesToShare.isEmpty()) {\n    builder.resourceTypes(resourcesToShare); // do not also call shareConfigured()\n} else {\n    builder.shareConfigured();\n}","typeGuard":null,"tryCatchPattern":"try {\n    builder.shareConfigured();\n} catch (IllegalStateException e) {\n    log.warn(\"resourceTypes already set; explicit list wins, skipping share-all\");\n}","preventionTips":["Treat shareConfigured() as an alternative to resourceTypes, never a complement.","Build the resource list first; only fall back to shareConfigured() when the list is empty.","Keep builder construction in one factory method with a single mode branch.","Add a unit test asserting the builder chain you ship does not throw IllegalStateException."],"tags":["pulsar","client","builder","illegal-state"],"backgroundTag":"illegal-builder-state","analyzedSha":"820761864ed8e2a7d2e52dd9763ad2ae117c1395","analyzedAt":"2026-09-06T00:14:20.138Z","contentChangedAt":"2026-09-06T00:14:20.138Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}