[SERVER-33227] Using the method `connect` in a script will as a side effect update global `db`. Created: 09/Feb/18  Updated: 08/Jan/24  Resolved: 23/Feb/18

Status: Closed
Project: Core Server
Component/s: Shell
Affects Version/s: 3.4.0, 3.6.0, 3.7.2
Fix Version/s: 3.4.14, 3.6.4, 3.7.3

Type: Bug Priority: Major - P3
Reporter: Peter Ahlers Assignee: Kevin Pulo
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: File connect-fixed.js    
Issue Links:
Backports
Related
related to SERVER-22382 mongo shell should accept mongodb:// ... Closed
Backwards Compatibility: Fully Compatible
Operating System: ALL
Backport Requested:
v3.6, v3.4
Steps To Reproduce:

Start two mongodbs (A and B).

//psuedo-code
mongo A --eval="var connectionB = connect(B); print('Connection A', db); print('Connect B', connectionB)";

Output will be something like
Connection A

{server:B}

Connection B

{server:B}
Sprint: Sharding 2018-03-12
Participants:

 Description   

Using the method `connect` in a script will as a side effect update global `db` with the new connection. This is not how connect is described in the documentation https://docs.mongodb.com/manual/reference/method/connect/.

The bug is introduced in version r3.3.12, in commit 88b540f0c14c7ab8af708aecc4cd6df83081b32e.

On line 235 you see the connection is stored in `db`. Because the declaration of db is removed (old line 240) the new connection is stored in the global `db`. The global `db` is used for the connection where the script is running.



 Comments   
Comment by Peter Ahlers [ 26/Feb/18 ]

Kevin Pulo, thanks for the script and fix.

Comment by Githook User [ 26/Feb/18 ]

Author:

{'email': 'kevin.pulo@mongodb.com', 'name': 'Kevin Pulo', 'username': 'devkev'}

Message: SERVER-33227 don't modify global `db` in shell `connect()`

(cherry picked from commit 1af47155814e1628b92597a58ed489b7509b6425)
Branch: v3.4
https://github.com/mongodb/mongo/commit/1e541da1aa391bee43548388c98451df0e54e1c1

Comment by Githook User [ 26/Feb/18 ]

Author:

{'email': 'kevin.pulo@mongodb.com', 'name': 'Kevin Pulo', 'username': 'devkev'}

Message: SERVER-33227 don't modify global `db` in shell `connect()`

(cherry picked from commit 1af47155814e1628b92597a58ed489b7509b6425)
Branch: v3.6
https://github.com/mongodb/mongo/commit/39ca8f82721991f53c03c5ed3dfd285607d52fe4

Comment by Githook User [ 23/Feb/18 ]

Author:

{'email': 'kevin.pulo@mongodb.com', 'name': 'Kevin Pulo', 'username': 'devkev'}

Message: SERVER-33227 don't modify global `db` in shell `connect()`
Branch: master
https://github.com/mongodb/mongo/commit/1af47155814e1628b92597a58ed489b7509b6425

Comment by Kevin Pulo [ 23/Feb/18 ]

Hi Peter,

Thanks for letting us know about this! I can confirm that this is an unexpected regression in the behaviour of the connect() shell helper:

$ for i in 1.8.5 2.0.9 2.2.7 2.4.14 2.6.12 3.0.15 3.2.19 3.4.13 3.6.3 3.7.2; do echo;echo; $i/bin/mongo --port 11111 --eval 'shellPrint(db.getMongo()); var db2 = connect("localhost:22222/test"); shellPrint(db.getMongo()); shellPrint(db2.getMongo());'; done
 
 
MongoDB shell version: 1.8.5
connecting to: 127.0.0.1:11111/test
connection to 127.0.0.1:11111
connecting to: localhost:22222/test
connection to 127.0.0.1:11111
connection to localhost:22222
 
 
MongoDB shell version: 2.0.9
connecting to: 127.0.0.1:11111/test
connection to 127.0.0.1:11111
connecting to: localhost:22222/test
connection to 127.0.0.1:11111
connection to localhost:22222
 
 
MongoDB shell version: 2.2.7
connecting to: 127.0.0.1:11111/test
connection to 127.0.0.1:11111
connecting to: localhost:22222/test
connection to 127.0.0.1:11111
connection to localhost:22222
 
 
MongoDB shell version: 2.4.14
connecting to: 127.0.0.1:11111/test
connection to 127.0.0.1:11111
connecting to: localhost:22222/test
connection to 127.0.0.1:11111
connection to localhost:22222
 
 
MongoDB shell version: 2.6.12
connecting to: 127.0.0.1:11111/test
connection to 127.0.0.1:11111
connecting to: localhost:22222/test
connection to 127.0.0.1:11111
connection to localhost:22222
 
 
MongoDB shell version: 3.0.15
connecting to: 127.0.0.1:11111/test
connection to 127.0.0.1:11111
connecting to: localhost:22222/test
connection to 127.0.0.1:11111
connection to localhost:22222
 
 
MongoDB shell version: 3.2.19
connecting to: 127.0.0.1:11111/test
connection to 127.0.0.1:11111
connecting to: localhost:22222/test
connection to 127.0.0.1:11111
connection to localhost:22222
 
 
MongoDB shell version v3.4.13
connecting to: mongodb://127.0.0.1:11111/
MongoDB server version: 3.4.10
connection to 127.0.0.1:11111
connecting to: mongodb://localhost:22222/test
MongoDB server version: 3.4.10
connection to localhost:22222
connection to localhost:22222
 
 
MongoDB shell version v3.6.3
connecting to: mongodb://127.0.0.1:11111/
MongoDB server version: 3.4.10
WARNING: shell and server versions do not match
connection to 127.0.0.1:11111
connecting to: mongodb://localhost:22222/test
MongoDB server version: 3.4.10
WARNING: shell and server versions do not match
connection to localhost:22222
connection to localhost:22222
 
 
MongoDB shell version v3.7.2
connecting to: mongodb://127.0.0.1:11111/
MongoDB server version: 3.4.10
WARNING: shell and server versions do not match
connection to 127.0.0.1:11111
connecting to: mongodb://localhost:22222/test
MongoDB server version: 3.4.10
WARNING: shell and server versions do not match
connection to localhost:22222
connection to localhost:22222

As expected, adding var in front of the definition of db fixes the problem:

$ diff -u connect-orig.js connect-fixed.js
--- connect-orig.js     2018-02-23 14:03:39.650636341 +1100
+++ connect-fixed.js    2018-02-23 14:04:05.283507435 +1100
@@ -43,7 +43,7 @@
 
     chatty("connecting to: " + url);
     var m = new Mongo(url);
-    db = m.getDB(m.defaultDB);
+    var db = m.getDB(m.defaultDB);
 
     if (user && pass) {
         if (!db.auth(user, pass)) {
$ 3.7.2/bin/mongo --port 11111 --eval 'load("connect-orig.js"); shellPrint(db.getMongo()); var db2 = connect("localhost:22222/test"); shellPrint(db.getMongo()); shellPrint(db2.getMongo());'
MongoDB shell version v3.7.2
connecting to: mongodb://127.0.0.1:11111/
MongoDB server version: 3.4.10
WARNING: shell and server versions do not match
connection to 127.0.0.1:11111
connecting to: mongodb://localhost:22222/test
MongoDB server version: 3.4.10
WARNING: shell and server versions do not match
connection to localhost:22222
connection to localhost:22222
$ 3.7.2/bin/mongo --port 11111 --eval 'load("connect-fixed.js"); shellPrint(db.getMongo()); var db2 = connect("localhost:22222/test"); shellPrint(db.getMongo()); shellPrint(db2.getMongo());'
MongoDB shell version v3.7.2
connecting to: mongodb://127.0.0.1:11111/
MongoDB server version: 3.4.10
WARNING: shell and server versions do not match
connection to 127.0.0.1:11111
connecting to: mongodb://localhost:22222/test
MongoDB server version: 3.4.10
WARNING: shell and server versions do not match
connection to 127.0.0.1:11111
connection to localhost:22222

I will now work on getting this fix committed and released. In the meantime, you can work around the problem by manually loading the above connect-fixed.js file (attached to this ticket).

Best regards,
Kev

Comment by Kelsey Schubert [ 12/Feb/18 ]

Hi pahlers,

Thanks for the clear bug report and investigative work – we're looking into it.

Kind regards,
Kelsey

Generated at Thu Feb 08 04:32:43 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.