theonedev/onedev · error · WicketRuntimeException

An error occurred while detaching component:

Error message

An error occurred while detaching component: 

What it means

Component.detach() wraps the detach phase (onDetach, model detach, behavior detach) in a try/catch and rethrows any Exception as a WicketRuntimeException with the message 'An error occurred while detaching component: ' plus the component's toString. The original exception is attached as the cause, so this is a wrapper around any failure in user detach code.

Source

Thrown at server-core/src/main/java/org/apache/wicket/Component.java:1195

			if (getFlag(FLAG_DETACHING))
			{
				throw new IllegalStateException(Component.class.getName() +
						" has not been properly detached. Something in the hierarchy of " +
						getClass().getName() +
						" has not called super.onDetach() in the override of onDetach() method");
			}

			// always detach models because they can be attached without the
			// component. eg component has a compoundpropertymodel and one of its
			// children component's getmodelobject is called
			detachModels();

			// detach any behaviors
			new Behaviors(this).detach();
		}
		catch (Exception x)
		{
			throw new WicketRuntimeException("An error occurred while detaching component: " + toString(true), x);
		}

		// always detach children because components can be attached
		// independently of their parents
		detachChildren();

		// reset the model to null when the current model is a IWrapModel and
		// the model that created it/wrapped in it is a IComponentInheritedModel
		// The model will be created next time.
		if (getFlag(FLAG_INHERITABLE_MODEL))
		{
			setModelImpl(null);
			setFlag(FLAG_INHERITABLE_MODEL, false);
		}

		clearEnabledInHierarchyCache();
		clearVisibleInHierarchyCache();

View on GitHub (pinned to d44925c47c)

Solutions

  1. Read the 'Caused by' exception to find the real failure
  2. Fix the underlying exception in the component's onDetach/load/model code
  3. Null-check resources in detach logic; avoid touching closed sessions
  4. Wrap risky detach work in try/finally with super.onDetach() still called

Example fix

// before
@Override
protected void onDetach() {
    getSession().bind(); // may fail after session invalidation
    super.onDetach();
}
// after
@Override
protected void onDetach() {
    try {
        if (getSession() != null) {
            getSession().bind();
        }
    } finally {
        super.onDetach();
    }
}
Defensive patterns

Strategy: try-catch

Validate before calling

// nothing to validate up front; ensure detach logic is null-safe:
if (model != null && model.getObject() != null) { /* safe to touch */ }

Try / catch

try {
    component.detach();
} catch (WicketRuntimeException e) {
    Throwable cause = e.getCause(); // inspect the real failure
    log.error("detach failed for component", cause);
}

Prevention

When it happens

Trigger: Any Exception thrown during detach: e.g. a LazyLoadableDetachableModel's load() throwing, a custom onDetach()/onModelChanged throwing, or behavior detach failing.

Common situations: Database/session resources accessed during detach after the underlying session closed; NPEs in custom detach logic; load failures in lazy models at end of request.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


AI-assisted analysis of theonedev/onedev@d44925c47c (2026-09-06). Data as JSON: /api/errors/6d3913b47c2f73d8. Report an issue: GitHub.