http-server icon indicating copy to clipboard operation
http-server copied to clipboard

Server Crashing with "Cannot set headers after they are sent to the client"

Open Inrixia opened this issue 5 years ago • 11 comments

Steps to reproduce the issue, if applicable. Include the actual command and output and/or stack trace.

Not sure sorry as there is a lot of traffic on the server, just appears to all be normal GET requests.

What did you expect to happen? The server to not crash. Ideally the error would be returned as a 503.

_http_outgoing.js:485
    throw new ERR_HTTP_HEADERS_SENT('set');
    ^

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at ServerResponse.setHeader (_http_outgoing.js:485:11)
    at module.exports.ResponseStream.<computed> [as setHeader] (C:\Users\Administrator\AppData\Roaming\npm\node_modules\http-server\node_modules\union\lib\response-stream.js:100:34)
    at Object.exports.<computed> (C:\Users\Administrator\AppData\Roaming\npm\node_modules\http-server\node_modules\ecstatic\lib\ecstatic\status-handlers.js:57:7)
    at ReadStream.<anonymous> (C:\Users\Administrator\AppData\Roaming\npm\node_modules\http-server\node_modules\ecstatic\lib\ecstatic.js:298:24)
    at ReadStream.emit (events.js:223:5)
    at internal/fs/streams.js:167:12
    at FSReqCallback.wrapper [as oncomplete] (fs.js:470:5) {
  code: 'ERR_HTTP_HEADERS_SENT'
}

Tell us about your environment Windows Server 2016 Node.js v12.14.1 Latest Version (Embedded/Global CLI Version from NPM)

Other information (related issues, suggestions for a fix, etc): As far as I am aware the only thing that really needs to be done is catching the error and returning 503.

Inrixia avatar Jun 07 '20 00:06 Inrixia

Currently I am just using a catch-all error handler and running off the source code rather than the global package. So just having a handler for that error would be a quick fix.

Inrixia avatar Jun 10 '20 13:06 Inrixia

This only happens with known file extensions, in my case txt, json, etc. Not sure what headers are being sent early, but without looking at the source, I'm sure it's trying to set the mime-type header, which it fails to do.

thevictor13 avatar Dec 21 '21 05:12 thevictor13

In my case it was happening for .json files, easy solution was to rollback to version 13.0.2

gcandrade10 avatar Feb 10 '22 23:02 gcandrade10

Same issue. Rolling back to 13.0.2 worked

shrimpwagon avatar Mar 01 '22 16:03 shrimpwagon

This is kind of a duplicate of #756 , in both cases the headers error hides some problem deeper in the code. Proposed improvement in #791.

@shrimpwagon since you do not experience this with older versions of http-server, I think you might have the problem described in #756 and may be able to solve it by updating Node to 12.16 or later.

@thornjad maybe you can close this issue as duplicate, also please check the PR #791 if you have time.

zbynek avatar Mar 04 '22 22:03 zbynek

Same issue. Rolling back to 13.0.2 worked

It works for me too. Thanks bro.

brizer avatar May 09 '22 07:05 brizer

I confirm, that the installation of v13 .. 13.1.0 works. I tried to run this repository with http-server https://github.com/pixijs/examples (as they recommend in readme)

jirkadev avatar Aug 17 '22 16:08 jirkadev

@jirikrepl is it also OK if you keep the latest http-server and update node to the latest 12.X, 14.X or 16.X?

zbynek avatar Aug 17 '22 16:08 zbynek

@zbynek thanks! it was crashing for node 10, version http-server v14 works fine with node 16

jirkadev avatar Aug 17 '22 16:08 jirkadev

Same issue. Rolling back to 13.0.2 worked. Thanks!

yutingkuang avatar Sep 08 '22 01:09 yutingkuang

Working with 16 as well! Thanks!

ccdaniele avatar Oct 12 '22 00:10 ccdaniele