Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error [ERR_STREAM_WRITE_AFTER_END]: write after end #120

Closed
designbuedchen opened this issue Feb 12, 2019 · 12 comments · Fixed by #121
Closed

Error [ERR_STREAM_WRITE_AFTER_END]: write after end #120

designbuedchen opened this issue Feb 12, 2019 · 12 comments · Fixed by #121

Comments

@designbuedchen
Copy link

Hi guys!

After entering

yarn netlify-lambda serve src/lambda

in the console and hitting e.g. http://localhost:9000/.netlify/functions/hello everything runs well and the console returns

Request from ::1: GET /.netlify/functions/hello
Response with status 400 in 25 ms.

but after a few seconds the localhost server stops and the console returns

Your lambda function took longer than 10 seconds to finish.
If you need a longer execution time, you can increase the timeout using the -t or --timeout flag.
Please note that default function invocation is 10 seconds, check our documentation for more > information (https://www.netlify.com/docs/functions/#custom-deployment-options).

events.js:173
throw er; // Unhandled 'error' event
^

Error [ERR_STREAM_WRITE_AFTER_END]: write after end
at write_ (_http_outgoing.js:584:17)
at ServerResponse.write (_http_outgoing.js:579:10)
at handleInvocationTimeout (/Git/mylocalrepository/node_modules/netlify-lambda/lib/serve.js:19:12)
at Timeout._onTimeout (/Git/mylocalrepository/node_modules/netlify-lambda/lib/serve.js:141:9)
at listOnTimeout (timers.js:327:15)
at processTimers (timers.js:271:5)
Emitted 'error' event at:
at writeAfterEndNT (_http_outgoing.js:646:7)
at processTicksAndRejections (internal/process/next_tick.js:76:17)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.`

The function behind hello is the standard example from this repository (async/await version).

Any ideas?

@alexanderson1993
Copy link
Contributor

I too am experiencing this issue, with a very simple function:

exports.handler = async function(event, context) {
  return {
    statusCode: 200,
    body: "Hello, World"
  };
};

It has the same behavior with the callback parameter as well.

I believe this is related to this PR: #116

@alexanderson1993
Copy link
Contributor

Digging in a bit, I think the issue is here: https://github.com/netlify/netlify-lambda/pull/116/files#diff-3288939317efd62bfc509440d662cacaR138

The Promise.race resolves, but never cancels the setTimeout call, so it still crashes even if the promise resolved properly.

I'll see about putting in a PR.

@swyxio
Copy link
Contributor

swyxio commented Feb 13, 2019

yikes, my bad. happy to take pr or if not i’ll fix ina couple hours

mean while feel free to use v1.3.x for the time being

@alexanderson1993
Copy link
Contributor

@sw-yx It's all good, I've already put in a PR. 👍

@swyxio
Copy link
Contributor

swyxio commented Feb 13, 2019

https://github.com/netlify/netlify-lambda/releases/tag/v1.4.1

@designbuedchen
Copy link
Author

Amazing, thanks for the super fast fix!!!!!!!

@Aratramba
Copy link

Aratramba commented Feb 13, 2019

I'm still experiencing the same issue (server quits after x seconds) with the new release (1.4.1) with the hello world example.

Response with status 200 in 6 ms.
TypeError: Promise.race(...).finally is not a function
    at …/node_modules/netlify-lambda/lib/serve.js:150:15
    at Layer.handle [as handle_request] (…/node_modules/express/lib/router/layer.js:95:5)
    at next (…/node_modules/express/lib/router/route.js:137:13)
    at next (…/node_modules/express/lib/router/route.js:131:14)
    at next (…/node_modules/express/lib/router/route.js:131:14)
    at next (…/node_modules/express/lib/router/route.js:131:14)
    at next (…/node_modules/express/lib/router/route.js:131:14)
    at next (…/node_modules/express/lib/router/route.js:131:14)
    at next (…/node_modules/express/lib/router/route.js:131:14)
    at Route.dispatch (…/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (…/node_modules/express/lib/router/layer.js:95:5)
    at …/node_modules/express/lib/router/index.js:281:22
    at param (…/node_modules/express/lib/router/index.js:354:14)
    at param (…/node_modules/express/lib/router/index.js:365:14)
    at Function.process_params (…/node_modules/express/lib/router/index.js:410:3)
    at next (…/node_modules/express/lib/router/index.js:275:10)
Your lambda function took longer than 10 seconds to finish.
If you need a longer execution time, you can increase the timeout using the -t or --timeout flag.
Please note that default function invocation is 10 seconds, check our documentation for more information (https://www.netlify.com/docs/functions/#custom-deployment-options).

events.js:183
      throw er; // Unhandled 'error' event
      ^

Error: write after end
    at write_ (_http_outgoing.js:622:15)
    at ServerResponse.write (_http_outgoing.js:617:10)
    at handleInvocationTimeout (…/node_modules/netlify-lambda/lib/serve.js:20:12)
    at Timeout._onTimeout (…/node_modules/netlify-lambda/lib/serve.js:146:11)
    at ontimeout (timers.js:498:11)
    at tryOnTimeout (timers.js:323:5)
    at Timer.listOnTimeout (timers.js:290:5)

@swyxio swyxio reopened this Feb 13, 2019
@swyxio
Copy link
Contributor

swyxio commented Feb 13, 2019

Hi @Aratramba, thats not good. what node version are you running? i realized promise.finally is only supported in node 10. we may need to rewrite to avoid using promise.finally.

i checked using https://github.com/netlify/create-react-app-lambda and i'm on node 10 and it works fine.

@swyxio
Copy link
Contributor

swyxio commented Feb 13, 2019

got rid of promise.finally with https://github.com/netlify/netlify-lambda/releases/tag/v1.4.2 cc @Aratramba

@swyxio swyxio closed this as completed Feb 13, 2019
@Aratramba
Copy link

you're right, I am on an older Node version 8.11.2. Both switching to Node 10 and updating to 1.4.2 solve this problem. Thanks!

@n1c01a5
Copy link

n1c01a5 commented Jun 17, 2019

I have this issue wit node v11 and netlify 1.4.2.

@abtx
Copy link

abtx commented Mar 17, 2020

I've got the same issue, every now and then the app crashes with 'write after end' error. I've got simple I/O functions on my Lamdas as well.

Screenshot 2020-03-17 at 13.17.22.pdf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants