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

Can't set headers after they are sent. #17

Closed
jwerre opened this issue Jul 7, 2015 · 1 comment
Closed

Can't set headers after they are sent. #17

jwerre opened this issue Jul 7, 2015 · 1 comment
Assignees
Labels

Comments

@jwerre
Copy link

jwerre commented Jul 7, 2015

Pardon me if this has been asked a million time but It's not clear how to best handle timeouts. The following script will crash the server since res.send is called after the timeout. I could wrap res.send in an if !res.headersSent but it seems a little excessive to do that for every route. I'm not sure how to best handle this situation.

var express = require('express');
var timeout = require('connect-timeout');

var handleTimeout = function (req, res, next) {
    if (!req.timedout) next();
};

var app = express()
    .use(timeout(500))
    .use(handleTimeout)
    .get("/", function (req, res, next) {
        setTimeout(function () {
            // This causes "Error: Can't set headers after they are sent."
            res.send({peaches:"yummy", turnips:"yucky"}); 
        }, 1000);
    })
    .use(handleTimeout)
    .listen(8888, function(){
            console.log('Server started on port 8888');
        });

Error:

Starting child process with 'node timeout.js'
Server started on port 8888
ServiceUnavailableError: Response timeout
    at IncomingMessage.<anonymous> (/Users/jw/Desktop/timeout/node_modules/connect-timeout/index.js:69:8)
    at IncomingMessage.emit (events.js:95:17)
    at null._onTimeout (/Users/jw/Desktop/timeout/node_modules/connect-timeout/index.js:41:11)
    at Timer.listOnTimeout [as ontimeout] (timers.js:112:15)

http.js:689
    throw new Error('Can\'t set headers after they are sent.');
          ^
Error: Can't set headers after they are sent.
    at ServerResponse.OutgoingMessage.setHeader (http.js:689:11)
    at ServerResponse.header (/Users/jw/Desktop/timeout/node_modules/express/lib/response.js:718:10)
    at ServerResponse.send (/Users/jw/Desktop/timeout/node_modules/express/lib/response.js:163:12)
    at ServerResponse.json (/Users/jw/Desktop/timeout/node_modules/express/lib/response.js:249:15)
    at ServerResponse.send (/Users/jw/Desktop/timeout/node_modules/express/lib/response.js:151:21)
    at null._onTimeout (timeout/timeout.js:13:17)
    at Timer.listOnTimeout [as ontimeout] (timers.js:112:15)
Program node timeout.js exited with code 8
@dougwilson dougwilson self-assigned this Jul 7, 2015
@dougwilson
Copy link
Contributor

I could wrap res.send in an if !res.headersSent but it seems a little excessive to do that for every route. I'm not sure how to best handle this situation.

There are two solutions:

  1. You can do as you suggest: check if the request timed out once your timer fired.
  2. When you set your timeout, also add a listener on the timeout event and then clear the timeout so your timer never even fires:
    .get("/", function (req, res, next) {
        var timer = setTimeout(function () {
            res.send({peaches:"yummy", turnips:"yucky"}); 
        }, 1000);
        req.once('timeout', function () {
          clearTimeout(timer);
        });
    })

@expressjs expressjs deleted a comment from ani-naslyan Sep 5, 2018
@expressjs expressjs deleted a comment from phuongvu0203 Sep 5, 2018
@expressjs expressjs deleted a comment from hakkisabah Sep 5, 2018
@expressjs expressjs deleted a comment from justswim Sep 5, 2018
@expressjs expressjs deleted a comment from sdiptapaul Sep 5, 2018
@expressjs expressjs deleted a comment from romanesko Sep 5, 2018
@expressjs expressjs locked as resolved and limited conversation to collaborators Sep 5, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants