[SERVER-7660] findAndModify causes exception when "new" and "upsert" are true and a sort is provided (with auth enabled) Created: 14/Nov/12  Updated: 11/Jul/16  Resolved: 14/Nov/12

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: 2.2.0
Fix Version/s: 2.2.3, 2.3.1

Type: Bug Priority: Major - P3
Reporter: Olivier Vaillancourt Assignee: Eliot Horowitz (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

OSX 64bit - 10.8.2 (Mountain Lion)
CentoOS release 6.3 (Final) - Linux 2.6.32-220.23.1.el6.x86_64 x86_64


Operating System: ALL
Steps To Reproduce:

1- Set up (or start) a database server with authentication enabled.
2- Open the mongodb shell
3- select database + authenticate
4- Paste the following in the shell:

db.testCollection.drop();
db.testCollection.findAndModify({ 
  query : { foo : 'bar' }, 
  update : { $set : { bob : 'john' } }, 
  sort: { foo : 1}, 
  upsert: true, 
  new : true 
})

Participants:

 Description   

findAndModify queries throw an exception if all of the following conditions are met:

  • the query parameter doesn't match an existing object in the collection
  • new is set to true
  • upsert is set to true
  • a sort is provided
  • authentication must be enabled on the server (can't reproduce when auth is disabled)

Other notes:

  • Indexing has no effect on the ability to reproduce the bug.
  • The sort's content does not impact reproductibility. It can be empty and the issue will still occur.
  • The document is correctly inserted in the collection despite the exception.
  • Doing a 2nd identical findAndModify query doesn't create the exception.
  • The issue doesn't occur on mongo 2.0.7

Error:

The shell outputs the following when the exception occurs :

	"lastErrorObject" : {
		"errmsg" : "need to login",
		"ok" : 0
	},
	"errmsg" : "exception: assertion src/mongo/db/../bson/bsonobjbuilder.h:131",
	"code" : 0,
	"ok" : 0

The stack trace is as follow (along with a couple of lines prior/after for context):

Tue Nov 13 18:46:37 [conn3] run command test.$cmd { findandmodify: "testCollection", query: { foo: "bar" }, update: { $set: { bob: "john" } }, sort: { foo: 1.0 }, upsert: true, new: true }
Tue Nov 13 18:46:37 [conn3] query test.testCollection query: { query: { foo: "bar" }, orderby: { foo: 1.0 } } ntoreturn:1 scanAndOrder:1 keyUpdates:0  nreturned:0 reslen:20 0ms
Tue Nov 13 18:46:37 [conn3] update test.testCollection query: { foo: "bar" } update: { $set: { bob: "john" } } nscanned:1 nupdated:1 fastmodinsert:1 keyUpdates:0  13ms
Tue Nov 13 18:46:37 [conn3] run command admin.$cmd { getlasterror: 1 }
Tue Nov 13 18:46:37 [conn3] command admin.$cmd command: { getlasterror: 1 } ntoreturn:1 keyUpdates:0  reslen:63 0ms
Tue Nov 13 18:46:37 [conn3]  test Assertion failure !e.eoo() src/mongo/db/../bson/bsonobjbuilder.h 131
0x1047c7c0b 0x1047a5c8c 0x104466f47 0x104519cfd 0x10454b89b 0x10454c91b 0x10454d004 0x10460acae 0x10460b43c 0x1045c0b25 0x10445bb02 0x1047be23d 0x1047f92c5 0x7fff8ef0a742 0x7fff8eef7181 
 0   mongod                              0x00000001047c7c0b _ZN5mongo15printStackTraceERSo + 43
 1   mongod                              0x00000001047a5c8c _ZN5mongo12verifyFailedEPKcS1_j + 284
 2   mongod                              0x0000000104466f47 _ZN5mongo14BSONObjBuilder8appendAsERKNS_11BSONElementERKNS_10StringDataE + 375
 3   mongod                              0x0000000104519cfd _ZN5mongo16CmdFindAndModify3runERKSsRNS_7BSONObjEiRSsRNS_14BSONObjBuilderEb + 2613
 4   mongod                              0x000000010454b89b _ZN5mongo12_execCommandEPNS_7CommandERKSsRNS_7BSONObjEiRNS_14BSONObjBuilderEb + 91
 5   mongod                              0x000000010454c91b _ZN5mongo11execCommandEPNS_7CommandERNS_6ClientEiPKcRNS_7BSONObjERNS_14BSONObjBuilderEb + 2907
 6   mongod                              0x000000010454d004 _ZN5mongo12_runCommandsEPKcRNS_7BSONObjERNS_11_BufBuilderINS_16TrivialAllocatorEEERNS_14BSONObjBuilderEbi + 1076
 7   mongod                              0x000000010460acae _ZN5mongo11runCommandsEPKcRNS_7BSONObjERNS_5CurOpERNS_11_BufBuilderINS_16TrivialAllocatorEEERNS_14BSONObjBuilderEbi + 46
 8   mongod                              0x000000010460b43c _ZN5mongo8runQueryERNS_7MessageERNS_12QueryMessageERNS_5CurOpES1_ + 1436
 9   mongod                              0x00000001045c0b25 _ZN5mongo16assembleResponseERNS_7MessageERNS_10DbResponseERKNS_11HostAndPortE + 1349
 10  mongod                              0x000000010445bb02 _ZN5mongo16MyMessageHandler7processERNS_7MessageEPNS_21AbstractMessagingPortEPNS_9LastErrorE + 206
 11  mongod                              0x00000001047be23d _ZN5mongo3pms9threadRunEPNS_13MessagingPortE + 1645
 12  mongod                              0x00000001047f92c5 thread_proxy + 229
 13  libsystem_c.dylib                   0x00007fff8ef0a742 _pthread_start + 327
 14  libsystem_c.dylib                   0x00007fff8eef7181 thread_start + 13
Tue Nov 13 18:46:37 [conn3] command test.$cmd command: { findandmodify: "testCollection", query: { foo: "bar" }, update: { $set: { bob: "john" } }, sort: { foo: 1.0 }, upsert: true, new: true } ntoreturn:1 keyUpdates:0 locks(micros) w:15909 reslen:182 15ms



 Comments   
Comment by auto [ 08/Jan/13 ]

Author:

{u'date': u'2012-11-14T05:17:52Z', u'name': u'Eliot Horowitz', u'email': u'eliot@10gen.com'}

Message: SERVER-7660 findAndModify with auth + sort fix
Branch: v2.2
https://github.com/mongodb/mongo/commit/aecd951b3df16248769a01682e8863656a564e8a

Comment by Olivier Vaillancourt [ 14/Nov/12 ]

Awesome thanks!

Comment by auto [ 14/Nov/12 ]

Author:

{u'date': u'2012-11-14T05:17:52Z', u'email': u'eliot@10gen.com', u'name': u'Eliot Horowitz'}

Message: SERVER-7660 findAndModify with auth + sort fix
Branch: master
https://github.com/mongodb/mongo/commit/80aaf5e73a4223b506c1630d6a7f3959acb3d840

Comment by Olivier Vaillancourt [ 14/Nov/12 ]

2.2.1 can be added to the list of affected versions.

Comment by Olivier Vaillancourt [ 14/Nov/12 ]

Quick note concerning the operating systems - I've marked it as "all" but it's only been tested on OSX and Linux.

Generated at Thu Feb 08 03:15:12 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.