@@ -62,14 +62,23 @@ exports.execFile = function (file /* args, options, callback */) {
62
62
var stderr = "" ;
63
63
var killed = false ;
64
64
65
+ function kill ( ) {
66
+ if ( killed ) return ;
67
+ try {
68
+ child . kill ( options . killSignal ) ;
69
+ } catch ( err ) {
70
+ if ( "No such process" !== err . message ) {
71
+ throw err ;
72
+ }
73
+ }
74
+ killed = true ;
75
+ }
76
+
65
77
var timeoutId ;
66
78
if ( options . timeout > 0 ) {
67
79
timeoutId = setTimeout ( function ( ) {
68
- if ( ! killed ) {
69
- child . kill ( options . killSignal ) ;
70
- killed = true ;
71
- timeoutId = null ;
72
- }
80
+ kill ( ) ;
81
+ timeoutId = null ;
73
82
} , options . timeout ) ;
74
83
}
75
84
@@ -78,30 +87,29 @@ exports.execFile = function (file /* args, options, callback */) {
78
87
79
88
child . stdout . addListener ( "data" , function ( chunk ) {
80
89
stdout += chunk ;
81
- if ( ! killed && stdout . length > options . maxBuffer ) {
82
- child . kill ( options . killSignal ) ;
83
- killed = true ;
90
+ if ( stdout . length > options . maxBuffer ) {
91
+ kill ( ) ;
84
92
}
85
93
} ) ;
86
94
87
95
child . stderr . addListener ( "data" , function ( chunk ) {
88
96
stderr += chunk ;
89
- if ( ! killed && stderr . length > options . maxBuffer ) {
90
- child . kill ( options . killSignal ) ;
91
- killed = true ;
97
+ if ( stderr . length > options . maxBuffer ) {
98
+ kill ( ) ;
92
99
}
93
100
} ) ;
94
101
95
102
child . addListener ( "exit" , function ( code , signal ) {
96
103
if ( timeoutId ) clearTimeout ( timeoutId ) ;
104
+ if ( ! callback ) return ;
97
105
if ( code === 0 && signal === null ) {
98
- if ( callback ) callback ( null , stdout , stderr ) ;
106
+ callback ( null , stdout , stderr ) ;
99
107
} else {
100
108
var e = new Error ( "Command failed: " + stderr ) ;
101
109
e . killed = killed ;
102
110
e . code = code ;
103
111
e . signal = signal ;
104
- if ( callback ) callback ( e , stdout , stderr ) ;
112
+ callback ( e , stdout , stderr ) ;
105
113
}
106
114
} ) ;
107
115
0 commit comments