Prevent race condition when restarting NVDA
Prior to f8852a8482a353f415c1070530d06bc6f7b054ec doInstall in
installerGui.py would just start a new copy of NVDA allowing the
new copy of NVDA to terminate the old copy by posting a WM_QUIT message.
With the update of wxPython, this caused a crash, see PR #12431 for
details.
The new approach was to trigger the exit NVDA after starting the new
process.
This leads to a race condition for ownership of the "single NVDA
instance" mutex.
To fix this race condition the mutex is created without taking ownership,
then waitForSingleObject is used to take ownership.
If the old NVDA process is still running, the new process waits.
If the old NVDA process has already released the mutex, the new process
will take ownership immediately.
If the old NVDA process has crashed and not released the mutex, the new
process takes ownership and logs the situation.
Note, previously only CloseHandle was used, this relied on the process
exiting and Windows releasing the mutex.
Now, the mutex is explicitly released before the handle is closed.