[SERVER-35758] Mongo shell prompt errors when running transactions after overriding "db" Created: 22/Jun/18  Updated: 29/Oct/23  Resolved: 10/Jul/18

Status: Closed
Project: Core Server
Component/s: Replication, Shell
Affects Version/s: None
Fix Version/s: 4.0.1, 4.1.1

Type: Bug Priority: Major - P3
Reporter: Spencer Brody (Inactive) Assignee: Siyuan Zhou
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Backports
Related
Backwards Compatibility: Fully Compatible
Operating System: ALL
Backport Requested:
v4.0
Sprint: Repl 2018-07-02, Repl 2018-07-16
Participants:

 Description   

> session = db.getMongo().startSession()
> db = session.getDatabase('test')
test
> session.startTransaction()
> db.foo.insert({a:1})
Given transaction number 0 does not match any in-progress transactions.
WriteCommandError({
	"errorLabels": [
		"TransientTransactionError"
	],
	"operationTime": Timestamp(1529690056, 1),
	"ok": 0,
	"errmsg": "Given transaction number 0 does not match any in-progress transactions.",
	"code": 251,
	"codeName": "NoSuchTransaction",
	"$clusterTime": {
		"clusterTime": Timestamp(1529690056, 1),
		"signature": {
			"hash": BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId": NumberLong(0)
		}
	}
})



 Comments   
Comment by Githook User [ 13/Jul/18 ]

Author:

{'username': 'visualzhou', 'name': 'Siyuan Zhou', 'email': 'siyuan.zhou@mongodb.com'}

Message: SERVER-35758 Mongo shell prompt errors when running transactions after overriding 'db'

(cherry picked from commit bb9f225e47faacb95ae9e5658313a5ebf56f37bf)
Branch: v4.0
https://github.com/mongodb/mongo/commit/d1d58971adb648aa5f2750f206bb0fba88a8214e

Comment by Githook User [ 10/Jul/18 ]

Author:

{'username': 'visualzhou', 'name': 'Siyuan Zhou', 'email': 'siyuan.zhou@mongodb.com'}

Message: SERVER-35758 Mongo shell prompt errors when running transactions after overriding 'db'
Branch: master
https://github.com/mongodb/mongo/commit/bb9f225e47faacb95ae9e5658313a5ebf56f37bf

Comment by Siyuan Zhou [ 22/Jun/18 ]

alyson.cabral, updated the title.

Comment by Alyson Cabral (Inactive) [ 22/Jun/18 ]

Can we update the title of the ticket to reflect the scope of this bug? spencer

Comment by Bruce Lucas (Inactive) [ 22/Jun/18 ]

Reassigning db is a tempting thing to do given the session API (as the example illustrates). If this is dangerous is there some way we can prevent it?

Comment by Andy Schwerin [ 22/Jun/18 ]

The problem stems from the user assigning a "session bound" Database object to the global "db" variable, which is consulted by shell prompt generators. spencer pointed out in person that we could adjust the environment in which the prompt is generated to always have a regular, session-less Database object during prompt generation. This seems like it would be nice to have, but users aren't really expected to override the "db" object in the interactive shell, and a lot of other surprising things might result.

Comment by Kay Kim (Inactive) [ 22/Jun/18 ]

FYI – the error doesn't occur if you don't override the db object in the shell:

shardA:PRIMARY> session = db.getMongo().startSession({ mode: "primary" } )
session { "id" : UUID("8109718c-1e16-494e-9db2-03f2bf8cdd6f") }
shardA:PRIMARY> foo = session.getDatabase("test").foo
test.foo
shardA:PRIMARY>   session.startTransaction( { readConcern: { level: "snapshot" }, writeConcern: { w: "majority" } } );
shardA:PRIMARY> foo.find({})
shardA:PRIMARY> session.commitTransaction()
shardA:PRIMARY> session.endSession()

And in our txn docs, all our mongo shell examples get the collection instead of db:

    employeesCollection = session.getDatabase("hr").employees;
    eventsCollection = session.getDatabase("reporting").events;

Comment by Spencer Brody (Inactive) [ 22/Jun/18 ]

It looks like the issue that for the shell's interactive mode we try to run isMaster/replSetGetStatus to provide information to the prompt, and in doing so we send the isMaster with the transaction arguments including 'startTransaction', so when we get to the first real transaction command, we assume it's the second or third statement in the transaction and thus don't attach 'startTransaction' and then get the NoSuchTransaction error.

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