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

Post Save Hook Throw ParallelSaveError: Can't save() the same doc multiple times in parallel #6611

Closed
lykmapipo opened this issue Jun 22, 2018 · 1 comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@lykmapipo
Copy link

Do you want to request a feature or report a bug?
bug

What is the current behavior?
I get this error:

{ ParallelSaveError: Can't save() the same doc multiple times in parallel. Document: 5b2c9a3276254b3c471396c2
    at ParallelSaveError.MongooseError [as constructor] (/home/lally/workspace/mongoose/playground/node_modules/mongoose/lib/error/mongooseError.js:11:11)
    at new ParallelSaveError (/home/lally/workspace/mongoose/playground/node_modules/mongoose/lib/error/parallelSave.js:18:17)
    at model.Model.save (/home/lally/workspace/mongoose/playground/node_modules/mongoose/lib/model.js:342:23)
    at toExecute.push.callback (/home/lally/workspace/mongoose/playground/node_modules/mongoose/lib/model.js:2244:16)
    at /home/lally/workspace/mongoose/playground/node_modules/async/internal/parallel.js:31:39
    at eachOfArrayLike (/home/lally/workspace/mongoose/playground/node_modules/async/eachOf.js:65:9)
    at exports.default (/home/lally/workspace/mongoose/playground/node_modules/async/eachOf.js:9:5)
    at _parallel (/home/lally/workspace/mongoose/playground/node_modules/async/internal/parallel.js:30:5)
    at parallelLimit (/home/lally/workspace/mongoose/playground/node_modules/async/parallel.js:88:26)
    at utils.promiseOrCallback.cb (/home/lally/workspace/mongoose/playground/node_modules/mongoose/lib/model.js:2248:5)
    at Object.promiseOrCallback (/home/lally/workspace/mongoose/playground/node_modules/mongoose/lib/utils.js:222:14)
    at Function.create (/home/lally/workspace/mongoose/playground/node_modules/mongoose/lib/model.js:2209:16)
    at Object.<anonymous> (/home/lally/workspace/mongoose/playground/parallel.save.js:31:6)
    at Module._compile (internal/modules/cjs/loader.js:678:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:689:10)
    at Module.load (internal/modules/cjs/loader.js:589:32)
  message: 'Can\'t save() the same doc multiple times in parallel. Document: 5b2c9a3276254b3c471396c2',
  name: 'ParallelSaveError' }

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

#!/usr/bin/env node

'use strict';

process.env.MONGODB_URI =
    (process.env.MONGODB_URI || 'mongodb://localhost/mongoose-playground');

/* dependencies */
const mongoose = require('mongoose');
const Schema = mongoose.Schema;


mongoose.connect(process.env.MONGODB_URI);

const NoteSchema = new Schema({
    body: String
});

NoteSchema.pre('save', function(next) {
    next();
});

NoteSchema.post('save', function(note, next) {
    note.save(function(error, saved) {
        next(error, saved);
    });
});

const Note = mongoose.model('Note', NoteSchema);

Note.create({ body: 'Quick note' }, function(error, note) {
    console.log(error);
    console.log(note);
});

What is the expected behavior?
Post save hook have to work as expected and not restricted with parallel saving checkings

Please mention your node.js, mongoose and MongoDB version.
node.js : v10.1.0
mongoose: v5.1.6
MongoDB: v3.4.10

@lineus
Copy link
Collaborator

lineus commented Jun 23, 2018

@lykmapipo thanks for reporting this, and especially thanks for the concise repro script! I'll sort out a PR for this ASAP.

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