Bug #97730 xdev api: ConcurrentModificationException at Session.close
Submitted: 21 Nov 2019 11:15
Reporter: Bernd Wahlen Email Updates:
Status: Open Impact on me:
None 
Category:Connector / J Severity:S3 (Non-critical)
Version:8.0.18 OS:Linux
Assigned to: CPU Architecture:x86

[21 Nov 2019 11:15] Bernd Wahlen
Description:
It looks like session map is not Thread safe:

java.util.ConcurrentModificationException (pool-13-thread-4)
java.util.ConcurrentModificationException: null
        at java.util.HashMap$HashIterator.nextNode(HashMap.java:1495) ~[?:?]
        at java.util.HashMap$KeyIterator.next(HashMap.java:1518) ~[?:?]
        at com.mysql.cj.xdevapi.ClientImpl.idleProtocol(ClientImpl.java:345) ~[mysql-connector-java-8.0.18.jar:8.0.18]
        at com.mysql.cj.xdevapi.ClientImpl$PooledXProtocol.close(ClientImpl.java:376) ~[mysql-connector-java-8.0.18.jar:8.0.18]
        at com.mysql.cj.MysqlxSession.quit(MysqlxSession.java:90) ~[mysql-connector-java-8.0.18.jar:?]
        at com.mysql.cj.xdevapi.SessionImpl.close(SessionImpl.java:217) ~[mysql-connector-java-8.0.18.jar:8.0.18]

I have to syncronize it myself outside like this:
    private final Object sessionMapLock = new Object();

    public Session getSession() {
        synchronized (sessionMapLock) {
            return client.getSession();
        }
    }

    public void closeSession(Session s) {
        synchronized (sessionMapLock) {
            s.close();
        }
    }

How to repeat:
use ThreadPoolExecutor and run multiple small Operations which open&closes many sessions.

Suggested fix:
use ConcurrentHashMap intead of HashMap