@@ -73,20 +73,38 @@ var PythonShell = function (script, options) {
73
73
errorData += '' + data ;
74
74
} ) ;
75
75
76
- this . childProcess . on ( 'exit' , function ( code , signal ) {
76
+ this . stderr . on ( 'end' , function ( ) {
77
+ self . stderrHasEnded = true
78
+ terminateIfNeeded ( ) ;
79
+ } )
80
+
81
+ this . stdout . on ( 'end' , function ( ) {
82
+ self . stdoutHasEnded = true
83
+ terminateIfNeeded ( ) ;
84
+ } )
85
+
86
+ this . childProcess . on ( 'exit' , function ( code , signal ) {
87
+ self . exitCode = code ;
88
+ self . exitSignal = signal ;
89
+ terminateIfNeeded ( ) ;
90
+ } ) ;
91
+
92
+ function terminateIfNeeded ( ) {
93
+ if ( ! self . stderrHasEnded || ! self . stdoutHasEnded || ( self . exitCode == null && self . exitSignal == null ) ) return ;
94
+
77
95
var err ;
78
- if ( errorData || ( code && code !== 0 ) ) {
96
+ if ( errorData || ( self . exitCode && self . exitCode !== 0 ) ) {
79
97
if ( errorData ) {
80
98
err = self . parseError ( errorData ) ;
81
99
} else {
82
- err = new Error ( 'process exited with code ' + code ) ;
100
+ err = new Error ( 'process exited with code ' + self . exitCode ) ;
83
101
}
84
102
err = extend ( err , {
85
103
executable : pythonPath ,
86
104
options : pythonOptions . length ? pythonOptions : null ,
87
105
script : self . script ,
88
106
args : scriptArgs . length ? scriptArgs : null ,
89
- exitCode : code
107
+ exitCode : self . exitCode
90
108
} ) ;
91
109
// do not emit error if only a callback is used
92
110
if ( self . listeners ( 'error' ) . length || ! self . _endCallback ) {
@@ -96,8 +114,8 @@ var PythonShell = function (script, options) {
96
114
97
115
self . terminated = true ;
98
116
self . emit ( 'close' ) ;
99
- self . _endCallback && self . _endCallback ( err , self . exitCode , signal ) ;
100
- } ) ;
117
+ self . _endCallback && self . _endCallback ( err , self . exitCode , self . exitSignal ) ;
118
+ } ;
101
119
} ;
102
120
util . inherits ( PythonShell , EventEmitter ) ;
103
121
0 commit comments