11import * as cp from 'child_process' ;
2- import { PythonEnvironment , PythonBackgroundRunOptions , PythonProcess } from '../../api' ;
2+ import { PythonBackgroundRunOptions , PythonEnvironment , PythonProcess } from '../../api' ;
3+ import { traceError , traceInfo , traceWarn } from '../../common/logging' ;
4+ import { quoteStringIfNecessary } from './execUtils' ;
35
46export async function runInBackground (
57 environment : PythonEnvironment ,
68 options : PythonBackgroundRunOptions ,
79) : Promise < PythonProcess > {
8- const executable =
9- environment . execInfo ?. activatedRun ?. executable ?? environment . execInfo ?. run . executable ?? 'python' ;
10+ let executable = environment . execInfo ?. activatedRun ?. executable ?? environment . execInfo ?. run . executable ;
11+ if ( ! executable ) {
12+ traceWarn ( 'No Python executable found in environment; falling back to "python".' ) ;
13+ executable = 'python' ;
14+ }
15+ // Check and quote the executable path if necessary
16+ executable = quoteStringIfNecessary ( executable ) ;
1017 const args = environment . execInfo ?. activatedRun ?. args ?? environment . execInfo ?. run . args ?? [ ] ;
1118 const allArgs = [ ...args , ...options . args ] ;
19+ traceInfo ( `Running in background: ${ executable } ${ allArgs . join ( ' ' ) } ` ) ;
1220
1321 const proc = cp . spawn ( executable , allArgs , { stdio : 'pipe' , cwd : options . cwd , env : options . env } ) ;
1422
@@ -22,8 +30,17 @@ export async function runInBackground(
2230 proc . kill ( ) ;
2331 }
2432 } ,
25- onExit : ( listener : ( code : number | null , signal : NodeJS . Signals | null ) => void ) => {
26- proc . on ( 'exit' , listener ) ;
33+ onExit : ( listener : ( code : number | null , signal : NodeJS . Signals | null , error ?: Error | null ) => void ) => {
34+ proc . on ( 'exit' , ( code , signal ) => {
35+ if ( code && code !== 0 ) {
36+ traceError ( `Process exited with error code: ${ code } , signal: ${ signal } ` ) ;
37+ }
38+ listener ( code , signal , null ) ;
39+ } ) ;
40+ proc . on ( 'error' , ( error ) => {
41+ traceError ( `Process error: ${ error ?. message || error } ${ error ?. stack ? '\n' + error . stack : '' } ` ) ;
42+ listener ( null , null , error ) ;
43+ } ) ;
2744 } ,
2845 } ;
2946}
0 commit comments