{"record":{"id":"efcad0b0be370425","repo":"quarkusio/quarkus","slug":"cannot-destroy-instance-of-bean-no-activ","errorCode":null,"errorMessage":"Cannot destroy instance of \" + bean + \" - no active context found for: \" + bean.getScope()","messagePattern":"Cannot destroy instance of \" \\+ bean \\+ \" - no active context found for: \" \\+ bean\\.getScope\\(\\)","errorType":"exception","errorClass":"ContextNotActiveException","httpStatus":null,"severity":"error","filePath":"independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/AbstractInstanceHandle.java","lineNumber":65,"sourceCode":"        return bean;\n    }\n\n    protected abstract boolean isInstanceCreated();\n\n    protected abstract T instanceInternal();\n\n    @Override\n    public void destroy() {\n        if (isInstanceCreated() && DESTROYED_UPDATER.compareAndSet(this, 0, 1)) {\n            if (destroyLogic != null) {\n                destroyLogic.accept(instanceInternal());\n            } else if (bean != null) {\n                if (bean.getScope().equals(Dependent.class)) {\n                    destroyInternal();\n                } else {\n                    InjectableContext context = Arc.requireContainer().getActiveContext(bean.getScope());\n                    if (context == null) {\n                        throw new ContextNotActiveException(\n                                \"Cannot destroy instance of \" + bean + \" - no active context found for: \" + bean.getScope());\n                    }\n                    context.destroy(bean);\n                }\n            }\n        }\n    }\n\n    protected void destroyInternal() {\n        if (parentCreationalContext != null) {\n            parentCreationalContext.release();\n        } else {\n            bean.destroy(instanceInternal(), creationalContext);\n        }\n    }\n\n    @Override\n    public String toString() {","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/quarkusio/quarkus/blob/e1c734241f34c7919086ceb4c9262b4a58f6de44/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/AbstractInstanceHandle.java#L47-L83","documentation":"AbstractInstanceHandle.destroy() throws ContextNotActiveException when the bean's scope is not @Dependent but no active context exists for that scope at destroy time. Destroying a contextual instance requires its context to be active so the container can delegate destruction.","triggerScenarios":"Calling handle.destroy() for a handle of, say, @ApplicationScoped/@RequestScoped bean when the corresponding context is inactive — commonly destroying a request-scoped handle after the request context has ended, or outside any active request.","commonSituations":"Background threads (scheduler, async task) touching handles created on a request thread after the request ended; destroying handles in a shutdown callback where contexts were already torn down; manually managing instances of scoped beans.","solutions":["Destroy the handle while its scope's context is still active (e.g. within the request, via Arc.container().requestContext().activate()/terminate)","Activate the needed context before destroying: Arc.container().requestContext().activate() then destroy then terminate","Use @Dependent beans (destroyInternal works without context) if manual lifecycle control is intended","Move destruction into a CDI lifecycle hook where the context is guaranteed active"],"exampleFix":"// before\nhandle.destroy(); // ContextNotActiveException outside request\n// after\nArc.container().requestContext().activate();\ntry {\n    handle.destroy();\n} finally {\n    Arc.container().requestContext().terminate();\n}","handlingStrategy":"try-catch","validationCode":"if (Arc.container().getActiveContext(handle.getBean().getScope()) == null) { Arc.container().requestContext().activate(); }","typeGuard":null,"tryCatchPattern":"try { handle.destroy(); } catch (ContextNotActiveException e) { Arc.container().requestContext().activate(); handle.destroy(); Arc.container().requestContext().terminate(); }","preventionTips":["Destroy handles within the same request/thread that created them","Activate/terminate contexts explicitly in background threads","Prefer @Dependent for manually managed instances"],"tags":["cdi","context","lifecycle"],"backgroundTag":"cdi-context-not-active","analyzedSha":"e1c734241f34c7919086ceb4c9262b4a58f6de44","analyzedAt":"2026-09-05T17:01:29.979Z","contentChangedAt":"2026-09-05T17:01:29.979Z","schemaVersion":2},"datasetVersion":"2026-09-15T02:17:10.978Z"}