[JAVA-5107] Fix race condition in ClientSession#close Created: 15/Aug/23  Updated: 28/Oct/23  Resolved: 16/Aug/23

Status: Closed
Project: Java Driver
Component/s: Sessions
Affects Version/s: None
Fix Version/s: 4.11.0

Type: Bug Priority: Minor - P4
Reporter: Jeffrey Yemin Assignee: Jeffrey Yemin
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Problem/Incident
Related
Backwards Compatibility: Fully Compatible
Documentation Changes: Not Needed
Documentation Changes Summary:

1. What would you like to communicate to the user about this feature?
2. Would you like the user to see examples of the syntax and/or executable code and its output?
3. Which versions of the driver/connector does this apply to?


 Description   

The sessions specification says that ClientSession should not attempt to implement any thread safety guarantees, and the Java driver does not do so. But the interpretation of that with respect to ClientSession#close leads to a race condition if multiple threads call the method concurrently on the same instance. This race can lead to duplicate server sessions added to the server session pool, which is undesirable.

The driver should protect against this in ClientSession#close by using proper thread synchronization (e.g. AtomicBoolean#compareAndSet.



 Comments   
Comment by Githook User [ 16/Aug/23 ]

Author:

{'name': 'Jeff Yemin', 'email': 'jeff.yemin@mongodb.com', 'username': 'jyemin'}

Message: Make the ClientSession#close method thread-safe (#1179)

Replace the volatile boolean with an AtomicBoolean
to ensure that the side effects of the close method
only execute once.

JAVA-5107
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/80a9cb4f961f727f4a5ac85882e658723e7f04d9

Generated at Thu Feb 08 09:03:46 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.