Process.Kill throwing Win32Exception (Access is denied)
Asked Answered
B

0

6

I have the following code:

foreach (var process in Process.GetProcessesByName(name))
{
    try
    {
        process.Kill();
        if (!process.WaitForExit(timeout))
        {
            session.Log("Killing process {0} timed out.", name);
        }
        else
        {
            session.Log("Killing process {0} finished successfully.", name);
        }
    }
    catch (Exception exception)
    {
        session.Log("Exception while trying to kill process {0}: {1}", name, exception.ToString());
    }
}

Process.Kill is throwing the following exception:

Exception while trying to kill process <process name>: System.ComponentModel.Win32Exception (0x80004005): Access is denied
   at System.Diagnostics.Process.Kill()
   at CustomActions.CustomActions.KillAllProcesses(String name, Session session, Nullable`1 wait)

The code usually works, but the odd time it does fail. Its part of a custom action on an installer that I have built, and when it runs it runs under the local system account. I have seen this happen with Process.Kill in the past. I know there are some alternatives to it. I cannot modify the code of the process in question so I am on my own to terminate it however I see fit. What is the best method of nuking a process that needs to die ASAP if I don't care about leaving it in some intermediate state because I am uninstalling its files from the system? Any IO should be allowed to finish or get canceled in case any settings are in the process of being set for the app on a global level and the user does an upgrade. Would P/Invoking TerminateProcess be the best solution?

Brendabrendan answered 16/3, 2015 at 18:38 Comment(5)
Have you tried to terminate it via Task Manager or Process Hacker or some such tool? Just to see what would happen.Irremissible
@Irremissible It works just fine, but so does Process.Kill some time after. I can't be sure of the state its in when I terminate it using task manager or process hacker (whatever state it is in that causes Process.Kill to throw this exception is a rare occurrence).Brendabrendan
MSDN Page for Process.Kill has a note: "If the call to the Kill method is made while the process is currently terminating, a Win32Exception is thrown for Access Denied." So, may be the process was already being terminated?Asparagine
@sthotakura Yes, I make a few calls to Process.Kill to try and kill that process, so this makes sense. It seems the process doesn't die at all though. Initially, I called Process.Kill on it, then again after 5 seconds, then again after another 5 seconds. I can kill the process fine now, and I can kill it now using Process.Kill, but during that particular time-frame it would not die despite going into a terminated state, almost as if termination was canceled.Brendabrendan
It looks like the problem was partly my fault. Something kept restarting the process but I wasn't able to use the GetProcesses() method to enumerate and see this change of active processes from the installer side of things because (as it turns out) I wasn't running under the SYSTEM account, but under another account, and for some reason (which I can't explain TBH) that method did not give me the active processes back. I ran my custom action using SYSTEM by setting Execute="deferred" Impersonate="no" and it worked, and I was able to kill my process a second time after it had been restarted.Brendabrendan

© 2022 - 2024 — McMap. All rights reserved.