http.get no longer works after fork (daemonized)

18 views
Skip to first unread message

Cory Mintz

unread,
Feb 10, 2012, 9:21:38 PM2/10/12
to nodejs
I have been using the node v0.4.X branch in production along with
daemon.node (https://github.com/indexzero/daemon.node) to run node
apps from init scripts for a few months now. I recently tried to
upgrade to v0.6.10 and noticed that my application would run but
didn't appear to be doing anything. After digging in, I narrowed it
down to http.get() specifically. After get() was called none of the
callbacks are ever called. I reduced it down to a pretty straight
forward example:

var daemon = require('daemon');
var fs = require('fs');
var http = require('http');

fs.open('test.log', 'w+', function (err, fd) {

daemon.start(fd);

var options = {
host: 'www.google.com',
port: 80,
path: '/index.html'
};

console.log('Making request');
http.get(options, function(res) {
console.log('Got response: ' + res.statusCode);
}).on('error', function(e) {
console.log('Got error: ' + e.message);
});

});


When this code is run, "Making request" is written to test.log, but
neither the callback or the error event are ever called. The
application will continue to run forever though and has to be killed
manually. While it's possible that it is daemon.node's fault, I feel
like is unlikely since all it does is fork the process and redirect
the standard streams. And 'Making request' is written to the log,
which is after the fork.

I am testing on CentOS 5.3

Has anyone else experienced problems after forking a node
application?

Ben Noordhuis

unread,
Feb 11, 2012, 6:23:41 PM2/11/12
to nod...@googlegroups.com

Node is not fork-safe (something that's been pointed out to the
daemon.node maintainers).

Your example may work if you fork before calling fs.open() but there
are no guarantees.

Cory Mintz

unread,
Feb 11, 2012, 10:11:25 PM2/11/12
to nodejs
Thanks. I did not know that. daemon.start(fs.openSync()) seems to work
as well, but of course something guaranteed would be nicer. Is there a
supported method of being able to start a node application from the
init process?


On Feb 11, 6:23 pm, Ben Noordhuis <i...@bnoordhuis.nl> wrote:
Reply all
Reply to author
Forward
0 new messages