Bug #97730 xdev api: ConcurrentModificationException at Session.close
Submitted: 21 Nov 2019 11:15 Modified: 28 May 2021 16:11
Reporter: Bernd Wahlen Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / J Severity:S3 (Non-critical)
Version:8.0.18 OS:Linux
Assigned to: Filipe Silva 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
[31 Jul 2020 22:15] Filipe Silva
Hi Bernd,

Thanks for this bug report and your interest in Connector/J.

You are right. It seems we missed a spot in the synchronization here.
[3 Aug 2020 21:25] Filipe Silva
Bug#99102 is a duplicate of this bug report.
[1 Mar 2021 16:45] Daniel So
Posted by developer:
 
Added the following entry to the C/J 8.0.24 changelog: 

" Concurrently getting and closing multiple sessions from the same X DevAPI Client object might result in a ConcurrentModificationException thrown by Connector/J at the closing of a session."