[CSHARP-2974] Task.WhenAll fails with transaction error when awaiting multiple BulkWrite operations Created: 20/Feb/20 Updated: 27/Oct/23 Resolved: 04/Mar/20 |
|
| Status: | Closed |
| Project: | C# Driver |
| Component/s: | None |
| Affects Version/s: | 2.10.2 |
| Fix Version/s: | None |
| Type: | Bug | Priority: | Major - P3 |
| Reporter: | Alex Bevilacqua | Assignee: | Robert Stam |
| Resolution: | Works as Designed | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Case: | (copied to CRM) |
| Description |
|
When awaiting multiple BulkWriteAsync tasks using Task.WhenAll wrapped in a transaction, if no other operations have been performed on the MongoClient the transaction will fail with: Only servers in a sharded cluster can start a new transaction at the active transaction number For example, the following would fail:
but un-commenting the db operation would cause the code to run successfully. -------------------- Reproduction:
|
| Comments |
| Comment by Sujesh Arukil [ 04/Mar/20 ] |
|
I don't think it is a race condition. Any operation to the db to trigger an activity is all is needed. like this _db.RunCommand((Command<BsonDocument>)"{ping:1}"); and then this succeeds openSession openTransaction await Task.WhenAll(....)
This is not needed when not using a transaction.
|
| Comment by Robert Stam [ 04/Mar/20 ] |
|
I wondered about that too. It's probably a race condition. If one call to `BulkWriteAsync` happens to complete before the other happens to start the code could succeed. |
| Comment by Sujesh Arukil [ 04/Mar/20 ] |
|
While this may be, it does not explain that performing any operation on the Db first and then calling a Task.WhenAll works. Any explanation on that? |
| Comment by Robert Stam [ 04/Mar/20 ] |
|
Sessions are not multi-thread (or multi-Task) safe. Only one operation at a time can be performed on each session. |