Chrome app [removed] not fired when process ended

2019-08-03 09:41发布

问题:

We have a chrome app that we keep open by openning again the main window in response to it's onClosed event. However, this event is not raised when the process is ended from Google's Chrome Task Manager. Why is this? Can we get around this so that we can keep the app open? So far, I've just run this on Windows.

回答1:

Chrome Task Manager uses a low-level system function to terminate the process, see the source code (1, 2).

  • On Windows it's TerminateProcess, and the documentation says:

    A process cannot prevent itself from being terminated.

    A quote from The Old New Thing (it's an MS software engineer's blog, quite famous):

    Eventually you have to decide which side wins, and Windows has decided to keep users in control of their own programs and data, and keep administrators in control of their own computer. So users can kill any process they want (given sufficient privileges), they can stop any program from stealing focus, and they can delete any file they want (again, given sufficient privileges).

    Programs can try to make themselves more difficult to kill (deny PROCESS_TERMINATE access, deny PROCESS_CREATE_THREAD access so people can't CreateRemoteThread(EndProcess), deny PROCESS_VM_WRITE so people can't scribble into your stack and make you doublefault, deny PROCESS_SUSPEND_RESUME so they can't suspend you), but eventually you just can't stop them from, say, elevating to Debug privilege, debugging your process, and moving EIP to "ExitProcess".

    Notice that you can kill CSRSS.EXE and WINLOGON.EXE if you like. Your computer will get very angry at you, but you can do it. (Save you work first!)

    Another useful question to ask yourself: "What's to prevent a virus from doing the same thing?" If there were a way to do these things, then a virus could take advantage of them and make itself invisible to Task Manager, undeletable, and unkillable. Clearly you don't want that, do you?

  • On Unix Chrome Task Manager uses kill API function to send SIGTERM signal, which can be seen by the process, followed by unconditional SIGKILL (after some time):

    The signals SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.