[JAVA-1613] Race condition on DBPort usage during write commands Created: 07/Jan/15  Updated: 01/Apr/16  Resolved: 07/Jan/15

Status: Closed
Project: Java Driver
Component/s: Connection Management
Affects Version/s: 2.12.0
Fix Version/s: 2.12.5

Type: Bug Priority: Major - P3
Reporter: John Morales Assignee: Jeffrey Yemin
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
is duplicated by JAVA-1632 IllegalArgumentException: response to... Closed
Related

 Description   

A DBPort maintains a usageCount which is incremented upon each usage of a DBPort. In the case of an UNACKNOWLEDGED write, this usageCount is utilized by WriteResult to indicate when a subsequent call to getLastError() is unsafe because its DBPort has already been checked out and used by another thread. (To be fair, a deprecated pattern as of 2.13.0.)

The race is that while non-write command operations using DBPort.say() and DBPort.call() do ensure the usageCount is safely incremented, write commands go through DBTCPConnector and do not increment this counter.

Therefore it's possible that a write command on one thread may end up sharing a DBPort for a getLastError() call on another thread, resulting in undefined behavior.

Available Workaround
Use ACKNOWLEDGED writes whenever a getLastError() result is required, such as for a WriteResult.getUpsertedId().

Credit: jeff.yemin for analysis and discovery.



 Comments   
Comment by Githook User [ 07/Jan/15 ]

Author:

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

Message: Fixed race condition between threads attempting to get the result of an unacknowledged write after the fact and threads attempting to do write commands.
The race was cause by a lack of synchronization on DPPort in the write command path, and a failure to update DBPort.usageCount on that path.

JAVA-1613
Branch: 2.12.x
https://github.com/mongodb/mongo-java-driver/commit/d64f5c8575ee9ab725e0d96737fda3c1e6b37634

Comment by Githook User [ 07/Jan/15 ]

Author:

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

Message: Fixed race condition between threads attempting to get the result of an unacknowledged write after the fact and threads attempting to do write commands.
The race was cause by a lack of synchronization on DPPort in the write command path, and a failure to update DBPort.usageCount on that path.

JAVA-1613
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/befb3cf06aa2b3663acf13f84ac0d3a6d1cc4162

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