Open wuxudong opened 10 years ago
Same problem.. Has anyone figured out how to fix this?
There is a simple brute solution. Look at code in io.socket.IOConnection.register(String origin, SocketIO socket)
static public IOConnection register(String origin, SocketIO socket) {
List<IOConnection> list = connections.get(origin);
if (list == null) {
list = new LinkedList<IOConnection>();
connections.put(origin, list);
} else {
synchronized (list) {
for (IOConnection connection : list) {
if (connection.register(socket))
return connection;
}
}
}
IOConnection connection = new IOConnection(origin, socket);
list.add(connection);
return connection;
}
one thread is iterating list, and another thread calls list.add(connection), ConcurrentModificationException will be thrown.
Simplest solution is
static public synchronized IOConnection register(String origin, SocketIO socket) {
List<IOConnection> list = connections.get(origin);
if (list == null) {
list = new LinkedList<IOConnection>();
connections.put(origin, list);
} else {
for (IOConnection connection : list) {
if (connection.register(socket))
return connection;
}
}
IOConnection connection = new IOConnection(origin, socket);
list.add(connection);
return connection;
}
After modifying the source code, the previous test passed. Enjoy it.
It works for me. Thanks @wuxudong !
Following code throws java.util.ConcurrentModificationException at java.util.LinkedList$ListItr.checkForComodification(LinkedList.java:953) at java.util.LinkedList$ListItr.next(LinkedList.java:886) at io.socket.IOConnection.register(IOConnection.java:239) at io.socket.SocketIO.setAndConnect(SocketIO.java:220) at io.socket.SocketIO.connect(SocketIO.java:183) at SocketIOTest$1.run(SocketIOTest.java:23)