[JAVA-2014] Async driver MongoClient.close() is not synchronous and has no callback Created: 13/Oct/15  Updated: 20/Oct/16  Resolved: 20/Oct/16

Status: Closed
Project: Java Driver
Component/s: Async, Connection Management
Affects Version/s: 3.1.0
Fix Version/s: None

Type: Bug Priority: Minor - P4
Reporter: Peter Hendriks Assignee: Unassigned
Resolution: Done Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: Java Source File ConcurrentRequestsDuringStartupExample.java    

 Description   

When a MongoClient is closed using the close() operation, it does not immediately close the connection pool. There is also no way to listen for a completed close of the connection pool.

During restarts, this potentially means the new connection pool has trouble connecting, leading to "hanging" requests or connection errors. In our use case, this is especially prevalent during integration testing where we do a lot of start/stops on the system and connection pool in quick succession.

Please see the attached example for a test case that eventually fails. Note that when a wait is introduced between close() and the next iteration, overlapping connection pool problems go away.

The close() API should either be synchronized, or should provide a callback option that will indicate when the close has completed. This will allow to wait for a full close before restarting other resources.



 Comments   
Comment by Jeffrey Yemin [ 20/Oct/16 ]

MongoClient.close() ) does synchronously close the connection pool. The issue is lower than the driver, which is demonstrated with the following simple program:

        for (int i = 0; i < Integer.MAX_VALUE; i++) {
            Socket socket = new Socket();
            try {
                socket.connect(new InetSocketAddress("localhost", 27017));
                System.out.println("Successfully connection socket " + i);
            } catch (IOException e) {
                System.out.println("Exception connecting socket" + i + ": " + e);
                e.printStackTrace();
            } finally {
                try {
                    socket.close();
                } catch (IOException e) {
                    System.out.println("Exception closing socket" + i + ": " + e);
                }
            }
        }

Eventually this program will start throwing similar exceptions to what you observed with MongoClient.

I don't think there's anything the driver can do to fix this.

Generated at Thu Feb 08 08:56:07 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.