[JAVA-4490] Reference count check is not atomic with release Created: 10/Feb/22  Updated: 28/Oct/23  Resolved: 10/Feb/22

Status: Closed
Project: Java Driver
Component/s: Reactive Streams
Affects Version/s: None
Fix Version/s: 4.5.0

Type: Bug Priority: Major - P3
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
is caused by JAVA-3926 Consider server load during server se... Closed
Backwards Compatibility: Fully Compatible
Documentation Changes: Not Needed

 Description   

This race condition showed up in a unit test but it seems possible, though rare, that it could affect an application using the driver.

The issue is that DefaultServer.AsyncOperationCountTrackingConnection#release is not synchronized, and it some circumstances be called concurrently. So if the timing is just wrong, multiple concurrent calls can observe a 0 reference count, resulting in the operation count decrementing below 0, which in turn causes an assertion to fire.

There are several approaches to fixing this, but the cleanest is to change the RefererenceCounted#release method to atomically return the decremented count and compare the returned value to 0.



 Comments   
Comment by Githook User [ 10/Feb/22 ]

Author:

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

Message: Make reference count check atomic with release (#876)

JAVA-4490

Co-authored-by: Valentin Kovalenko <valentin.kovalenko@mongodb.com>
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/058d9f4af7c519f34612bd4e4a23c52af08fbf7f

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