Skip to content

Commit 89ce9b1

Browse files
committed
refactor: ignore errors when the process already exited
Signed-off-by: leo <longshuang@msn.cn>
1 parent faf98d8 commit 89ce9b1

1 file changed

Lines changed: 18 additions & 1 deletion

File tree

src/Native/Linux.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,24 @@ public void OpenWithDefaultEditor(string file)
180180
public void TerminateProcess(Process proc)
181181
{
182182
if (kill(-proc.Id, 15) != 0)
183-
proc.Kill(true); // Fallback to force kill if the process is not terminated by SIGTERM
183+
{
184+
// If the process already exited, we can just ignore the error.
185+
if (Marshal.GetLastPInvokeError() == 3 /* ESRCH */)
186+
return;
187+
188+
// Actually, this will not be called since the process is
189+
// spawned by us (EPERM will not happen), and SIGTERM (15)
190+
// is a valid signal (EINVAL will not happen).
191+
// See https://www.man7.org/linux/man-pages/man2/kill.2.html
192+
try
193+
{
194+
proc.Kill(true);
195+
}
196+
catch
197+
{
198+
// Ignore any errors when trying to kill the process
199+
}
200+
}
184201
}
185202

186203
private string FindExecutable(string filename)

0 commit comments

Comments
 (0)