-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Avoid error Uncaught Error: Can't set headers after they are sent. #867
Comments
@abarre could you show me the full code where this happens? This shouldn't happen to you as the consumer but I'm guessing you are writing your own headers in a non error case? |
My error handler is something similar to the example below : proxy.on('error', function (err, req, res, target) {
if (err.message.indexOf("ETIMEDOUT") !== -1) {
res.writeHead(globals.TIMEOUT_ERROR, "socket hang up);
res.end();
}
else.... (for other error case)
}; A response is returned to the client in the error handler. |
@abarre so this happens in a case where both |
If so your fix seems like it would be correct. I just want to establish why this is happening exactly :). |
Hum, I don't remember well, I think that an |
I am also experiencing very similar issue, but on lines 112-115. In my specific case headers are sent immediately during http.request creation. In this case it would be more useful to add an event (hopefully a synchronous one) at the end of common.setupOutgoing(), so that it would be possible to modify options instead of the request. |
having smth similar
It started to happen only http-proxy introduced. Would be nice to have web-outgoing.js functions wrapped in try catches with some error events emitted |
I forked this project into https://github.com/HBOCodeLabs/node-http-proxy and created a synchronous callback hook like @giuliopaci described. Here is the PR: #1091 |
In this portion of the code : https://github.com/nodejitsu/node-http-proxy/blob/master/lib/http-proxy/passes/web-incoming.js#L148...L157, there is no verification if the headers of the response have already been sent to the client.
It happens in my case when an error is fired and after I return an error 500 to the client.
Fix :
This portion of code can be wrapped by
if(!res.finished){}
to prevent the exceptionUncaught Error: Can't set headers after they are sent.
.The text was updated successfully, but these errors were encountered: