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.findOneAndUpdate() has infinite loop when there's Map in the dbref's schema #6750

Closed
andyzhau opened this issue Jul 20, 2018 · 3 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@andyzhau
Copy link

Do you want to request a feature or report a bug?
It's a bug.

What is the current behavior?
Model.findOneAndUpdate() hangs when update has a reference model which has a Map as a field.

If the current behavior is a bug, please provide the steps to reproduce.

const User = mongoose.model('User', {
      maps: { type: Map, of: String },
});

const Post = mongoose.model('Post', {
      user: { type: Schema.Types.ObjectId, ref: 'User' },
});

const user = await User.create({});
await Post.findOneAndUpdate({user}, {user}, {upsert: true, new: true}); /// hangs here.

I took a quick investigation. In lib/helpers/common.js function modifiedPaths will recursive call itself when there's map, because there is a key $__parent generated when model has a Map.

function modifiedPaths(update, path, result) {
  var keys = Object.keys(update || {});
  var numKeys = keys.length;
  result = result || {};
  path = path ? path + '.' : '';

  for (var i = 0; i < numKeys; ++i) {
    var key = keys[i];
    var val = update[key];

    if (key.startsWith('$')) continue;   // << Works if I added this line

    result[path + key] = true;
    if (utils.isMongooseObject(val) && !Buffer.isBuffer(val)) {
      val = val.toObject({ transform: false, virtuals: false });
    }
    if (shouldFlatten(val)) {
      modifiedPaths(val, path + key, result);
    }
  }

  return result;
}

What is the expected behavior?
No infinite loop.

Please mention your node.js, mongoose and MongoDB version.
node v10.5.0
mongoose 5.2.4
mongodb v3.6.4

@vkarpov15
Copy link
Collaborator

Below script executes without error on mongoose 5.2.4, can you modify the below script to repro your issue?

const assert = require('assert');
const mongoose = require('mongoose');
mongoose.set('debug', true);

const GITHUB_ISSUE = `gh6750`;
const connectionString = `mongodb://localhost:27017/${ GITHUB_ISSUE }`;
const { Schema } = mongoose;

run().then(() => console.log('done')).catch(error => console.error(error.stack));

async function run() {
  await mongoose.connect(connectionString);
  await mongoose.connection.dropDatabase();

  const User = mongoose.model('User', {
      maps: { type: Map, of: String },
});

  const Post = mongoose.model('Post', {
    user: { type: Schema.Types.ObjectId, ref: 'User' },
  });

  const user = await User.create({});
  const doc = await Post.findOneAndUpdate({user}, {user}, {upsert: true, new: true});
  console.log('Doc', doc);
}

@vkarpov15 vkarpov15 added the can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. label Jul 22, 2018
@andyzhau
Copy link
Author

Construct the map with a default '{}', then it can trigger error like

RangeError: Maximum call stack size exceeded
const assert = require('assert');
const mongoose = require('mongoose');
mongoose.set('debug', true);

const GITHUB_ISSUE = `gh6750`;
const connectionString = `mongodb://localhost:27017/${ GITHUB_ISSUE }`;
const { Schema } = mongoose;

run().then(() => console.log('done')).catch(error => console.error(error.stack));

async function run() {
  await mongoose.connect(connectionString);
  await mongoose.connection.dropDatabase();

  const User = mongoose.model('User', {
      maps: { type: Map, of: String, default: {} },
});

  const Post = mongoose.model('Post', {
    user: { type: Schema.Types.ObjectId, ref: 'User' },
  });

  const user = await User.create({});
  const doc = await Post.findOneAndUpdate({user}, {user}, {upsert: true, new: true});
  console.log('Doc', doc);
}

vkarpov15 added a commit that referenced this issue Jul 25, 2018
@vkarpov15 vkarpov15 added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. labels Jul 25, 2018
@vkarpov15 vkarpov15 added this to the 5.2.6 milestone Jul 25, 2018
@vkarpov15
Copy link
Collaborator

Thanks for reporting, will fix asap

vkarpov15 added a commit that referenced this issue Jul 26, 2018
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

2 participants