@@ -32,7 +32,6 @@ module.exports = function(RED) {
3232 var node = this ;
3333
3434 var cleanup = function ( p ) {
35- //console.log("CLEANUP!!!",p);
3635 node . activeProcesses [ p ] . kill ( ) ;
3736 node . status ( { fill :"red" , shape :"dot" , text :"timeout" } ) ;
3837 node . error ( "Exec node timeout" ) ;
@@ -53,37 +52,44 @@ module.exports = function(RED) {
5352 /* istanbul ignore else */
5453 if ( RED . settings . verbose ) { node . log ( cmd + " [" + arg + "]" ) ; }
5554 child = spawn ( cmd , arg ) ;
55+ var unknownCommand = ( child . pid === undefined ) ;
5656 if ( node . timer !== 0 ) {
5757 child . tout = setTimeout ( function ( ) { cleanup ( child . pid ) ; } , node . timer ) ;
5858 }
5959 node . activeProcesses [ child . pid ] = child ;
6060 child . stdout . on ( 'data' , function ( data ) {
61- //console.log('[exec] stdout: ' + data);
62- if ( isUtf8 ( data ) ) { msg . payload = data . toString ( ) ; }
63- else { msg . payload = data ; }
64- node . send ( [ RED . util . cloneMessage ( msg ) , null , null ] ) ;
61+ if ( node . activeProcesses . hasOwnProperty ( child . pid ) && node . activeProcesses [ child . pid ] !== null ) {
62+ // console.log('[exec] stdout: ' + data,child.pid);
63+ if ( isUtf8 ( data ) ) { msg . payload = data . toString ( ) ; }
64+ else { msg . payload = data ; }
65+ node . send ( [ RED . util . cloneMessage ( msg ) , null , null ] ) ;
66+ }
6567 } ) ;
6668 child . stderr . on ( 'data' , function ( data ) {
67- //console.log('[exec] stderr: ' + data);
68- if ( isUtf8 ( data ) ) { msg . payload = data . toString ( ) ; }
69- else { msg . payload = new Buffer ( data ) ; }
70- node . send ( [ null , RED . util . cloneMessage ( msg ) , null ] ) ;
69+ if ( node . activeProcesses . hasOwnProperty ( child . pid ) && node . activeProcesses [ child . pid ] !== null ) {
70+ if ( isUtf8 ( data ) ) { msg . payload = data . toString ( ) ; }
71+ else { msg . payload = new Buffer ( data ) ; }
72+ node . send ( [ null , RED . util . cloneMessage ( msg ) , null ] ) ;
73+ }
7174 } ) ;
7275 child . on ( 'close' , function ( code ) {
73- //console.log('[exec] result: ' + code);
74- delete node . activeProcesses [ child . pid ] ;
75- if ( child . tout ) { clearTimeout ( child . tout ) ; }
76- msg . payload = code ;
77- if ( code === 0 ) { node . status ( { } ) ; }
78- if ( code === null ) { node . status ( { fill :"red" , shape :"dot" , text :"timeout" } ) ; }
79- else if ( code < 0 ) { node . status ( { fill :"red" , shape :"dot" , text :"rc: " + code } ) ; }
80- else { node . status ( { fill :"yellow" , shape :"dot" , text :"rc: " + code } ) ; }
81- node . send ( [ null , null , RED . util . cloneMessage ( msg ) ] ) ;
76+ if ( unknownCommand || ( node . activeProcesses . hasOwnProperty ( child . pid ) && node . activeProcesses [ child . pid ] !== null ) ) {
77+ delete node . activeProcesses [ child . pid ] ;
78+ if ( child . tout ) { clearTimeout ( child . tout ) ; }
79+ msg . payload = code ;
80+ if ( code === 0 ) { node . status ( { } ) ; }
81+ if ( code === null ) { node . status ( { fill :"red" , shape :"dot" , text :"timeout" } ) ; }
82+ else if ( code < 0 ) { node . status ( { fill :"red" , shape :"dot" , text :"rc: " + code } ) ; }
83+ else { node . status ( { fill :"yellow" , shape :"dot" , text :"rc: " + code } ) ; }
84+ node . send ( [ null , null , RED . util . cloneMessage ( msg ) ] ) ;
85+ }
8286 } ) ;
8387 child . on ( 'error' , function ( code ) {
84- delete node . activeProcesses [ child . pid ] ;
8588 if ( child . tout ) { clearTimeout ( child . tout ) ; }
86- node . error ( code , RED . util . cloneMessage ( msg ) ) ;
89+ delete node . activeProcesses [ child . pid ] ;
90+ if ( node . activeProcesses . hasOwnProperty ( child . pid ) && node . activeProcesses [ child . pid ] !== null ) {
91+ node . error ( code , RED . util . cloneMessage ( msg ) ) ;
92+ }
8793 } ) ;
8894 }
8995 else {
@@ -120,7 +126,10 @@ module.exports = function(RED) {
120126 /* istanbul ignore else */
121127 if ( node . activeProcesses . hasOwnProperty ( pid ) ) {
122128 if ( node . activeProcesses [ pid ] . tout ) { clearTimeout ( node . activeProcesses [ pid ] . tout ) ; }
123- node . activeProcesses [ pid ] . kill ( ) ;
129+ // console.log("KILLLING",pid);
130+ var process = node . activeProcesses [ pid ] ;
131+ node . activeProcesses [ pid ] = null ;
132+ process . kill ( ) ;
124133 }
125134 }
126135 node . activeProcesses = { } ;
0 commit comments