http.js:644 - Can't set headers after they are sent. #1265

Closed
artalat opened this Issue Aug 3, 2012 · 11 comments

7 participants

@artalat

Hi, I'm new to node.js and express so please bear with me. I've just started off with my first project and seems to have hit a road block with a way nowhere to be found.

The code works fine on local host but gives the following error on production server:

http.js:644
    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:644:11)
    at ServerResponse.res.setHeader (/home1/artalatc/public_html/cloud1/node_modules/express/node_modules/connect/lib/patch.js:59:22)
    at next (/home1/artalatc/public_html/cloud1/node_modules/express/node_modules/connect/lib/proto.js:153:13)
    at Function.app.handle (/home1/artalatc/public_html/cloud1/node_modules/express/node_modules/connect/lib/proto.js:198:3)
    at Server.app (/home1/artalatc/public_html/cloud1/node_modules/express/node_modules/connect/lib/connect.js:66:31)
    at Manager.handleRequest (/home1/artalatc/public_html/cloud1/node_modules/socket.io/lib/manager.js:564:28)
    at Server.<anonymous> (/home1/artalatc/public_html/cloud1/node_modules/socket.io/lib/manager.js:118:10)
    at Server.EventEmitter.emit (events.js:115:20)
    at HTTPParser.parser.onIncoming (http.js:1793:12)
    at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:111:23)

Here is my app.js and rest of the project files are auto generated.

var express = require('express')
  , routes = require('./routes')
  , http = require('http')
  , path = require('path');

var app = express();

app.configure(function(){
  app.set('port', process.env.PORT || 8000);
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.favicon());
  app.use(express.logger('dev'));
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);
  app.use(express.static(path.join(__dirname, 'public')));
});

app.configure('development', function(){
  app.use(express.errorHandler());
});

app.get('/', routes.index);

var server = app.listen(8000);
var io = require('socket.io').listen(server);

server.listen(app.get('port'), function(){
  console.log("Express server listening on port " + app.get('port'));
});

Also, the problem seems to be with the socket.io as commenting out the line var io = require('socket.io').listen(server); removes the error.

@tj
expressjs member
tj commented Aug 4, 2012

looks fine to me, @guille any ideas?

@rauchg

LGTM. Weird. Try using http.createServer(app)

@artalat

Already checked, no use. The server initially starts fine with the following message on console:

   info  - socket.io started
Express server listening on port 8000

But when the page is requested from the browser gives the following error and breaks down:

GET / 200 90ms - 170

http.js:644
    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:644:11)
    at ServerResponse.res.setHeader (/home1/artalatc/public_html/express/node_modules/express/node_modules/connect/lib/patch.js:59:22)
    at next (/home1/artalatc/public_html/express/node_modules/express/node_modules/connect/lib/proto.js:153:13)
    at Function.app.handle (/home1/artalatc/public_html/express/node_modules/express/node_modules/connect/lib/proto.js:198:3)
    at Server.app (/home1/artalatc/public_html/express/node_modules/express/node_modules/connect/lib/connect.js:66:31)
    at Manager.handleRequest (/home1/artalatc/public_html/express/node_modules/socket.io/lib/manager.js:564:28)
    at Server.<anonymous> (/home1/artalatc/public_html/express/node_modules/socket.io/lib/manager.js:118:10)
    at Server.EventEmitter.emit (events.js:115:20)
    at HTTPParser.parser.onIncoming (http.js:1793:12)
    at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:111:23)

Im not sure but is the get function being called twice?

@artalat

Without var io = require('socket.io').listen(server); it first shows this:

Express server listening on port 8000

And afterwards:

GET / 200 101ms - 170
GET /stylesheets/style.css 304 100ms
@tj
expressjs member
tj commented Aug 4, 2012

I would check out these lines:

at Manager.handleRequest (/home1/artalatc/public_html/express/node_modules/socket.io/lib/manager.js:564:28)
    at Server.<anonymous> (/home1/artalatc/public_html/express/node_modules/socket.io/lib/manager.js:118:10)

yeah somewhere setHeader is being called after a response is made

@artalat

hmm...based on what you said, i investigated manager.js, and commented out line 564 in the for loop:

this.oldListeners[i].call(this.server, req, res);

This solved the problem for now, but again, i dont know what impact it'll have on future code and so this cant be the permanent solution.

@parj

Shouldn't the server listening be - var socket = io.listen(http.createServer(app).listen(port)) - This is what I have in my minimal websocket code with expressjs 3.x and works fine - http://goo.gl/b7ncb and got it deployed to nodejitsu.com - http://goo.gl/Nk2mY

@dougvk

@parj it still happens with your code -- it's not the websockets it's expressjs app. add a app.get('/', function () {res.send(' ');}); to your server.js. I don't know if this helps, but when I modify my package.json to have node 0.9.8 installed instead of 0.9.9, then the error stops showing up.

see this code for more examples of the problem: https://github.com/dougvk/doubling-problem

@tj
expressjs member
tj commented Sep 1, 2012

i dont think this is an express issue, closing until we have more info.. it should be pretty obvious if you follow the stack trace

@tj tj closed this Sep 1, 2012
@englercj

Any idea when a fix for this issue with be in the works?

@audreyt

This seems to be a socket.io issue, and is reliably reproducible on Node.js 0.9.3.

A fix is available here: socketio/socket.io#1080

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment