1

Edit: Looks like a duplicate, but I assure you, it's not. I'm looking to kill the current running process cleanly, not to kill a separate process.

The problem is the process I'm killing isn't spawned by subprocess or exec. It's basically trying to kill itself.

Here's the scenario: The program does cleanup on exit, but sometimes this takes too long. I am sure that I can terminate the program, because the first step in the quit saves the Database. How do I go about doing this?

  • cannot use taskkill, as it is not available in some Windows installs e.g. home editions of XP
  • tskill also doesn't work

  • win32api.TerminateProcess(handle, 0) works, but i'm concerned it may cause memory leaks because i won't have the opportunity to close the handle (program immediately stops after calling TerminateProcess). note: Yup, I am force quitting it so there are bound to be some unfreed resources, but I want to minimize this as much as possible (as I will only do it only if it is taking an unbearable amount of time, for better user experience) but i don't think python will handle gc if it's force-quit.

I'm currently doing the last one, as it just works. I'm concerned though about the unfreed handle. Any thoughts/suggestions would be very much appreciated!

3 Answers 3

1

win32api.TerminateProcess(handle, 0) works, but i'm concerned it may cause memory leaks because i won't have the opportunity to close the handle (program immediately stops after calling TerminateProcess). note: Yup, I am force quitting it so there are bound to be some unfreed resources, but I want to minimize this as much as possible (as I will only do it only if it is taking an unbearable amount of time, for better user experience) but i don't think python will handle gc if it's force-quit.

If a process self-terminates, then you don't need to worry about garbage collection. The OS will automatically clean up all memory resources used by that process, so you don't have to worry about memory leaks. Memory leaks are when a process is running and using more and more memory as time goes by.

So yes terminating your process this way isn't very "clean", but there wont be any ill side-effects.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks; that was what i was worrying about, but it does look like windows does the cleaning up. i ended up using this method. i know killing the process is a little "dirty", but it's being done for a better user experience, and only as a last resort (if cleanup takes > 1 minute).
1

If I understand your question, you're trying to get the program to shut itself down. This is usually done with sys.exit().

1 Comment

I've tried this too; but the problem is the program sometimes gets stuck in the quit sequence itself (WM_EXIT message). I could try to redesign it such that the processes in WM_EXIT will be spawned in a separate thread, so I can call sys.exit from the main thread if the threaded processes are not responding, unless there is a better way to do this.
1

TerminateProcess and taskkill /f do not free resources and will result in memory leaks. Here is the MS quote on terminateProcess: { ... Terminating a process does not cause child processes to be terminated. Terminating a process does not necessarily remove the process object from the system. A process object is deleted when the last handle to the process is closed. ... } MS heavily uses COM and DCOM, which share handles and resources the OS does not and can not track. ExitProcess should then be used instead, if you do not intend to reboot often. That allows a process to properly free the resources it used. Linux does not have this problem because it does not use COM or DCOM.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.