@@ -93,11 +93,9 @@ public static bool IsErrored(this IProcessProxy process, IEnumerable<int> succes
9393 /// </summary>
9494 /// <param name="process">The process to kill.</param>
9595 /// <param name="logger">The logger to use to write trace information.</param>
96- /// <param name="confirmationWaitTime">Max duration to wait for exit. Default = 10 seconds. Use TimeSpan.Zero for no wait.</param>
96+ /// <param name="confirmationWaitTime">Max duration to wait for exit. Default = no wait.</param>
9797 public static void SafeKill ( this IProcessProxy process , ILogger logger = null , TimeSpan ? confirmationWaitTime = null )
9898 {
99- TimeSpan effectiveTimeout = confirmationWaitTime ?? TimeSpan . FromSeconds ( 10 ) ;
100-
10199 if ( ! process . HasExited )
102100 {
103101 // Process confirmed exited
@@ -118,7 +116,7 @@ public static void SafeKill(this IProcessProxy process, ILogger logger = null, T
118116 EventContext errorContext = EventContext . Persisted ( ) ;
119117 errorContext . AddProcessDetails ( process . ToProcessDetails ( process . Name ) ) ;
120118 errorContext . AddError ( new WorkloadException (
121- $ "Process kill attempt failed (id={ processId } , name={ processName } , timeout={ effectiveTimeout } ).",
119+ $ "Process kill attempt failed (id={ processId } , name={ processName } , timeout={ confirmationWaitTime ? . ToString ( ) ?? "none" } ).",
122120 exc ) ) ;
123121
124122 logger ? . LogMessage ( $ "ProcessKillFailed.{ processName } ", LogLevel . Warning , errorContext ) ;
@@ -171,63 +169,58 @@ public static void SafeKill(this ProcessManager processManager, IEnumerable<stri
171169 /// <param name="confirmationWaitTime">Max duration to wait for exit. Default = 10 seconds. Use TimeSpan.Zero for no wait.</param>
172170 public static void SafeKill ( this ProcessManager processManager , IProcessProxy process , ILogger logger = null , TimeSpan ? confirmationWaitTime = null )
173171 {
174- TimeSpan effectiveTimeout = confirmationWaitTime ?? TimeSpan . FromSeconds ( 10 ) ;
172+ string processName = null ;
173+ int processId = - 1 ;
175174
176- using ( CancellationTokenSource tokenSource = new CancellationTokenSource ( effectiveTimeout ) )
175+ try
177176 {
178- string processName = null ;
179- int processId = - 1 ;
177+ processName = SafeGet < string > ( ( ) => process . Name ) ;
178+ processId = SafeGet < int > ( ( ) => process . Id ) ;
180179
181- try
180+ if ( processManager . Platform == PlatformID . Unix )
182181 {
183- processName = SafeGet < string > ( ( ) => process . Name ) ;
184- processId = SafeGet < int > ( ( ) => process . Id ) ;
185-
186- if ( processManager . Platform == PlatformID . Unix )
182+ using ( IProcessProxy kill = processManager . CreateProcess ( "kill" , $ "-9 { processId } ") )
187183 {
188- using ( IProcessProxy kill = processManager . CreateProcess ( "kill" , $ "-9 { processId } ") )
189- {
190- kill . StartAndWaitAsync ( tokenSource . Token , confirmationWaitTime )
191- . GetAwaiter ( ) . GetResult ( ) ;
184+ kill . StartAndWaitAsync ( CancellationToken . None , confirmationWaitTime )
185+ . GetAwaiter ( ) . GetResult ( ) ;
192186
193- // 0 = Success
194- // 1 = Process not found
195- if ( kill . ExitCode != 0 && kill . ExitCode != 1 )
196- {
197- kill . ThrowErrored < WorkloadException > (
198- $ "Unix kill -9 attempt failed with exit code { kill . ExitCode } for process (id={ processId } , name={ processName } , timeout={ effectiveTimeout } ). " +
199- $ "{ kill . StandardOutput } { kill . StandardError } ". Trim ( ) ,
200- ErrorReason . WorkloadUnexpectedAnomaly ) ;
201- }
187+ // 0 = Success
188+ // 1 = Process not found
189+ if ( kill . ExitCode != 0 && kill . ExitCode != 1 )
190+ {
191+ kill . ThrowErrored < WorkloadException > (
192+ $ "Unix kill -9 attempt failed with exit code { kill . ExitCode } for process (id={ processId } , name={ processName } , timeout={ confirmationWaitTime ? . ToString ( ) ?? "none" } ). " +
193+ $ "{ kill . StandardOutput } { kill . StandardError } ". Trim ( ) ,
194+ ErrorReason . WorkloadUnexpectedAnomaly ) ;
202195 }
203196 }
204- else if ( processManager . Platform == PlatformID . Win32NT )
197+ }
198+ else if ( processManager . Platform == PlatformID . Win32NT )
199+ {
200+ using ( IProcessProxy taskkill = processManager . CreateProcess ( "taskkill" , $ "/F /PID { processId } ") )
205201 {
206- using ( IProcessProxy taskkill = processManager . CreateProcess ( "taskkill" , $ "/F /PID { processId } ") )
207- {
208- taskkill . StartAndWaitAsync ( tokenSource . Token , confirmationWaitTime )
209- . GetAwaiter ( ) . GetResult ( ) ;
202+ taskkill . StartAndWaitAsync ( CancellationToken . None , confirmationWaitTime )
203+ . GetAwaiter ( ) . GetResult ( ) ;
210204
211- // 0 = Success
212- // 1 = Process not found
213- if ( taskkill . ExitCode != 0 && taskkill . ExitCode != 128 )
214- {
215- taskkill . ThrowErrored < WorkloadException > (
216- $ "Windows taskkill attempt failed with exit code { taskkill . ExitCode } for process (id={ processId } , name={ processName } , timeout={ effectiveTimeout } ). " +
217- $ "{ taskkill . StandardOutput } { taskkill . StandardError } ". Trim ( ) ,
218- ErrorReason . WorkloadUnexpectedAnomaly ) ;
219- }
205+ // 0 = Success
206+ // 1 = Process not found
207+ if ( taskkill . ExitCode != 0 && taskkill . ExitCode != 128 )
208+ {
209+ taskkill . ThrowErrored < WorkloadException > (
210+ $ "Windows taskkill attempt failed with exit code { taskkill . ExitCode } for process (id={ processId } , name={ processName } , timeout={ confirmationWaitTime ? . ToString ( ) ?? "none" } ). " +
211+ $ "{ taskkill . StandardOutput } { taskkill . StandardError } ". Trim ( ) ,
212+ ErrorReason . WorkloadUnexpectedAnomaly ) ;
220213 }
221214 }
222215 }
223- catch ( Exception exc )
224- {
225- EventContext errorContext = EventContext . Persisted ( ) ;
226- errorContext . AddProcessDetails ( process . ToProcessDetails ( processName ) ) ;
227- errorContext . AddError ( exc ) ;
216+ }
217+ catch ( Exception exc )
218+ {
219+ EventContext errorContext = EventContext . Persisted ( ) ;
220+ errorContext . AddProcessDetails ( process . ToProcessDetails ( processName ) ) ;
221+ errorContext . AddError ( exc ) ;
228222
229- logger ? . LogMessage ( $ "ProcessKillFailed.{ processName } ", LogLevel . Warning , errorContext ) ;
230- }
223+ logger ? . LogMessage ( $ "ProcessKillFailed.{ processName } ", LogLevel . Warning , errorContext ) ;
231224 }
232225 }
233226
0 commit comments