{"record":{"id":"90f1650d8a46a2ac","repo":"quarkusio/quarkus","slug":"more-than-one-context-object-for-the-given-scope","errorCode":null,"errorMessage":"More than one context object for the given scope: ${selectedContext} ${context}","messagePattern":"More than one context object for the given scope: (.+?) (.+?)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/ClientProxies.java","lineNumber":48,"sourceCode":"        T result = context.getIfActive(bean, ClientProxies::newCreationalContext);\n        if (result == null) {\n            throw notActive(bean);\n        }\n        return result;\n    }\n\n    public static <T> T getDelegate(InjectableBean<T> bean) {\n        List<InjectableContext> contexts = Arc.requireContainer().getContexts(bean.getScope());\n        T result = null;\n        if (contexts.size() == 1) {\n            result = contexts.get(0).getIfActive(bean, ClientProxies::newCreationalContext);\n        } else {\n            InjectableContext selectedContext = null;\n            for (int i = 0; i < contexts.size(); i++) {\n                InjectableContext context = contexts.get(i);\n                if (result != null) {\n                    if (context.isActive()) {\n                        throw new IllegalArgumentException(\n                                \"More than one context object for the given scope: \" + selectedContext + \" \" + context);\n                    }\n                } else {\n                    result = context.getIfActive(bean, ClientProxies::newCreationalContext);\n                    if (result != null) {\n                        selectedContext = context;\n                    }\n                }\n            }\n        }\n        if (result == null) {\n            throw notActive(bean);\n        }\n        return result;\n    }\n\n    private static ContextNotActiveException notActive(InjectableBean<?> bean) {\n        String msg = String.format(","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/quarkusio/quarkus/blob/e1c734241f34c7919086ceb4c9262b4a58f6de44/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/ClientProxies.java#L30-L66","documentation":"ClientProxies.getDelegate() resolves a scope's client proxy target by asking each registered InjectableContext for that scope to return the instance if active. If two contexts for the same scope are both active AND both return a value, ArC cannot pick one deterministically and throws IllegalArgumentException naming both contexts. CDI allows at most one active context per normal scope.","triggerScenarios":"Calling a method on a client proxy (normal-scoped bean) when two context objects registered for the same scope annotation are simultaneously active and each yields an instance (e.g. a request context registered twice, or a custom context overlapping a built-in one).","commonSituations":"Custom @ActivatedScope/@RequestScoped-like context implementations registered via servlet/request filters that start a second context while the built-in one is active; testing harnesses that start contexts multiple times; extension bugs double-registering a context.","solutions":["Find code that activates the scope twice (e.g. RequestContext.activate() in both a filter and a test harness) and remove the duplicate activation.","If you register custom contexts, verify no custom context's scope overlaps a built-in active context for the same scope annotation.","Terminate contexts properly (try/finally with terminate/deactivate) so stale active contexts don't coexist."],"exampleFix":"// before\nrequestContext.activate(); // also started by the test filter -> two active contexts\n\n// after\nif (!Arc.container().requestContext().isActive()) {\n    Arc.container().requestContext().activate();\n}","handlingStrategy":"validation","validationCode":"long active = Arc.container().getActiveContexts(MyScoped.class).stream() // or per-impl equivalents\n    .filter(InjectableContext::isActive).count();\nif (active > 1) throw new IllegalStateException(\"scope activated more than once\");","typeGuard":"static boolean isSingleActiveScope(Class<? extends Annotation> scope) {\n    long n = Arc.container().getContexts(scope).stream()\n        .filter(InjectableContext::isActive).count();\n    return n <= 1;\n}","tryCatchPattern":"try {\n    myScopedBean.doWork();\n} catch (IllegalArgumentException e) {\n    if (!e.getMessage().contains(\"More than one context object\")) throw e;\n    // deactivate duplicate context or fail fast with a clear message\n}","preventionTips":["Always check isActive() before activating a context.","Terminate contexts in finally blocks to avoid lingering activations.","Give custom contexts their own scope annotation; never reuse built-in scopes.","Avoid activating the same scope in both test harness and production filters."],"tags":["cdi","context","client-proxy","quarkus-arc","scope"],"backgroundTag":"duplicate-active-context","analyzedSha":"e1c734241f34c7919086ceb4c9262b4a58f6de44","analyzedAt":"2026-09-05T17:01:29.979Z","contentChangedAt":"2026-09-05T17:01:29.979Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}