http-party / http-server

a simple zero-configuration command-line http server
MIT License
13.66k stars 1.5k forks source link

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

Open Inrixia opened 4 years ago

Inrixia commented 4 years ago

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 commented 4 years ago

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.

thevictor13 commented 2 years ago

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.

gcandrade10 commented 2 years ago

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

shrimpwagon commented 2 years ago

Same issue. Rolling back to 13.0.2 worked

zbynek commented 2 years ago

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.

jirikrepl commented 2 years ago

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)

zbynek commented 2 years ago

@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?

jirikrepl commented 2 years ago

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

yutingkuang commented 2 years ago

Same issue. Rolling back to 13.0.2 worked. Thanks!

ccdaniele commented 2 years ago

Working with 16 as well! Thanks!