Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid corrupting $set-ed arrays when transaction error occurs #14346

Merged
merged 3 commits into from Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/plugins/trackTransaction.js
Expand Up @@ -85,7 +85,7 @@ function mergeAtomics(destination, source) {
destination.$addToSet = (destination.$addToSet || []).concat(source.$addToSet);
}
if (source.$set != null) {
destination.$set = Object.assign(destination.$set || {}, source.$set);
destination.$set = Array.isArray(source.$set) ? [...source.$set] : Object.assign({}, source.$set);
}

return destination;
Expand Down
56 changes: 56 additions & 0 deletions test/docs/transactions.test.js
Expand Up @@ -397,6 +397,62 @@ describe('transactions', function() {
assert.equal(docs[0].name, 'test');
});

it('handles resetting array state with $set atomic (gh-13698)', async function() {
db.deleteModel(/Test/);
const subItemSchema = new mongoose.Schema(
{
name: { type: String, required: true }
},
{ _id: false }
);

const itemSchema = new mongoose.Schema(
{
name: { type: String, required: true },
subItems: { type: [subItemSchema], required: true }
},
{ _id: false }
);

const schema = new mongoose.Schema({
items: { type: [itemSchema], required: true }
});

const Test = db.model('Test', schema);

const { _id } = await Test.create({
items: [
{ name: 'test1', subItems: [{ name: 'x1' }] },
{ name: 'test2', subItems: [{ name: 'x2' }] }
]
});

const doc = await Test.findById(_id).orFail();
let attempt = 0;

await db.transaction(async(session) => {
await doc.save({ session });

if (attempt === 0) {
attempt += 1;
throw new mongoose.mongo.MongoServerError({
message: 'Test transient transaction failures & retries',
errorLabels: [mongoose.mongo.MongoErrorLabel.TransientTransactionError]
});
}
});

const { items } = await Test.findById(_id).orFail();
assert.ok(Array.isArray(items));
assert.equal(items.length, 2);
assert.equal(items[0].name, 'test1');
assert.equal(items[0].subItems.length, 1);
assert.equal(items[0].subItems[0].name, 'x1');
assert.equal(items[1].name, 'test2');
assert.equal(items[1].subItems.length, 1);
assert.equal(items[1].subItems[0].name, 'x2');
});

it('transaction() retains modified status for documents created outside of the transaction then modified inside the transaction (gh-13973)', async function() {
db.deleteModel(/Test/);
const Test = db.model('Test', Schema({ status: String }));
Expand Down