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

fix(model): avoid hanging on empty bulkWrite() with ordered: false #13684

Merged
merged 1 commit into from Aug 3, 2023
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
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