quarkusio/quarkus · error · ContextNotActiveException

Cannot destroy instance of " + bean + " - no active context

Error message

Cannot destroy instance of " + bean + " - no active context found for: " + bean.getScope()

What it means

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.

Source

Thrown at independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/AbstractInstanceHandle.java:65

        return bean;
    }

    protected abstract boolean isInstanceCreated();

    protected abstract T instanceInternal();

    @Override
    public void destroy() {
        if (isInstanceCreated() && DESTROYED_UPDATER.compareAndSet(this, 0, 1)) {
            if (destroyLogic != null) {
                destroyLogic.accept(instanceInternal());
            } else if (bean != null) {
                if (bean.getScope().equals(Dependent.class)) {
                    destroyInternal();
                } else {
                    InjectableContext context = Arc.requireContainer().getActiveContext(bean.getScope());
                    if (context == null) {
                        throw new ContextNotActiveException(
                                "Cannot destroy instance of " + bean + " - no active context found for: " + bean.getScope());
                    }
                    context.destroy(bean);
                }
            }
        }
    }

    protected void destroyInternal() {
        if (parentCreationalContext != null) {
            parentCreationalContext.release();
        } else {
            bean.destroy(instanceInternal(), creationalContext);
        }
    }

    @Override
    public String toString() {

View on GitHub (pinned to e1c734241f)

Solutions

  1. Destroy the handle while its scope's context is still active (e.g. within the request, via Arc.container().requestContext().activate()/terminate)
  2. Activate the needed context before destroying: Arc.container().requestContext().activate() then destroy then terminate
  3. Use @Dependent beans (destroyInternal works without context) if manual lifecycle control is intended
  4. Move destruction into a CDI lifecycle hook where the context is guaranteed active

Example fix

// before
handle.destroy(); // ContextNotActiveException outside request
// after
Arc.container().requestContext().activate();
try {
    handle.destroy();
} finally {
    Arc.container().requestContext().terminate();
}
Defensive patterns

Strategy: try-catch

Validate before calling

if (Arc.container().getActiveContext(handle.getBean().getScope()) == null) { Arc.container().requestContext().activate(); }

Try / catch

try { handle.destroy(); } catch (ContextNotActiveException e) { Arc.container().requestContext().activate(); handle.destroy(); Arc.container().requestContext().terminate(); }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of quarkusio/quarkus@e1c734241f (2026-09-05). Data as JSON: /api/errors/efcad0b0be370425. Report an issue: GitHub.