{"record":{"id":"d153d5d37e6a3cdb","repo":"quarkusio/quarkus","slug":"not-allowed-to-clear-the-context-data-map","errorCode":null,"errorMessage":"Not allowed to clear the context data map","messagePattern":"Not allowed to clear the context data map","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/ContextDataMap.java","lineNumber":61,"sourceCode":"        if (delegate == null) {\n            return Collections.emptyMap();\n        } else {\n            return delegate;\n        }\n    }\n\n    private Map<String, Object> getDelegateForWrite(final int sizeHint) {\n        if (delegate == null) {\n            this.delegate = new HashMap<>(sizeHint, 1.0f);\n        }\n        return delegate;\n    }\n\n    // ** Implement methods from the Map interface **\n\n    @Override\n    public void clear() {\n        throw new UnsupportedOperationException(\"Not allowed to clear the context data map\");\n    }\n\n    @Override\n    public boolean containsKey(Object key) {\n        if (ArcInvocationContext.KEY_INTERCEPTOR_BINDINGS.equals(key)) {\n            return true;\n        }\n        return getDelegateForRead().containsKey(key);\n    }\n\n    @Override\n    public boolean containsValue(Object value) {\n        if (interceptorBindings.equals(value)) {\n            return true;\n        }\n        return getDelegateForRead().containsValue(value);\n    }\n","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/quarkusio/quarkus/blob/e1c734241f34c7919086ceb4c9262b4a58f6de44/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/ContextDataMap.java#L43-L79","documentation":"ContextDataMap backs ArcInvocationContext, the map passed to interceptor methods (InvocationContext.getContextData()). Clearing it wholesale would destroy framework-managed data such as the interceptor bindings, so ArC deliberately makes clear() unsupported and throws UnsupportedOperationException.","triggerScenarios":"Calling contextData.clear() on the Map returned from InvocationContext.getContextData() inside an interceptor method (or via Map.clear() on any ContextDataMap reference).","commonSituations":"Generic utility code that 'resets' any map it receives (cleanup methods, generic cache eviction) being handed the interceptor context data; copying common map-manipulation patterns onto the CDI invocation context.","solutions":["Remove the clear() call; instead remove only the specific keys you own with contextData.remove(yourKey).","If you need a scratch area, create your own HashMap and use it instead of mutating the context data map.","Iterate and delete only keys you placed into the map (avoiding KEY_INTERCEPTOR_BINDINGS)."],"exampleFix":"// before\ncontextData.clear();\n\n// after\ncontextData.remove(\"my.custom.data.key\");","handlingStrategy":"type-guard","validationCode":"if (map instanceof ContextDataMap || ArcInvocationContext.class.isAssignableFrom(/* source */ Object.class)) {\n    // never call clear() on interceptor context data\n}","typeGuard":"static boolean isMutableRegularMap(Map<String, Object> m) {\n    return !(m instanceof ContextDataMap);\n}","tryCatchPattern":"try {\n    contextData.clear();\n} catch (UnsupportedOperationException e) {\n    // context data is framework-managed: remove only own keys instead\n}","preventionTips":["Treat InvocationContext.getContextData() as read-mostly framework state.","Remove only keys you put in; never clear the whole map.","Use a private HashMap for scratch data instead of the context map."],"tags":["cdi","interceptor","unsupported-operation","quarkus-arc"],"backgroundTag":"immutable-collection-modification","analyzedSha":"e1c734241f34c7919086ceb4c9262b4a58f6de44","analyzedAt":"2026-09-05T17:01:29.979Z","contentChangedAt":"2026-09-05T17:01:29.979Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}