Skip to content

Commit

Permalink
Properly remove security index recovered state consumer (elastic#112927)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
n1v0lg committed Sep 17, 2024
1 parent 6a25aff commit f97b8d2
Showing 1 changed file with 10 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -344,13 +344,16 @@ public static int getMigrationVersionFromIndexMetadata(IndexMetadata indexMetada
}

public void onStateRecovered(Consumer<State> recoveredStateConsumer) {
BiConsumer<State, State> 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<State, State> 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);
Expand Down

0 comments on commit f97b8d2

Please sign in to comment.