From cfe8dfa32252b280acf551d5343b95f88ca3806c Mon Sep 17 00:00:00 2001 From: Nikolaj Volgushev Date: Tue, 17 Sep 2024 12:24:20 +0200 Subject: [PATCH] Properly remove security index recovered state consumer (#112927) One of the security index state change listeners attempts to remove itself once it has received the expected event. However, since the consumer is a lambda, `this` in `stateChangeListeners.remove(this);` actually refers to the enclosing class instance (`SecurityIndexManager.this`) which results in a noop and not an actual removal of the relevant consumer. This PR fixes this by converting the lambda to an anonymous class. It's technically a bug, but so minor that it doesn't warrant a bug changelog IMO; so I'm labelling it a non-issue instead. --- .../security/support/SecurityIndexManager.java | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/support/SecurityIndexManager.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/support/SecurityIndexManager.java index a6377c3ea789..a6c8de003c15 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/support/SecurityIndexManager.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/support/SecurityIndexManager.java @@ -344,13 +344,16 @@ public class SecurityIndexManager implements ClusterStateListener { } public void onStateRecovered(Consumer recoveredStateConsumer) { - BiConsumer stateChangeListener = (previousState, nextState) -> { - boolean stateJustRecovered = previousState == UNRECOVERED_STATE && nextState != UNRECOVERED_STATE; - boolean stateAlreadyRecovered = previousState != UNRECOVERED_STATE; - if (stateJustRecovered) { - recoveredStateConsumer.accept(nextState); - } else if (stateAlreadyRecovered) { - stateChangeListeners.remove(this); + BiConsumer stateChangeListener = new BiConsumer<>() { + @Override + public void accept(State previousState, State nextState) { + boolean stateJustRecovered = previousState == UNRECOVERED_STATE && nextState != UNRECOVERED_STATE; + boolean stateAlreadyRecovered = previousState != UNRECOVERED_STATE; + if (stateJustRecovered) { + recoveredStateConsumer.accept(nextState); + } else if (stateAlreadyRecovered) { + stateChangeListeners.remove(this); + } } }; stateChangeListeners.add(stateChangeListener);