close server after context succeed - #103
Conversation
|
I have to withdraw this solution. While it works great in the serverless offline environment it fails in AWS lambda. I think I was under a misconception of how AWS lambda works. Looks like a lambda function service serves more than a single request. |
|
|
||
| context.succeed(successResponse) | ||
| context.succeed(successResponse); | ||
| if (server && server.close) { |
There was a problem hiding this comment.
I realize this is closed, but I am dealing with the same. Thoughts using something like:
if(process.env.IS_OFFLINE) { .... }
|
This is a great suggestion. If you or OP want to submit PR with this plus tests, I'll merge it in. Otherwise I'll tackle it when I can. |
|
Thx :) I will take a stab at the PR + tests when I get a moment this weekend. I have monkey patched locally for now 😞 |
|
@scalebig ℹ️ I applied the monkey patch you suggested and while it works locally, with it applied in an AWS context, I'm intermittently seeing the following errors in my CloudWatch logs as well as |
|
I had the same problem described here when running aws-serverless-express with node-lambda so implemented this same PR together with a 'local' flag defined in node-lambda when running locally (motdotla/node-lambda#405) I guess the same flag could also be added to serverless-offline and similar tools. |
|
Here is my workaround I call this function before function monkeyPatchAwsServerlessExpress(){
// awsServerlessExpress keeps warning when using it on localhost
// we will patch it in order to stop it from polluting the console
// here is what this patch is hiding from you on each request :
// WARNING: Attempting to listen on socket /tmp/server0.sock, but it is already in use.
// This is likely as a result of a previous invocation error or timeout.
// Check the logs for the invocation(s) immediately prior to this for root cause, and consider increasing the timeout and/or cpu/memory allocation if this is purely as a result of a timeout.
// aws-serverless-express will restart the Node.js server listening on a new port and continue with this request.
// when using with serverless-offline
// junk files inside tmp folder has to be cleaned up
if( process.env.IS_OFFLINE){
let fs = require('fs')
let createServer = awsServerlessExpress.createServer
awsServerlessExpress.createServer = (server)=>{
let awsExpressServer = createServer( server )
let tmp_path = awsServerlessExpress.getSocketPath(awsExpressServer._socketPathSuffix)
if( fs.existsSync(tmp_path) ) { fs.unlinkSync( tmp_path ) }
return awsExpressServer
}
}
} |
This PR is in response to #91 an issue that our organization experiences as well.
In our case, we are using serverless framework with the "serverless-offline" plugin that spins up the service in our local environment and serves multiple requests. I think the expectation in lambda is a single request being serviced per process but in some local dev configurations a single process might be servicing multiple requests. For example we do local dredd API testing which starts serverless offline and calls all of our REST endpoints before shutting down serverless.
To recreate the issue:
Serverless config
Serverless handler
Test your service locally:
Hit your endpoint multiple times:
Observe warnings:
With the change the PR applies, we simply call close on server after
context.succeedis called.Re-run local test and hit the endpoint multiple times and observe the warning is no longer present.