Automattic / mongoose

MongoDB object modeling designed to work in an asynchronous environment.
https://mongoosejs.com
MIT License
26.96k stars 3.84k forks source link

Creating a document with invalid data timeouts in AWS Lambda #10196

Closed GeorgiMateev closed 3 years ago

GeorgiMateev commented 3 years ago

Do you want to request a feature or report a bug? Bug

What is the current behavior? We use Mongoose in AWS Lambda. The lambda reads and writes data to Mongo Atlas. We try to create and save document with input data which can't be converted to the schema type. When we test the code locally using Serverless offline we see the error in the logs and the catched exception could be handled. The same code in Lambda timeouts on the following cases instead of throwing error.

You can see the lambda invocation logs in CloudWatch - the logs just stop before creating the document and then after 20 sec. the lambda timeouts.

We disabled buffering and you can see in the logs that the connection status is 1.

We already talked with the support of Mongo Atlas - there are no server errors, no long running queries.

The support of AWS confirmed that there are no network connectivity issues to Mongo Atlas. Other queries in the same Lambda invocation are successful.

If the current behavior is a bug, please provide the steps to reproduce. I am attaching stripped Serverless project which can be run locally or in AWS. Instructions how to test it are inside the README.

timeout-test.zip

What is the expected behavior? The code have to throw the same error as we see locally and this error have to be handled in the catch block without causing the Lambda to timeout.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version. Node 12 (this problem also appears in Node 14) "dependencies": { "mongodb": "3.6.4", "mongodb-client-encryption": "1.1.0", "mongoose": "5.12.5" } Mongo Atlas cluster version: 4.4.5 - One primary, two replicas

vkarpov15 commented 3 years ago

I set up and ran your code locally, and below is the output I get:

Mongoose default connection is with state: 1
About to create Mongoose doc
Mongoose doc created.
About to save mongoose doc
Error processing. Error: ValidationError. Message: Q validation failed: pieces: Cast to Number failed for value "should be number" at path "pieces"
Finally

Which shows that Mongoose is throwing an error and your try/catch is handling it. Not sure what you expect to happen here. I also don't see any code in your timeout-test.zip file that executes a find() or findOne(), so I'm not sure how this example code fits in with your bug report.

GeorgiMateev commented 3 years ago

Hello, thanks for the reply. That‘s the expected behavior when you run it on your machine. Did you tried to deploy it in an AWS Lambda? Only there the timeout occurs. I couldn‘t isolate a case with findOne, I can try to do it later. Let‘s see if you can reproduce this sample.

GeorgiMateev commented 3 years ago

Hi, @vkarpov15 , can you please let me know if the result you've got is from the sample deployed and running in AWS Lambda using the Serverless framework?

vkarpov15 commented 3 years ago

@GeorgiMateev I'm unable to repro, running your script in Lambda seems to work fine as well, for both cold starts:

image

And warm starts:

image

It looks like your mongo.service.ts file doesn't line up with our recommended pattern for connecting to MongoDB from Lambda - I'd recommend trying the docs' recommended pattern and seeing if that helps.