Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-14833

Creating descending index on _id can corrupt namespace

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Critical - P2 Critical - P2
    • 2.4.11
    • Affects Version/s: 2.4.10
    • Component/s: Index Maintenance
    • None
    • Fully Compatible
    • ALL
    • Hide

      db.foo.ensureIndex({_id:-1});
      db.foo.ensureIndex({_id:-1});

      Show
      db.foo.ensureIndex({_id:-1}); db.foo.ensureIndex({_id:-1});

      Issue Status as of Aug 21, 2014

      ISSUE SUMMARY
      It is possible to create multiple _id indexes on a collection using ensureIndex({_id:-1}).

      db.foo.ensureIndex({_id:-1})
      db.foo.ensureIndex({_id:-1})
      db.foo.ensureIndex({_id:-1})
      db.foo.getIndexes().length 
      4
      

      Although having multiple _id indexes on a collection is not intended behavior, the collection may be usable in this state. However, an attempt to subsequently drop such a collection corrupts the namespace:

      db.foo.drop()
      Fri Aug  8 16:43:48.593 drop failed: {
          "nIndexesWas" : 4,
          "errmsg" : "exception: drop: dropIndexes for collection failed - consider trying repair  cause: BSONObj size: 0 (0x00000000) is invalid. Size must be between 0 and 16793600(16MB) First element: EOO",
          "code" : 12503,
          "ok" : 0
      } at src/mongo/shell/collection.js:383
      

      USER IMPACT
      The affected namespace is corrupted upon collection drop, and cannot be used until it is repaired.

      WORKAROUNDS
      Create _id indexes with ensureIndex({_id: 1}) instead of ensureIndex({_id: -1}). If the indexes have already been created and the affected collection needs to be dropped, run db.repairDatabase() afterwards to fix the namespace corruption.

      AFFECTED VERSIONS
      MongoDB 2.4 production releases are affected by this issue.

      FIX VERSION
      The fix is included in the 2.4.11 production release.

      RESOLUTION DETAILS
      Reject ensureIndex({{_id:-1}) requests if the _id index already exists.

      Original description

      Affects only 2.4 branch (issue not in 2.6.3 nor in master as of 7 Aug 2014 GMT)

      You can insert the same descending _id index multiple times, as they are inserting as ascending each time.

      Create a descending _id index:

      db.foo.ensureIndex({_id:-1});
      db.foo.ensureIndex({_id:-1});
      db.foo.ensureIndex({_id:-1});
      

      Check the count

      > db.foo.getIndexes().length 
      4
      

      We have created the same index 3 times.

      Attempting to drop the collection will corrupt the namespace

      > db.foo.drop();
      Fri Aug  8 16:43:48.593 drop failed: {
      	"nIndexesWas" : 4,
      	"errmsg" : "exception: drop: dropIndexes for collection failed - consider trying repair  cause: BSONObj size: 0 (0x00000000) is invalid. Size must be between 0 and 16793600(16MB) First element: EOO",
      	"code" : 12503,
      	"ok" : 0
      } at src/mongo/shell/collection.js:383
      

            Assignee:
            rassi J Rassi
            Reporter:
            david.hows David Hows
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved: