@@ -73,20 +73,38 @@ var PythonShell = function (script, options) {
7373 errorData += '' + data ;
7474 } ) ;
7575
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+
7795 var err ;
78- if ( errorData || ( code && code !== 0 ) ) {
96+ if ( errorData || ( self . exitCode && self . exitCode !== 0 ) ) {
7997 if ( errorData ) {
8098 err = self . parseError ( errorData ) ;
8199 } else {
82- err = new Error ( 'process exited with code ' + code ) ;
100+ err = new Error ( 'process exited with code ' + self . exitCode ) ;
83101 }
84102 err = extend ( err , {
85103 executable : pythonPath ,
86104 options : pythonOptions . length ? pythonOptions : null ,
87105 script : self . script ,
88106 args : scriptArgs . length ? scriptArgs : null ,
89- exitCode : code
107+ exitCode : self . exitCode
90108 } ) ;
91109 // do not emit error if only a callback is used
92110 if ( self . listeners ( 'error' ) . length || ! self . _endCallback ) {
@@ -96,8 +114,8 @@ var PythonShell = function (script, options) {
96114
97115 self . terminated = true ;
98116 self . emit ( 'close' ) ;
99- self . _endCallback && self . _endCallback ( err , self . exitCode , signal ) ;
100- } ) ;
117+ self . _endCallback && self . _endCallback ( err , self . exitCode , self . exitSignal ) ;
118+ } ;
101119} ;
102120util . inherits ( PythonShell , EventEmitter ) ;
103121
0 commit comments