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

Server crash when client disconnect: "Can\'t set headers after they are sent." #1

Open
sylvain101010 opened this issue Nov 5, 2017 · 2 comments

Comments

@sylvain101010
Copy link

sylvain101010 commented Nov 5, 2017

Hi,
When a client disconnect, the server crash with the following error:

Error: Can't set headers after they are sent.

here is a stacktrace

server listening on 3000
_http_outgoing.js:504
throw new Error('Can't set headers after they are sent.');
^
Error: Can't set headers after they are sent.
at validateHeader (_http_outgoing.js:504:11)
at ServerResponse.setHeader (_http_outgoing.js:511:3)
at Immediate.wrapReplyEnd [as _onImmediate] (/Users/test/fastifyy/node_modules/fastify/lib/reply.js:145:15)
at runCallback (timers.js:785:20)
at tryOnImmediate (timers.js:743:5)
at processImmediate [as _immediateCallback] (timers.js:714:5)

How to reproduce:

just run example.js
node example.js
then
curl localhost:3000/sse2
And quit the running curl job
Ctrl+c

@YerkoPalma
Copy link

I just had this very same issue. You must ensure to call reply.sse() (with no arguments) before closing your server. This works for me

fastify.addHook('onClose', (instance, done) => {
  instance._Reply.sse()
  done()
})

try adding that hook, then running your server and stop it with Ctrl + c

@piranna
Copy link
Contributor

piranna commented Jan 13, 2019

He is talking about when clients disconnect, that's the same problem I have, NOT when server close...

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

No branches or pull requests

3 participants