node.js - Kill fork on windows throws "write EPIPE" -
i kill forks after specific amount of time. however, in codebase, following error (only on windows) :
events.js:85 throw er; // unhandled 'error' event ^ error: write epipe @ exports._errnoexception (util.js:746:11) @ childprocess.target._send (child_process.js:484:28) @ childprocess.target.send (child_process.js:416:12) @ sendhelper (cluster.js:676:8) @ send (cluster.js:512:5) @ cluster.js:488:7 @ sharedhandle.add (cluster.js:99:3) @ queryserver (cluster.js:480:12) @ worker.onmessage (cluster.js:438:7) @ childprocess.<anonymous> (cluster.js:692:8)
this error seems happen whenever worker not yet started , killed (eg takes 1 second start , killed before having started)
here's minimal example can reproduce.
var cluster = require('cluster'); var http = require('http'); var numcpus = require('os').cpus().length; var workers=[]; if (cluster.ismaster) { // fork workers. (var = 0; < numcpus; i++) { workers[i] = cluster.fork(); console.log('forking'); } cluster.on('exit', function(worker, code, signal) { console.log('worker ' + worker.process.pid + ' died'); }); settimeout(function(){ workers.foreach(function(worker){ worker.kill(); }) },1) } else { // workers can share tcp connection // in case http server http.createserver(function(req, res) { res.writehead(200); res.end("hello world\n"); }).listen(8000); }
if change http.createserver
console.log
, don't have problem, suspect because worker hasn't finished "starting".
strangely enough, assertionerror
instead (they doesn't seem kind of pattern, have had 10s in row of same error, toggles between 2 errors : seems random between epipe , assertion error).
assert.js:86 throw new assert.assertionerror({ ^ assertionerror: resource leak detected. @ removeworker (cluster.js:346:9) @ childprocess.<anonymous> (cluster.js:366:34) @ childprocess.g (events.js:199:16) @ childprocess.emit (events.js:110:17) @ process.childprocess._handle.onexit (child_process.js:1074:12)
the reason error daemon not yet listening when send sigterm signal:
the solution wait listening
event before killing fork.
var cluster = require('cluster'); var http = require('http'); var numcpus = require('os').cpus().length; var workers=[]; if (cluster.ismaster) { // fork workers. (var = 0; < numcpus; i++) { workers[i] = cluster.fork(); console.log('forking'); } cluster.on('listening', function(worker, code, signal) { settimeout(function(){ worker.kill(); },1) }); cluster.on('exit', function(worker, code, signal) { console.log('worker ' + worker.process.pid + ' died'); }); } else { // workers can share tcp connection // in case http server http.createserver(function(req, res) { res.writehead(200); res.end("hello world\n"); }).listen(8000); }
Comments
Post a Comment