Quick answer: Structure the shutdown so nothing draws after quitting: break out of the main loop, quit once at the end, and end the process cleanly.

This error is a zombie frame: the game shut the display down and then tried to draw one more time. It is always a control-flow bug around exit. Here is the clean pattern.

How to fix it

1. Quit after the loop, not inside it

The canonical shape: running = True; while running: ... if event.type == pygame.QUIT: running = False, then pygame.quit() after the while — quitting mid-loop leaves the rest of that frame drawing on a dead surface.

2. Exit immediately if you must quit inline

If some code path calls pygame.quit() deep inside, follow it with sys.exit() (or raise SystemExit) so no later draw runs — but prefer the flag pattern.

3. Check timers and threads

pygame.time.set_timer events and worker threads posting draw work can fire after shutdown — cancel timers and join threads before quitting.

4. Reproduce by quitting fast

Open and close the game rapidly, including during loading — shutdown-path errors love the moments developers never test.

Catching the ones you can't reproduce

The hardest version of this to fix is the one you can't reproduce — it only happens on a player's hardware, OS, driver, or save state, under conditions that simply aren't present on your machine. A report that says “it crashed” or “it froze” gives you nothing to act on, so the bug survives release after release while quietly costing you players.

Automatic error capture closes that gap. Each failure arrives with its full stack trace, the device and OS, the build number, and a breadcrumb trail of what the player did right before it broke, so even a failure you have never seen becomes a specific, reproducible issue. Fold identical failures into one signature ranked by how many players each hits, and your worklist sorts itself worst-first instead of arriving as a stream of vague complaints.

This is where a tool like Bugnet earns its place. Its SDK captures every Pygame error automatically with the full stack trace plus device, OS, memory, build, and game-state context, folds duplicates into one grouped issue with an occurrence count, and ties each to the build it first appeared on — so you fix the problem that hurts the most players first and confirm it is gone when its signature disappears from the next release.

Ship the fix, watch the signature disappear from the next build. That's how you know it's really gone.