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

Too large queries cause process to crash instead of throwing an error #5812

Closed
odedsh opened this issue Nov 13, 2017 · 1 comment
Closed
Labels
underlying library issue This issue is a bug with an underlying library, like the MongoDB driver or mongodb-core
Milestone

Comments

@odedsh
Copy link

odedsh commented Nov 13, 2017

When sending a malformed query I expect an exception to be thrown. Instead node crashes with a stack trace that does not allow to debug the issue

This example code reproduces the problem

var mongoose = require("mongoose");
var uuid = require('uuid');

mongoose.set('debug', false);
mongoose.connect("mongodb://localhost:27017/bug_reproduce", {useMongoClient:true, keepAlive:true});

var schema = new mongoose.Schema({
  firstName: String,
  middleName: String,
  lastName: String,
  fullName: String,
  email: String
}, {collection: 'TestCollection'});

var model = mongoose.model("TestCollection", schema);
var ridicolousAmountOfData = new Array(400000)

for (var i=0; i<ridicolousAmountOfData.length; ++i) {
  ridicolousAmountOfData[i] = uuid.v1();
}

console.log("Starting");
model.find({email: {$in: ridicolousAmountOfData}}).lean().exec()
  .then( function() {
    console.log("All is Well");
    process.exit(0);
  })
  .catch(function(err) {console.log("Error Caught")})


The output:

Starting
(node:73146) DeprecationWarning: Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead: http://mongoosejs.com/docs/promises.html
/Users/oded/wisdo/bug-reproduce/node_modules/mongodb/lib/utils.js:123
    process.nextTick(function() { throw err; });
                                  ^

RangeError: Attempt to write outside buffer bounds
    at Buffer.write (buffer.js:876:13)
    at serializeString (/Users/oded/wisdo/bug-reproduce/node_modules/bson/lib/bson/parser/serializer.js:41:83)
    at serializeInto (/Users/oded/wisdo/bug-reproduce/node_modules/bson/lib/bson/parser/serializer.js:600:17)
    at serializeObject (/Users/oded/wisdo/bug-reproduce/node_modules/bson/lib/bson/parser/serializer.js:308:18)
    at serializeInto (/Users/oded/wisdo/bug-reproduce/node_modules/bson/lib/bson/parser/serializer.js:776:17)
    at serializeObject (/Users/oded/wisdo/bug-reproduce/node_modules/bson/lib/bson/parser/serializer.js:308:18)
    at serializeInto (/Users/oded/wisdo/bug-reproduce/node_modules/bson/lib/bson/parser/serializer.js:776:17)
    at serializeObject (/Users/oded/wisdo/bug-reproduce/node_modules/bson/lib/bson/parser/serializer.js:308:18)
    at serializeInto (/Users/oded/wisdo/bug-reproduce/node_modules/bson/lib/bson/parser/serializer.js:776:17)
    at BSON.serialize (/Users/oded/wisdo/bug-reproduce/node_modules/bson/lib/bson/bson.js:58:27)
    at Query.toBin (/Users/oded/wisdo/bug-reproduce/node_modules/mongodb-core/lib/connection/commands.js:140:25)
    at Pool.write (/Users/oded/wisdo/bug-reproduce/node_modules/mongodb-core/lib/connection/pool.js:986:23)
    at Cursor._find (/Users/oded/wisdo/bug-reproduce/node_modules/mongodb-core/lib/cursor.js:286:22)
    at nextFunction (/Users/oded/wisdo/bug-reproduce/node_modules/mongodb-core/lib/cursor.js:591:10)
    at Cursor.next [as _next] (/Users/oded/wisdo/bug-reproduce/node_modules/mongodb-core/lib/cursor.js:699:3)
    at fetchDocs (/Users/oded/wisdo/bug-reproduce/node_modules/mongodb/lib/cursor.js:857:10)

I expected the output to be:

Starting
Error Caught

Note: I understand that the query size is limited and when it is exceeded the query will fail.
However I expect to fail with an error in the Promise thread that caused this.

What happens is that Node crashes in a separate execution thread and I could find no way to figure out what query causes this error.

In my specific scenario an express server was querying one collection to get a list of Ids then using a second collection to look up all the matching documents.
When a user would use a filter condition that would match too many ids the system would simply crash because the resulting query is too large.

@sobafuchs
Copy link
Contributor

oof, this looks like a problem with the underlying bson module which can't handle the size of the array you're passing in. It looks like mongoose isn't containing any errors that might bubble up from the stuff that prepares the data. I'll mark this as a bug for now.

@sobafuchs sobafuchs added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. bug? and removed confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. labels Nov 15, 2017
@sobafuchs sobafuchs added this to the 4.13.4 milestone Nov 15, 2017
@sobafuchs sobafuchs added the underlying library issue This issue is a bug with an underlying library, like the MongoDB driver or mongodb-core label Nov 15, 2017
@vkarpov15 vkarpov15 modified the milestones: 4.13.4, 4.13.5 Nov 17, 2017
vkarpov15 added a commit that referenced this issue Nov 19, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
underlying library issue This issue is a bug with an underlying library, like the MongoDB driver or mongodb-core
Projects
None yet
Development

No branches or pull requests

3 participants