# Pygame freezing on QUIT

I'm having problems with quitting a Pygame application (6502 based computer emulator).

The part regarding exit looks like this:

while True:
do_something_200_times()

for event in pygame.event.get()
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()


I'm also using pygame.mixer to play sounds as they appear, but I'm not sure that's related. I'm also using pygame.time.set_timer to update screen in regular intervals and play sound if buffer has filled enough. Is it possible the event queue overfills? How would I determine if this is the case?

It simply freezes in 40-50% cases, and attaching strace -p to the python interpreter gives:

Process 21326 attached
futex(0x7f5a32f839d0, FUTEX_WAIT, 21329, NULL


Also, I'm getting occasional pulseaudio related crashes, is this combination known to be buggy?

Any ideas and suggestions are more than welcome! Thanks!

EDIT: it freezes when I click the window's close button (x)

• This is just guess, but sys.exit() only raises an exeption. If anything catches it (pygame or perhaps pygame.mixer) it won't actually exit. Try calling break or return instead to see if that's the problem. – Nocturno Nov 13 '15 at 18:22

RUNNING = True
while RUNNING:    # Loop while RUNNING == True
do_something_200_times()

for event in pygame.event.get()
if event.type == pygame.QUIT:
RUNNING = False    # Set RUNNING = False and end the loop

pygame.quit()    # Quit pygame and allow the program to end


Instead of creating an infinite loop, assign it's execution to a variable, in this case RUNNING. Then, when an exit event is raised, you can just set the variable, RUNNING, to false. That will end the loop where Pygame can be exited after the loop and allow the program to end naturally without raising any exceptions and without having to use the sys module.

• I tried that, but it behaves the same... sometimes it quits normally, sometimes it hangs waiting for some thread. If You have any advice how to debug further, I'm all ears. – Hrvoje Nov 27 '15 at 15:55