[SERVER-68104] Can't send queries using the same transaction concurrently, NoSuchTransaction Created: 16/Jul/22  Updated: 09/Aug/22  Resolved: 09/Aug/22

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: 5.0.6
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: yaman lakis Assignee: Chris Kelly
Resolution: Done Votes: 0
Labels: Transactions, concurrency
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: File 2nd-log.json     Text File error.txt     File index.js     File log.json    
Operating System: ALL
Steps To Reproduce:

I have attached a JS file to reproduce the issue, any MongoDB instance will work there is no special configuration required or anything, just a MongoDB replica set running.

Participants:

 Description   

Running this operation will sometimes cause the following error NoSuchTransaction.

 

...
await Promise.all([
  collectionA.insertOne({ _id: new ObjectId() }, { session }),
  collectionB.insertOne({ _id: new ObjectId() }, { session })
])
...

 

Now as I noticed mongodb client sends a `startTransaction` set to true on the first command executed, insert to collection A in our example, alongside a `txnNumber` and only send the the `txnNumber` value with the following commands.
I inspected the logs on mongodb and found that whenever the error happens the command with `startTransaction` set to true always comes after some other command using the same `txnNumber`, and since the transaction was not started yet the first command in the logs, insert to collection B in our example will throw an error indicating that the transaction does not exist.

 

I included a part of the log as a json file in the attachment that shows how first the collectionB.insert command gets executed then collectionA.insert.



 Comments   
Comment by Chris Kelly [ 09/Aug/22 ]

Yaman,

While it's correct that the first step should be startTransaction, you are trying to send requests in parallel and there is no guarantee these arrive to the server in order. MongoDB processes requests to the same session serially, as only one request runs per session, per server, at a time. This also means that parallelizing requests won't yield any benefit in a transaction.

You can check out the NodeJS example in the documentation here for some more context. If you don't parallelize your inserts, you shouldn't run into this error.

I'll close this issue for now, but if you encounter issues with the general process shown in the documentation, we can continue discussion here. Thanks for reaching out!

Regards,
Christopher

 

Comment by yaman lakis [ 23/Jul/22 ]

I tried running it again on the versions you have mentioned, MongoDB 5.0.6 and MongoDB NodeJS driver 4.8.0, and still encountered the same issue.
Here is exactly what I've done
I'm using Docker Desktop version 4.8.2 (79419), Linux containers

Ran a MongoDB container using the following command:

docker run --name mongo-promise-all -p 27017:27017 mongo:5.0.6 --replSet rs0 

In the container I ran mongo shell and initiated the replica set using the following configuration:

rs.initiate({ _id: "rs0", members: [{ _id:0, host:"127.0.0.1:27017" }] }) 

then i ran the following:

use db
db.setLogLevel(5, "query")
db.setLogLevel(5, "control")
db.setLogLevel(5, "transaction")

installed MongoDB NodeJS driver version 4.8.0 and ran the code in the file I attached in my issue.

I got the follwoing logs:

2nd-log.json

I added comments at the lines where I think they are related to the problem in the log, hope it makes things clearer.

and here is the error logged by the NodeJS process

error.txt

Thank you for your time Chris

Comment by Chris Kelly [ 22/Jul/22 ]

Hi Yaman,

Thanks for your report - I ran your test driver on MongoDB 5.0.6 and did not observe the NoSuchTransaction message in the logs, and the inserts appeared to work. Can you explain how you are running this exactly? I used the version 4.8.0 of the mongodb node driver to run your code.

Regards,
Christopher

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