vercel: MongoDB Integration causes intermittent connection errors (LAMBDA_RUNTIME fails)
Discussed in https://github.com/vercel/vercel/discussions/10621
<div type='discussions-op-text'>Originally posted by Nikhil22 September 28, 2023
Question
When I connect to MongoDB (via MONGODB_URI env variable, via the node driver) from my Vercel-deployed app, I get intermittent errors (MongoServerSelectionError) that present as 500 errors in my Vercel-deployed app. The errors go away on refresh.
An example of the error is:
ERROR Unhandled Promise Rejection
{
"errorType":"Runtime.UnhandledPromiseRejection",
"errorMessage":"MongoServerSelectionError: Server selection timed out after 30000 ms",
"reason":{
"errorType":"MongoServerSelectionError",
"errorMessage":"Server selection timed out after 30000 ms",
"reason":{
"type":"ReplicaSetNoPrimary",
"servers":{
},
"stale":false,
"compatible":true,
"heartbeatFrequencyMS":10000,
"localThresholdMS":15,
"setName":"atlas-j7739j-shard-0",
"maxElectionId":null,
"maxSetVersion":null,
"commonWireVersion":0,
"logicalSessionTimeoutMinutes":null
},
"stack":[
"MongoServerSelectionError: Server selection timed out after 30000 ms",
" at Timeout._onTimeout (/var/task/node_modules/mongodb/lib/sdam/topology.js:278:38)",
" at listOnTimeout (node:internal/timers:569:17)",
" at process.processTimers (node:internal/timers:512:7)"
]
},
"promise":{
},
"stack":[
"Runtime.UnhandledPromiseRejection: MongoServerSelectionError: Server selection timed out after 30000 ms",
" at process.<anonymous> (file:///var/runtime/index.mjs:1250:17)",
" at process.emit (node:events:526:35)",
" at emit (node:internal/process/promises:149:20)",
" at processPromiseRejections (node:internal/process/promises:283:27)",
" at process.processTicksAndRejections (node:internal/process/task_queues:96:32)"
]
}
"LAMBDA_RUNTIME Failed to post handler success response. Http response code":"400.
Just to be clear, the connection string and credentials work and are correct as it does connect properly when errors are not there, but when errors that I describe above do occur, it crashes the entire app.
I’ve written about this in detail in the MongoDB community here (https://www.mongodb.com/community/forums/t/no-way-to-avoid-replicasetnoprimary-errors/243498).
I’ve also been in contact with MongoDB support and they said that there are no problems with the Mongo cluster itself.
Moreover, upon further research, it seems that this is a common problem when trying to connect to MongoDB from Vercel. For example, here (https://www.mongodb.com/community/forums/t/intermittently-getting-mongoserverselectionerror-error-on-my-next-js-vercel-deployment/242470).
I’d like to solve this problem, or at the very least, not have my entire app crash with a 500 due to the error, as it currently does.
Please help.</div>
About this issue
- Original URL
- State: closed
- Created 9 months ago
- Reactions: 13
- Comments: 18
yup same issue
Same problem here. Occasionally, the communication with MongoDB simply crashes and we need to restart the application on Vercel (PRO).
Please, the community need a workaround at least.
PLEASE FIX
I’ve got the same issue, here is a full log:
Unhandled Promise Rejection {"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"MongoNetworkTimeoutError: connection timed out","reason":{"errorType":"MongoNetworkTimeoutError","errorMessage":"connection timed out","connectionGeneration":0,"stack":["MongoNetworkTimeoutError: connection timed out"," at connectionFailureError (/var/task/node_modules/.pnpm/mongodb@6.3.0/node_modules/mongodb/lib/cmap/connect.js:381:20)"," at TLSSocket.<anonymous> (/var/task/node_modules/.pnpm/mongodb@6.3.0/node_modules/mongodb/lib/cmap/connect.js:285:22)"," at Object.onceWrapper (node:events:631:28)"," at TLSSocket.emit (node:events:517:28)"," at TLSSocket.emit (node:domain:489:12)"," at Socket._onTimeout (node:net:598:8)"," at listOnTimeout (node:internal/timers:569:17)"," at process.processTimers (node:internal/timers:512:7)"]},"promise":{},"stack":["Runtime.UnhandledPromiseRejection: MongoNetworkTimeoutError: connection timed out"," at process.<anonymous> (file:///var/runtime/index.mjs:1276:17)"," at process.emit (node:events:529:35)"," at process.emit (node:domain:489:12)"," at emit (node:internal/process/promises:149:20)"," at processPromiseRejections (node:internal/process/promises:283:27)"," at process.processTicksAndRejections (node:internal/process/task_queues:96:32)"]} Error: Runtime exited with error: exit status 128 (Unhandled Promise Rejection)It appears at random and throws 500 errors, it seems that it doesn’t follow a definitive pattern.
I’m also using Prisma as ORM, but it doesn’t seems to be linked to that.
I found some clues about people having the same issue on AWS Lambda.
Same problem. Please fix this
Same thing happening here, and my app is in production. Not a good UX. I hope they release a fix ASAP.
Same issue here. Is there any news on this, or hope?
Yea, it’s really tough. I’ve done a ton of debugging and I really think it’s outside the developers control. When the issue happens NO logging in my code ever runs, even things that are output before calls to the DB code. My best guess:
A previous call leaves a connection open, during the management of that connection something goes wrong and throws an unhandled exception, when a future request is handled by the same thread it already has an unhandled exception waiting for it.
I have no real evidence of this but its the only thing that makes sense.
Also, monitoring the events I see the serverClosedEvent happens almost at the same time as the error.