{"record":{"id":"6d3913b47c2f73d8","repo":"theonedev/onedev","slug":"an-error-occurred-while-detaching-component","errorCode":null,"errorMessage":"An error occurred while detaching component: ","messagePattern":"An error occurred while detaching component: ","errorType":"exception","errorClass":"WicketRuntimeException","httpStatus":null,"severity":"error","filePath":"server-core/src/main/java/org/apache/wicket/Component.java","lineNumber":1195,"sourceCode":"\t\t\tif (getFlag(FLAG_DETACHING))\n\t\t\t{\n\t\t\t\tthrow new IllegalStateException(Component.class.getName() +\n\t\t\t\t\t\t\" has not been properly detached. Something in the hierarchy of \" +\n\t\t\t\t\t\tgetClass().getName() +\n\t\t\t\t\t\t\" has not called super.onDetach() in the override of onDetach() method\");\n\t\t\t}\n\n\t\t\t// always detach models because they can be attached without the\n\t\t\t// component. eg component has a compoundpropertymodel and one of its\n\t\t\t// children component's getmodelobject is called\n\t\t\tdetachModels();\n\n\t\t\t// detach any behaviors\n\t\t\tnew Behaviors(this).detach();\n\t\t}\n\t\tcatch (Exception x)\n\t\t{\n\t\t\tthrow new WicketRuntimeException(\"An error occurred while detaching component: \" + toString(true), x);\n\t\t}\n\n\t\t// always detach children because components can be attached\n\t\t// independently of their parents\n\t\tdetachChildren();\n\n\t\t// reset the model to null when the current model is a IWrapModel and\n\t\t// the model that created it/wrapped in it is a IComponentInheritedModel\n\t\t// The model will be created next time.\n\t\tif (getFlag(FLAG_INHERITABLE_MODEL))\n\t\t{\n\t\t\tsetModelImpl(null);\n\t\t\tsetFlag(FLAG_INHERITABLE_MODEL, false);\n\t\t}\n\n\t\tclearEnabledInHierarchyCache();\n\t\tclearVisibleInHierarchyCache();\n","sourceCodeStart":1177,"sourceCodeEnd":1213,"githubUrl":"https://github.com/theonedev/onedev/blob/d44925c47c37992c828ea673a5f9620539bc3ff2/server-core/src/main/java/org/apache/wicket/Component.java#L1177-L1213","documentation":"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.","triggerScenarios":"Any Exception thrown during detach: e.g. a LazyLoadableDetachableModel's load() throwing, a custom onDetach()/onModelChanged throwing, or behavior detach failing.","commonSituations":"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.","solutions":["Read the 'Caused by' exception to find the real failure","Fix the underlying exception in the component's onDetach/load/model code","Null-check resources in detach logic; avoid touching closed sessions","Wrap risky detach work in try/finally with super.onDetach() still called"],"exampleFix":"// before\n@Override\nprotected void onDetach() {\n    getSession().bind(); // may fail after session invalidation\n    super.onDetach();\n}\n// after\n@Override\nprotected void onDetach() {\n    try {\n        if (getSession() != null) {\n            getSession().bind();\n        }\n    } finally {\n        super.onDetach();\n    }\n}","handlingStrategy":"try-catch","validationCode":"// nothing to validate up front; ensure detach logic is null-safe:\nif (model != null && model.getObject() != null) { /* safe to touch */ }","typeGuard":null,"tryCatchPattern":"try {\n    component.detach();\n} catch (WicketRuntimeException e) {\n    Throwable cause = e.getCause(); // inspect the real failure\n    log.error(\"detach failed for component\", cause);\n}","preventionTips":["Always read the 'Caused by' chain — this error is only a wrapper","Make onDetach and lazy model load() null-safe and session-independent","Avoid database/network access in detach paths"],"tags":["wicket","lifecycle","detach","wrapped-exception"],"backgroundTag":"internal-invariant-violation","analyzedSha":"d44925c47c37992c828ea673a5f9620539bc3ff2","analyzedAt":"2026-09-06T07:18:27.995Z","contentChangedAt":"2026-09-06T07:18:27.995Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}