I think I see the problem but I'm not sure where to fix it as it is a little deep.
Forgot to mention that I not only have auth, but multiple databases in my Mongo instance. I authenticate with a username and password that are in the admin db. Then in the client code I do a bunch of db.db("some_non_admin_db"), which is all fine.
The problem is that when the primary fails and the retry happens:
1) self.auths is not being copied over during db.db, so the unfortunate Db object doesn't even know it needs to authenticate (but that's a trivial fix)
2) even with the above fix, the Db is no longer admin, but something else, so it can't authenticate with the admin credentials (and that's just an annoying mongo weirdness, but what can you do).
So it feels like the Db and the Connection abstractions are not decoupled enough IMHO to address the mongo admin users weirdness. Under the _retry, we'll have to stop the operation, somehow authenticate the Db the credentials are for, and and resume the operation.
Would love to hear suggestion on workarounds in client code (besides drop the auth).
Ivan