Skip to content

Commit

Permalink
Merge pull request #13684 from Automattic/vkarpov15/gh-13664
Browse files Browse the repository at this point in the history
fix(model): avoid hanging on empty `bulkWrite()` with ordered: false
  • Loading branch information
vkarpov15 committed Aug 3, 2023
2 parents 899bf5c + 221ca3d commit 9cb950d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
28 changes: 16 additions & 12 deletions lib/model.js
Expand Up @@ -3472,18 +3472,22 @@ Model.bulkWrite = async function bulkWrite(ops, options) {
let validOps = [];
let validationErrors = [];
const results = [];
for (let i = 0; i < validations.length; ++i) {
validations[i]((err) => {
if (err == null) {
validOps.push(i);
} else {
validationErrors.push({ index: i, error: err });
results[i] = err;
}
if (--remaining <= 0) {
completeUnorderedValidation.call(this);
}
});
if (remaining === 0) {
completeUnorderedValidation.call(this);
} else {
for (let i = 0; i < validations.length; ++i) {
validations[i]((err) => {
if (err == null) {
validOps.push(i);
} else {
validationErrors.push({ index: i, error: err });
results[i] = err;
}
if (--remaining <= 0) {
completeUnorderedValidation.call(this);
}
});
}
}

validationErrors = validationErrors.
Expand Down
9 changes: 9 additions & 0 deletions test/model.test.js
Expand Up @@ -5923,6 +5923,15 @@ describe('Model', function() {

});

it('Model.bulkWrite(...) does not hang with empty array and ordered: false (gh-13664)', async function() {
const userSchema = new Schema({ name: String });
const User = db.model('User', userSchema);

const err = await User.bulkWrite([], { ordered: false }).then(() => null, err => err);
assert.ok(err);
assert.equal(err.name, 'MongoInvalidArgumentError');
});

it('allows calling `create()` after `bulkWrite()` (gh-9350)', async function() {
const schema = Schema({ foo: Boolean });
const Model = db.model('Test', schema);
Expand Down

0 comments on commit 9cb950d

Please sign in to comment.