fastify / fastify-mongodb

Fastify MongoDB connection plugin

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

MongoError: Cannot use a session that has ended (in tap test suites)

mashpie opened this issue · comments

In case someone ran into this

Fresh install of fastify-mongodb installed mongodb@3.5.3. Now any tap test suite break with "MongoError: Cannot use a session that has ended", even without mongo operations. Last working version seamed to be mongodb@3.4.1 - pinnig to it by

 "resolutions": {
    "mongodb": "3.4.1"
  }

resolves... as a quick fix. Root-Cause will probably be on mongodb's side. Wanted to share my experience for anyone running into same issue.

To Reproduce

I wanted to setup demo repo with an excerpt of my breaking setup. Guess what: It works! I will take some time to figure out

Expected behavior

I've seen plenty of issues with mongodb driver before, like you did with greenkeeper upgrades. I'd expect maintainers at mongo to not break on minor upgrades any more - but this is out of scope of this report... just wanted to provide a quick workaround fix... will do my best to reproduce though. Thanks!

Your Environment

  • node version: 12.x
  • fastify version: >=2.0.0
  • os: Mac & Linux

We have not seen that specific failure with greenkeeper in this repo. Maybe are you doing something that got problematic with the MongoDB driver?

Could be... as in my setup we ensure some indices in collections on startup. Newer mongo driver doesn‘t wait for running processes to be finished before closing connections(ie on teardown of tests), probavly. I will inestigate further and report later today. Thanks!

Turns out that this was caused by not properly awaiting promises in my code to finish, ie.:

worked until <=3.4.1

module.exports = async fastify => {
  const collection = fastify.mongo.db.collection('issue')
  collection.createIndex({ id: 1 }, { unique: true })

  // [...] 
}

fixed for >=3.5.0 upwards

module.exports = async fastify => {
  const collection = fastify.mongo.db.collection('issue')
  await collection.createIndex({ id: 1 }, { unique: true })

  // [...] 
}

Quite obvious as a server daemon will run "forever" and so background creation of index will finish some time. In a test the connection seams to get cut before finishing that background creation, which yields an error and breaks tests. One could consider that as a bug of mongodb-driver... But hey: Promises should be awaited and that way we "don't run into that bug". Hope that doesn't compromise startup time.

Full Demo: https://github.com/uscreen/mongo-issue

closing - battle tested in production as well as in CI/QA across certain projects.

Thanks a lot and stay safe!

Wow! Thank you!