-
Type:
Bug
-
Resolution: Gone away
-
Priority:
Unknown
-
None
-
Affects Version/s: None
-
Component/s: None
-
None
-
None
-
Java Drivers
-
None
-
None
-
None
-
None
-
None
-
None
Summary
When performing an update with updateOne and using UpdateOptions().upsert(true) , a race condition can occur, causing a DuplicateKeyException to be thrown (which should be impossible for an upsert).
This likely occurs because there is a race happening here:
- Upsert-logic checks if the document already exists, finds that it doesn't and chooses to go with insert instead of update
- Another process inserts that document
- Now the insert fails with duplicate key
__ This could be fixed in several ways, but the easiest is probably to just catch the DuplicateKeyException when the race occurs and then to simply retry with an update instead of an insert, which is then guaranteed to work (unless a second race happens with a deletion)
Please provide the version of the driver. If applicable, please provide the MongoDB server version and topology (standalone, replica set, or sharded cluster).
MongoDB Driver 5.6.1, in Kotlin
How to Reproduce
Race conditions are hard to reproduce, but essentially any setup with multiple tasks trying to upsert elements with shared ids will produce this error
Additional Background
-