{"record":{"id":"fd86a70cabbb4c4b","repo":"quarkusio/quarkus","slug":"no-active-context-found-for","errorCode":null,"errorMessage":"No active context found for: ","messagePattern":"No active context found for: ","errorType":"exception","errorClass":"ContextNotActiveException","httpStatus":null,"severity":"error","filePath":"independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/InstanceImpl.java","lineNumber":231,"sourceCode":"\n    @Override\n    public boolean isUnsatisfied() {\n        return beans().isEmpty();\n    }\n\n    @Override\n    public boolean isAmbiguous() {\n        return beans().size() > 1;\n    }\n\n    @Override\n    public void destroy(Object instance) {\n        Objects.requireNonNull(instance);\n        if (instance instanceof ClientProxy) {\n            ClientProxy proxy = (ClientProxy) instance;\n            InjectableContext context = Arc.requireContainer().getActiveContext(proxy.arc_bean().getScope());\n            if (context == null) {\n                throw new ContextNotActiveException(\"No active context found for: \" + proxy.arc_bean().getScope());\n            }\n            context.destroy(proxy.arc_bean());\n        } else {\n            // First try to destroy a dependent instance\n            if (!creationalContext.removeDependentInstance(instance, true)) {\n                // If not successful then try the singleton context\n                SingletonContext singletonContext = (SingletonContext) Arc.requireContainer().getActiveContext(Singleton.class);\n                singletonContext.destroyInstance(instance);\n            }\n        }\n    }\n\n    @Override\n    public InstanceHandle<T> getHandle() {\n        return getHandle(bean());\n    }\n\n    @Override","sourceCodeStart":213,"sourceCodeEnd":249,"githubUrl":"https://github.com/quarkusio/quarkus/blob/e1c734241f34c7919086ceb4c9262b4a58f6de44/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/InstanceImpl.java#L213-L249","documentation":"Instance.destroy(Object) requires an active context for the scope of the bean being destroyed when the instance is a client proxy of a normal-scoped bean. ArC looks up the active context via Arc.requireContainer().getActiveContext(scope); if none is active it throws ContextNotActiveException. For dependent instances it destroys them directly via the CreationalContext, so this error only concerns normal-scoped proxies.","triggerScenarios":"Calling injectableInstance.destroy(clientProxy) outside the scope's active window, e.g. destroying a @RequestScoped bean outside a request, a @SessionScoped bean without a session, or @ApplicationScoped after container shutdown in tests.","commonSituations":"Background threads/async tasks touching request-scoped instances; calling destroy() in a test without @ActivateRequestContext; cleanup code running after the CDI request context ended.","solutions":["Only call destroy() for normal-scoped beans while their context is active (inside the request/session)","Annotate the destroying code with @ActivateRequestContext (io.quarkus.arc) to make the request context active","Destroy the dependent instance (not the proxy): keep the unwrapped dependent object and destroy that","Re-architect so cleanup happens within the scope's lifecycle instead of manual destroy()"],"exampleFix":"// before (no active context in background thread)\nexecutor.submit(() -> instance.destroy(requestScopedBean));\n// after\n@ActivateRequestContext\nvoid cleanup() { instance.destroy(requestScopedBean); }","handlingStrategy":"try-catch","validationCode":"ScopeInstance scope = Arc.container().getActiveScope(bean instanceof ClientProxy\n    ? ((ClientProxy) bean).arc_bean().getScope() : Dependent.class);\nif (scope == null) throw new IllegalStateException(\"Context not active; cannot destroy\");","typeGuard":"static boolean isDestroyableNow(Object instance) {\n    if (!(instance instanceof ClientProxy p)) return true; // dependent instances fine\n    return Arc.container().getActiveContext(p.arc_bean().getScope()) != null;\n}","tryCatchPattern":"try {\n    instance.destroy(bean);\n} catch (ContextNotActiveException e) {\n    log.warnf(\"Context not active for %s; deferring destroy\", bean);\n    // retry inside the proper scope or skip\n}","preventionTips":["Never destroy normal-scoped beans from background threads without @ActivateRequestContext","Prefer letting the container destroy beans via scope lifecycle instead of manual destroy()","For manual cleanup, destroy the dependent (unwrapped) instance, not the client proxy","In tests, use @ActivateRequestContext or QuarkusUnitTest so contexts are active"],"tags":["cdi","arc","context","scopes"],"backgroundTag":"context-not-active","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"}