Skip to content

ConcurrentModificationException when looping connections #921

@hovis

Description

@hovis

Describe the bug
ConcurrentModificationException when looping connections returned by getConnections() and a connection is created/removed elsewhere at the same time.

java.util.ConcurrentModificationException
	at java.util.HashMap$HashIterator.nextEntry(HashMap.java:810)
	at java.util.HashMap$KeyIterator.next(HashMap.java:845)
	at java.util.AbstractCollection.toArray(AbstractCollection.java:141)
	at java.util.ArrayList.<init>(ArrayList.java:151)
	at org.java_websocket.server.WebSocketServer.getConnections(WebSocketServer.java:289)
        ...

To Reproduce
Steps to reproduce the behavior:

  1. . Create a thread that requests connections via getConnections() and iterates over the connections (say gathering stats). Run all of that in a loop.
  2. .. Whilst that thread is running connect and disconnect from the WebSocket server using a WebSocket client.
  3. ... Should see an intermittent race condition leading to a ConcurrentModificationException

Expected behavior
No ConcurrentModificationException

Environment(please complete the following information):

  • Version used: 1.3.9 & 1.4.0
  • Java version: 1.8
  • Operating System and version: MacOS / Linux
  • Endpoint Name and version: Chrome/Safari/Firefox

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions