Fix fatal crash on Windows when window title matches MIDI loopback device name#17
Open
zitongcharliedeng wants to merge 2 commits intoflipcoder:mainfrom
Open
Conversation
On certain Windows systems, pygame.display.set_caption('midimech') causes
a fatal native process termination (exit code -1, no traceback) when a MIDI
loopback device named 'midimech' is present. This appears to be a
case-insensitive collision between SDL's window title and the Windows MIDI
subsystem, causing TerminateProcess ~1-2s after the window is created.
Fix: Change TITLE from 'midimech' to 'midimech app' to avoid the name
collision. Also harden the MIDI callback and shutdown cleanup.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes a fatal crash on some Windows setups where midimech silently dies ~1-2 seconds after launch with exit code
-1(0xFFFFFFFF). No Python traceback, no faulthandler output, no Windows Event Viewer entries — just a hard native process termination.I was trying midimech on an older Windows system — I usually run it on Linux or a different Windows machine where loopMIDI works fine. This particular system has some combination of loopMIDI driver version, audio stack, or Windows configuration that triggers the issue. The exact underlying cause is unknown, but the fix is harmless and prevents the crash entirely.
Root Cause
pygame.display.set_caption("midimech")collides with the MIDI loopback device named"midimech"(created via loopMIDI). On this Windows setup, when an SDL window title matches a MIDI device name (case-insensitive), something in the MIDI subsystem fatally terminates the process approximately 1-2 seconds after the window is created. The crash happens duringpygame.event.get()on the first frame of the main loop.This only affects some Windows configurations — the exact mechanism is unknown. It does not appear to affect all Windows systems with loopMIDI.
How it was found
This took extensive bisection testing (~30 test scripts) over multiple sessions to isolate:
Core.__init__— the crash disappeared when__init__was replaced with a standalone function via monkey-patchingpygame.display.set_caption(TITLE)beforeset_mode()crashed, while the one withoutset_captionsurvivedset_caption("midimech")+set_mode()+ event loop crashed, whileset_caption("test")orset_caption("midimech2")survived"midimech","Midimech","midiMECH"all crash;"midimech app"or any non-matching string survivesWhy the earlier init hypothesis was misleading
The monkey-patched
__init__tests survived because they set up the pygame display manually in the test script without callingset_caption(TITLE). The realCore.__init__always calledset_caption(TITLE), so it always crashed. The correlation with__init__size was a red herring.Changes
src/constants.py: ChangedTITLEfrom"midimech"to"midimech app"to avoid the MIDI device name collisionsrc/core.py: Added init guard and try/except wrapper tocb_midi_in— prevents potential crashes if the MIDI callback fires beforeCoreis fully initializedmidimech.py: Hardenedmain()cleanup — wrappeddel core,pygame.midi.quit(), andpygame.display.quit()in individual try/except blocks so one failure doesn't prevent the others from runningCrash characteristics (for future reference)
If anyone encounters similar issues, here's the signature:
0xFFFFFFFF(-1), not a Python exceptionatexithandlers don't run — this is a hardTerminateProcessgc.disable()delays but doesn't prevent the crashVerified