Gottox / socket.io-java-client

Socket.IO Client Implementation in Java
MIT License
950 stars 387 forks source link

Possible concurrent problem in multi-thread #42

Open Mavlarn opened 11 years ago

Mavlarn commented 11 years ago

I want to use this lib in a multiple thread environment. I will create multiple SocketIO to connect the server, and send and get message. I checked the code to create connection, but I found it is possible to have a concurrent problem if I have many thread trying to create SocketIO object at the same time.

The related code is as below: static public IOConnection register(String origin, SocketIO socket) {

             //it is not synchronized, so there will be problem.
    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;
}
emilssolmanis commented 11 years ago

Confirmed, this is not thread-safe, far from it actually. Just crashed on this myself. Will see if I can patch it up.

mbabuskov commented 10 years ago

It seems that there are problems even when running stuff on a single thread, when messages are passed very quickly. I'm not sure if it is related, but I'll try to create a test case.