Skip to content
This repository was archived by the owner on Sep 28, 2025. It is now read-only.

Commit d741ac7

Browse files
committed
Fixup: Without using kill on linux
1 parent 446863f commit d741ac7

File tree

1 file changed

+19
-30
lines changed

1 file changed

+19
-30
lines changed

src/StreamMaster.Streams/Factories/CommandExecutor.cs

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ private void GracefullyTerminateProcessInternal(Process? processToTerminate, ILo
148148
if (processToTerminate == null) { log.LogDebug("Terminate: Process is null."); return; }
149149

150150
int processId = -1;
151-
try { processId = processToTerminate.Id; } catch { /* Handle case where Id access fails */ }
151+
try { processId = processToTerminate.Id; } catch { /* Handle */ }
152152

153153
bool alreadyExited = false;
154154
try { alreadyExited = processToTerminate.HasExited; }
@@ -168,45 +168,35 @@ private void GracefullyTerminateProcessInternal(Process? processToTerminate, ILo
168168

169169
try
170170
{
171+
bool exitedAfterQ = false;
171172
if (processToTerminate.StartInfo.RedirectStandardInput)
172173
{
173-
log.LogDebug("Terminate: Attempting to send 'q' to stdin for process {ProcessId}", processId);
174174
try
175175
{
176+
log.LogDebug("Terminate: Attempting to send 'q' to stdin for process {ProcessId}", processId);
176177
processToTerminate.StandardInput.WriteLine("q");
177178
processToTerminate.StandardInput.Close();
178-
179-
if (processToTerminate.WaitForExit(1000))
179+
if (processToTerminate.WaitForExit(1000)) // Wait 1 second
180180
{
181181
log.LogInformation("Process {ProcessId} terminated gracefully after sending 'q'.", processId);
182-
return;
182+
exitedAfterQ = true;
183183
}
184-
log.LogDebug("Terminate: Process {ProcessId} did not exit after sending 'q' and 1s wait.", processId);
185-
}
186-
catch (InvalidOperationException ioex)
187-
{
188-
log.LogWarning(ioex, "Terminate: Error accessing StandardInput for process {ProcessId} (likely already closed/exited).", processId);
189-
if (processToTerminate.WaitForExit(50))
184+
else
190185
{
191-
log.LogInformation("Process {ProcessId} terminated shortly after stdin error.", processId);
192-
return;
186+
log.LogDebug("Terminate: Process {ProcessId} did not exit after sending 'q' and 1s wait.", processId);
193187
}
194188
}
189+
catch (Exception ex) when (ex is InvalidOperationException || ex is ObjectDisposedException)
190+
{
191+
log.LogWarning(ex, "Terminate: Error accessing StandardInput for process {ProcessId} (likely already closed/exited).", processId);
192+
if (processToTerminate.WaitForExit(50)) exitedAfterQ = true; // Check again quickly
193+
}
195194
catch (Exception ex)
196195
{
197196
log.LogError(ex, "Terminate: Error sending 'q' to process {ProcessId}", processId);
198197
}
199198
}
200-
else
201-
{
202-
log.LogWarning("Terminate: Cannot send 'q' to process {ProcessId}, StandardInput not redirected.", processId);
203-
}
204-
205-
if (processToTerminate.HasExited)
206-
{
207-
log.LogDebug("Terminate: Process {ProcessId} exited before signal attempt.", processId);
208-
return;
209-
}
199+
if (exitedAfterQ) return;
210200

211201
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
212202
{
@@ -221,25 +211,24 @@ private void GracefullyTerminateProcessInternal(Process? processToTerminate, ILo
221211
}
222212
else
223213
{
224-
log.LogDebug("Terminate: Sending SIGTERM to process {ProcessId}...", processId);
225-
Process.Start("kill", $"-TERM {processToTerminate.Id}");
214+
log.LogDebug("Terminate: Waiting up to 3s for process {ProcessId} to exit (Non-Windows)...", processId);
226215
if (processToTerminate.WaitForExit(3000))
227216
{
228-
log.LogInformation("Process {ProcessId} terminated after SIGTERM.", processId);
217+
log.LogInformation("Process {ProcessId} terminated gracefully after wait (Non-Windows).", processId);
229218
return;
230219
}
231-
log.LogWarning("Process {ProcessId} did not terminate after SIGTERM, sending SIGKILL", processId);
220+
221+
log.LogWarning("Process {ProcessId} did not terminate after wait, forcing kill (Non-Windows)", processId);
232222
if (!processToTerminate.HasExited)
233223
{
234-
log.LogDebug("Terminate: Sending SIGKILL to process {ProcessId}...", processId);
235-
Process.Start("kill", $"-KILL {processToTerminate.Id}");
224+
processToTerminate.Kill(true);
236225
processToTerminate.WaitForExit(500);
237226
}
238227
}
239228
}
240229
catch (Exception ex)
241230
{
242-
log.LogError(ex, "Error during signal/kill phase for process {ProcessId}", processId);
231+
log.LogError(ex, "Error during termination phase for process {ProcessId}", processId);
243232
try
244233
{
245234
if (!processToTerminate.HasExited)

0 commit comments

Comments
 (0)