IridiumIO / CompactGUI

Transparently compress active games and programs using Windows 10/11 APIs
GNU General Public License v3.0
4.71k stars 223 forks source link

Unhandled `AbandonedMutexException` #413

Closed Eta0 closed 1 week ago

Eta0 commented 3 months ago

Attempting to grab the mutex during application startup can throw an AbandonedMutexException, which successfully acquires the mutex, but will crash the program if not handled. When it crashes, it abandons the mutex again, ultimately causing the program to crash repeatedly on every launch until the mutex is cleaned up by some other mechanism.

There are two prerequisites to get into this situation:

  1. Something else causes the mutex to be abandoned the first time (some unrelated crash)
  2. Another process is holding an open handle to the mutex between program launches and prevents it from being cleaned up by the operating system (which would reset its state automatically)

I noticed this because of some other bug that was leaving an instance application in a quasi-shut-down state (no remaining application window, and not holding that mutex, but not fully exiting and releasing handles), which then made all subsequent launches fail until the odd one was terminated via task manager. It's a pretty niche set of circumstances, but should be pretty easy to handle to prevent that sort of crash loop from coming up. Either ignoring the AbandonedMutexException and treating it as a successful acquire (since it still is one) or attempting to clean up the program state by terminating all other CompactGUI instances (since something is broken at that point) might be reasonable approaches.