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

Model.bulkWrite function never returns on empty ops param and options.ordered = false #14117

Closed
2 tasks done
doomhz opened this issue Nov 23, 2023 · 2 comments
Closed
2 tasks done
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.

Comments

@doomhz
Copy link

doomhz commented Nov 23, 2023

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

Introduced in v6.11.3.
Can be reproduces in all other higher versions up to the latest version to date (v8.0.1) .

Node.js version

18.0

MongoDB server version

5.0

Typescript version (if applicable)

No response

Description

The Model.bulkWrite function broke after this fix was merged.

The bulkWrite function never returns when ops = [] and options = { ordered: false }, since the check for if (ops.length === 0) { return cb(null, getDefaultBulkwriteResult()); } was moved inside the new if (ordered) { block.

A potential solution would be to move the empty ops array check above the new if block and return early from the callback function.

Steps to Reproduce

Execute Model.bulkWrite([], { ordered: false }) for any model. The Node process would get stuck in an endless loop and eventually time out.

Expected Behavior

Early return cb(null, getDefaultBulkwriteResult()); from the Model.bulkWrite on empty ops array param.

@doomhz doomhz changed the title Model.bulkWrite function never returns on empty ops and options.ordered = false Model.bulkWrite function never returns on empty ops param and options.ordered = false Nov 23, 2023
@vkarpov15 vkarpov15 added this to the 6.12.4 milestone Nov 25, 2023
@vkarpov15 vkarpov15 added the has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue label Nov 25, 2023
@IslandRhythms IslandRhythms added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Nov 27, 2023
@IslandRhythms
Copy link
Collaborator

const mongoose = require('mongoose');


const testSchema = new mongoose.Schema({
  name: String
});

const Test = mongoose.model('Test', testSchema);

async function run() {
  await mongoose.connect('mongodb://localhost:27017');
  await mongoose.connection.dropDatabase();

  await Test.bulkWrite([], { ordered: false });
  console.log('done');
}

run();

@vkarpov15
Copy link
Collaborator

This was fixed in 6.12.0 with #13684 .

@vkarpov15 vkarpov15 removed this from the 6.12.4 milestone Dec 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

No branches or pull requests

3 participants