From 5879ea4868e7e7939d7079fed1f71044821024f1 Mon Sep 17 00:00:00 2001 From: Boubaker Khanfir Date: Fri, 19 Sep 2025 11:14:40 +0100 Subject: [PATCH] fix: Allow to remove a ThreadLocalAccessor by Key of Type Object The `ThreadLocalAccessor.key()` is of Type Object. Thus, when it comes to delete a predefined `ThreadLocalAccessor` which have a non-String key, it's becomes impossible to delete. This behavior was observed while attempting to remove `org.springframework.security.core.context.ReactiveSecurityContextHolderThreadLocalAccessor` from the `ContextRegistry`, which have a key of type `java.lang.Class`. This change will add a dedicated method to allow deleting a `ThreadLocalAccessor` identified by its key of type `java.lang.Object`. Signed-off-by: Boubaker Khanfir --- .../java/io/micrometer/context/ContextRegistry.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/context-propagation/src/main/java/io/micrometer/context/ContextRegistry.java b/context-propagation/src/main/java/io/micrometer/context/ContextRegistry.java index f5f5239..122370c 100644 --- a/context-propagation/src/main/java/io/micrometer/context/ContextRegistry.java +++ b/context-propagation/src/main/java/io/micrometer/context/ContextRegistry.java @@ -149,8 +149,18 @@ public ContextRegistry registerThreadLocalAccessor(ThreadLocalAccessor access * Removes a {@link ThreadLocalAccessor}. * @param key under which the accessor got registered * @return {@code true} when accessor got successfully removed + * @deprecated use {@link ContextRegistry#removeThreadLocalAccessorForKey(Object)} instead */ public boolean removeThreadLocalAccessor(String key) { + return removeThreadLocalAccessorForKey(key); + } + + /** + * Removes a {@link ThreadLocalAccessor}. + * @param key under which the accessor got registered + * @return {@code true} when accessor got successfully removed + */ + public boolean removeThreadLocalAccessorForKey(Object key) { for (ThreadLocalAccessor existing : this.threadLocalAccessors) { if (existing.key().equals(key)) { return this.threadLocalAccessors.remove(existing);