Skip to content

Instantly share code, notes, and snippets.

@aleafs
Created August 31, 2012 03:52
Show Gist options
  • Save aleafs/3548894 to your computer and use it in GitHub Desktop.
Save aleafs/3548894 to your computer and use it in GitHub Desktop.
node crash when send handle to child
/* vim: set expandtab tabstop=2 shiftwidth=2 foldmethod=marker: */
"use strict";
var listen = function (port, onconnection) {
var res = new (process.binding('tcp_wrap').TCP);
res.onconnection = onconnection;
res.bind('0.0.0.0', port);
res.listen(128);
return res;
};
var _master = (process.argv.length > 2 && 'worker' == process.argv[2]) ? false : true;
if (_master) {
var child = require('child_process').fork(__filename, ['worker']);
listen(3126, function (handle) {
console.log('Got connection');
handle.close();
//process.nextTick(function () {
child.send('request', handle);
//});
//handle.close();
//handle = null;
});
} else {
var _http = require('http').createServer(function (req, res) {
res.end('Hello world');
});
process.on('message', function (msg, handle) {
if (!handle) {
return;
}
var socket = new (require('net').Socket)({
'handle' : handle,
});
socket.readable = true;
socket.writable = true;
socket.resume();
socket.emit('connect');
_http.emit('connection', socket);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment