Automattic / Automattic/mongoose
Mongoose does not reset object versioning during transaction abort?
- Dominant language
- JavaScript
- Stars
- 27.5k
- Forks
- 4k
- Avg merge
- 2d 7h
- Merged PRs (30d)
- 35
Description
**Do you want to request a *feature* or report a *bug*?**
bug
**What is the current behavior?**
It does not seem like Mongoose reset object versioning during transaction abort
**If the current behavior is a bug, please provide the steps to reproduce.**
it appears the object version is not reverted after transaction abort in the below snippet. relevant code: https://github.com/Automattic/mongoose/blob/master/lib/connection.js#L483
```
const newItem = createAndSaveTestItem(); // creates a version 0
let secondVersion: ITestCollection;
try {
await session.withTransaction(async () => {
newItem.someNumber = 432;
secondVersion = await newItem.save({ session });
// secondVersion is now storing the state of a successful v1
attempt++;
console.log(`Version after update and before transaction fail: ${newItem!.__v}`);
throw new Error('Testing a transaction rollback');
});
} catch (err) {
//
}
console.log(`Version is now ${secondVersion!.__v}`);
```
this causes problem for us when mongodb driver automatically retries (https://mongodb.github.io/node-mongodb-native/3.2/api/node_modules_mongodb-core_lib_sessions.js.html#line348) the transaction and resulted in version mismatches error like `VersionError: No matching document found for id "..." version 1 modifiedPaths ...`
we were able to fix this by fetching the object again at the start of the transaction so we're more confident on the above hypothesis.
**What is the expected behavior?**
object version is reverted back, either in default versioning or optimistic locking
**What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.**
forked version of 5.12.7, don't think we changed the transaction part of code
Contributor guide
Research direction
Start by reproducing the supplied session.withTransaction example against the transaction handling in lib/connection.js around line 483. Trace how the document and its version are restored after an abort, including the retry scenario described in the issue. Done means the aborted transaction leaves object versioning consistent and avoids the reported VersionError.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, mongodb, node.js
- Domain
- backend, databases
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100