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(db): throw readable error when creating _id with background: true #1740

Merged
merged 1 commit into from Jun 18, 2018
Merged

fix(db): throw readable error when creating _id with background: true #1740

merged 1 commit into from Jun 18, 2018

Conversation

vkarpov15
Copy link
Contributor

My least favorite bug strikes again: createIndex() falls back to inserting an index doc and swallows the original error. Below is a minimal repro script with mongodb@3.0.9:

const { MongoClient } = require('mongodb');

run().catch(error => console.error(error.stack));

async function run() {
  const client = await MongoClient.connect('mongodb://localhost:27017/test');

  const db = client.db();

  await db.collection('Test2').ensureIndex({ _id: 1 }, { background: true });

  console.log('Done');
}

This throws a very unhelpful error:

$ node gh-6109.js 
Error: cyclic dependency detected
    at serializeObject (/home/val/Workspace/MongoDB/troubleshoot-mongoose/node_modules/bson/lib/bson/parser/serializer.js:333:34)
    at serializeInto (/home/val/Workspace/MongoDB/troubleshoot-mongoose/node_modules/bson/lib/bson/parser/serializer.js:937:17)
    at serializeObject (/home/val/Workspace/MongoDB/troubleshoot-mongoose/node_modules/bson/lib/bson/parser/serializer.js:347:18)
    at serializeInto (/home/val/Workspace/MongoDB/troubleshoot-mongoose/node_modules/bson/lib/bson/parser/serializer.js:937:17)
    at serializeObject (/home/val/Workspace/MongoDB/troubleshoot-mongoose/node_modules/bson/lib/bson/parser/serializer.js:347:18)
    at serializeInto (/home/val/Workspace/MongoDB/troubleshoot-mongoose/node_modules/bson/lib/bson/parser/serializer.js:937:17)
    at serializeObject (/home/val/Workspace/MongoDB/troubleshoot-mongoose/node_modules/bson/lib/bson/parser/serializer.js:347:18)
    at serializeInto (/home/val/Workspace/MongoDB/troubleshoot-mongoose/node_modules/bson/lib/bson/parser/serializer.js:937:17)
    at serializeObject (/home/val/Workspace/MongoDB/troubleshoot-mongoose/node_modules/bson/lib/bson/parser/serializer.js:347:18)
    at serializeInto (/home/val/Workspace/MongoDB/troubleshoot-mongoose/node_modules/bson/lib/bson/parser/serializer.js:937:17)

Admittedly, trying to create an index on _id is impossible because MongoDB creates one by default, but plenty of people do this in mongoose, so a lot of people are getting this baffling "cyclic dependency detected" error (see Automattic/mongoose#6109). Unfortunately I can't fix this issue from mongoose, so can this get into 3.0.x so I can close out Automattic/mongoose#6109?

@vkarpov15
Copy link
Contributor Author

Also, a related issue: what do you think about adding a toBSON() method to the ClientSession class? This cyclic dependency is because bson chokes on serializing a session.

@mbroadst
Copy link
Member

@vkarpov15 thanks for this! I agree we should throw a toBSON on ClientSession, if not just to have it throw an exception. I made NODE-1518 to track this, perhaps one of our interns can even pick it up today.

@mbroadst mbroadst merged commit aac47a1 into mongodb:3.0 Jun 18, 2018
@vkarpov15 vkarpov15 deleted the patch-3 branch June 19, 2018 12:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants