{"record":{"id":"fd8afdfeaa9b2c58","repo":"quarkusio/quarkus","slug":"more-than-one-active-context-object-for-the-given","errorCode":null,"errorMessage":"More than one active context object for the given scope: ${selected} ${context}","messagePattern":"More than one active 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/Contexts.java","lineNumber":118,"sourceCode":"                    SessionScoped.class);\n        }\n    }\n\n    InjectableContext getActiveContext(Class<? extends Annotation> scopeType) {\n        // Application/Singleton/Dependent context is always active and it's not possible to register a custom context for these scopes\n        if (ApplicationScoped.class.equals(scopeType)) {\n            return applicationContext;\n        } else if (Singleton.class.equals(scopeType)) {\n            return singletonContext;\n        } else if (Dependent.class.equals(scopeType)) {\n            return dependentContext;\n        }\n        List<InjectableContext> contextsForScope = getContexts(scopeType);\n        InjectableContext selected = null;\n        for (InjectableContext context : contextsForScope) {\n            if (context.isActive()) {\n                if (selected != null) {\n                    throw new IllegalArgumentException(\n                            \"More than one active context object for the given scope: \" + selected + \" \" + context);\n                }\n                selected = context;\n            }\n        }\n        return selected;\n    }\n\n    List<InjectableContext> getContexts(Class<? extends Annotation> scopeType) {\n        // Optimize for built-in scopes - this method is used internally during client proxy invocation\n        if (ApplicationScoped.class.equals(scopeType)) {\n            return applicationContextSingleton;\n        } else if (RequestScoped.class.equals(scopeType)) {\n            return requestContextSingleton;\n        } else if (Singleton.class.equals(scopeType)) {\n            return singletonContextSingleton;\n        } else if (Dependent.class.equals(scopeType)) {\n            return dependentContextSingleton;","sourceCodeStart":100,"sourceCodeEnd":136,"githubUrl":"https://github.com/quarkusio/quarkus/blob/e1c734241f34c7919086ceb4c9262b4a58f6de44/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/Contexts.java#L100-L136","documentation":"Contexts.getActiveContext(scopeType) iterates all registered contexts for a scope and returns the single active one. CDI permits at most one active context per normal scope; if two are simultaneously active, ArC throws IllegalArgumentException naming both. This protects deterministic bean instance resolution for client proxies and injected references.","triggerScenarios":"Accessing a bean (directly or via client proxy) while two InjectableContext instances registered for the same scope annotation report isActive() == true — e.g. nested activation of a custom scope, or a custom context whose scope collides with a built-in one.","commonSituations":"Custom context extensions (e.g. a thread/transaction-scoped context) activated on top of an already-active matching context; test frameworks that activate request/session contexts in setup code while the runtime also activated them; duplicate context registration in a portable extension.","solutions":["Audit activation code (activate()/begin()) for the scope and remove nested or duplicate activations; check isActive() first.","If you register a custom context, ensure its scope annotation is unique and not shared with a built-in scope.","Ensure contexts are properly terminated in finally blocks so overlapping activations cannot linger.","Check ArC/Quarkus version mismatches in your build that might load two copies of a context provider."],"exampleFix":"// before\ntransactionContext.begin(); // may already be active in outer code\n\n// after\nif (!transactionContext.isActive()) {\n    transactionContext.begin();\n}\ntry { /* work */ } finally { transactionContext.end(); }","handlingStrategy":"validation","validationCode":"long active = Arc.container().getContexts(MyScoped.class).stream()\n    .filter(InjectableContext::isActive).count();\nif (active > 1) {\n    throw new IllegalStateException(\"MyScope must have at most one active context, found \" + active);\n}","typeGuard":"static boolean hasSingleActiveContext(Class<? extends Annotation> scope) {\n    return Arc.container().getContexts(scope).stream()\n        .filter(InjectableContext::isActive).limit(2).count() <= 1;\n}","tryCatchPattern":"try {\n    return Arc.container().instance(MyBean.class).get();\n} catch (IllegalArgumentException e) {\n    if (!e.getMessage().contains(\"More than one active context\")) throw e;\n    // end the duplicate context or rethrow with diagnostics\n}","preventionTips":["Guard all context activations with isActive() checks.","Register custom contexts only under unique scope annotations.","Use try/finally to guarantee context termination and prevent overlaps.","Review portable extensions for duplicate addContext calls."],"tags":["cdi","context","scope","quarkus-arc"],"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-12T22:17:10.623Z"}