Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enabling a listener to remove itself results in ConcurrentModificationException #158

Closed
joeltoby opened this issue Jan 27, 2017 · 1 comment
Labels

Comments

@joeltoby
Copy link
Contributor

joeltoby commented Jan 27, 2017

Use-case: application needs to listen for one specific event to occur. Once the event has been handled, the listener should be removed so that the handler isn't triggered for any future events of that type. The easiest way to do this is for the event handler to remove itself.

The listeners are in a List and the events are dispatched by iterating the list as follows:


for (L listener : listeners) {
  try {
    listener.onEvent(event, SlackWebSocketSessionImpl.this);
  } catch (Throwable thr) {
    LOGGER.error("caught exception in dispatchImpl", thr);
  }
}

( SlackWebSocketSessionImpl.java#dispatchImpl )

This of course results in a ConcurrentModificationException. Should an iterator be used instead, or is there another preferred way to do this?

Example:

ReactionAddedListener reactionAddedListener = new ReactionAddedListener() {
        @Override
        public void onEvent(ReactionAdded event, SlackSession session) {
          // do something here
          session.removeReactionAddedListener(this);
}
joeltoby added a commit to joeltoby/simple-slack-api that referenced this issue Jan 28, 2017
@bcorne bcorne added the bug label Feb 1, 2017
bcorne added a commit that referenced this issue Oct 26, 2017
This change allows listeners to add and remove safely some new
listeners when they are reacting to events.

Fixing issue #158.
@bcorne
Copy link
Contributor

bcorne commented Oct 26, 2017

fixed with 08a55eb

@bcorne bcorne closed this as completed Oct 26, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants