@@ -105,11 +105,17 @@ export class UnityHub {
105105 const tasksCompleteMessage = 'All Tasks Completed Successfully.' ;
106106 const child = spawn ( executable , execArgs , {
107107 stdio : [ 'ignore' , 'pipe' , 'pipe' ] ,
108+ detached : process . platform !== 'win32'
108109 } ) ;
109110 const sigintHandler = ( ) => child . kill ( 'SIGINT' ) ;
110111 const sigtermHandler = ( ) => child . kill ( 'SIGTERM' ) ;
111112 process . once ( 'SIGINT' , sigintHandler ) ;
112113 process . once ( 'SIGTERM' , sigtermHandler ) ;
114+ function removeListeners ( ) {
115+ process . removeListener ( 'SIGINT' , sigintHandler ) ;
116+ process . removeListener ( 'SIGTERM' , sigtermHandler ) ;
117+ }
118+ let forceCloseTimeout : NodeJS . Timeout | undefined ;
113119 function processOutput ( data : Buffer ) {
114120 try {
115121 const chunk = data . toString ( ) ;
@@ -138,8 +144,21 @@ export class UnityHub {
138144 if ( child ?. pid ) {
139145 Logger . instance . debug ( `Unity Hub reported all tasks completed, terminating process...` ) ;
140146 const childProcInfo = { pid : child . pid , name : child . spawnfile , ppid : process . pid } ;
141- KillChildProcesses ( childProcInfo ) ;
142- TryKillProcess ( childProcInfo ) ;
147+ KillChildProcesses ( childProcInfo ) . then ( async ( ) => {
148+ const killedPid = await TryKillProcess ( childProcInfo , 'SIGTERM' ) ;
149+
150+ if ( ! killedPid ) {
151+ Logger . instance . error ( `Failed to terminate Unity Hub process!` ) ;
152+ }
153+
154+ // In case the process doesn't close itself, force kill after 5 seconds
155+ forceCloseTimeout = setTimeout ( ( ) => {
156+ Logger . instance . info ( `Force closing Unity Hub process after timeout...` ) ;
157+ TryKillProcess ( childProcInfo , 'SIGKILL' ) ;
158+ removeListeners ( ) ;
159+ resolve ( 0 ) ;
160+ } , 5000 ) ;
161+ } ) ;
143162 }
144163 }
145164 } catch ( error : any ) {
@@ -151,13 +170,19 @@ export class UnityHub {
151170 child . stdout . on ( 'data' , processOutput ) ;
152171 child . stderr . on ( 'data' , processOutput ) ;
153172 child . on ( 'error' , ( error ) => {
154- process . removeListener ( 'SIGINT' , sigintHandler ) ;
155- process . removeListener ( 'SIGTERM' , sigtermHandler ) ;
173+ if ( forceCloseTimeout ) {
174+ clearTimeout ( forceCloseTimeout ) ;
175+ }
176+
177+ removeListeners ( ) ;
156178 reject ( error ) ;
157179 } ) ;
158180 child . on ( 'close' , ( code ) => {
159- process . removeListener ( 'SIGINT' , sigintHandler ) ;
160- process . removeListener ( 'SIGTERM' , sigtermHandler ) ;
181+ if ( forceCloseTimeout ) {
182+ clearTimeout ( forceCloseTimeout ) ;
183+ }
184+
185+ removeListeners ( ) ;
161186
162187 if ( tasksComplete ) {
163188 resolve ( 0 ) ;
0 commit comments