Skip to content

fix: infinite loop on user-initiated exit#43

Merged
jjhafer merged 1 commit intosilvermine:masterfrom
velocitysystems:exit_bug
Mar 10, 2026
Merged

fix: infinite loop on user-initiated exit#43
jjhafer merged 1 commit intosilvermine:masterfrom
velocitysystems:exit_bug

Conversation

@velocitysystems
Copy link
Contributor

@velocitysystems velocitysystems commented Mar 5, 2026

Problem

User-initiated app exit (e.g. closing the window, Cmd+Q) could hang indefinitely. The exit handler called api.prevent_exit(), ran cleanup, then called app_handle.exit() — which re-triggered ExitRequested, creating an infinite loop.

Solution

Break the loop by ensuring cleanup only runs once, then letting the programmatic exit through:

  1. An atomic state guard tracks whether cleanup has started. The first ExitRequested claims ownership; subsequent ones are skipped.
  2. Cleanup runs asynchronously on the Tokio runtime instead of blocking the event loop with thread::spawn().join().
  3. After cleanup finishes (or times out after 5 seconds), app_handle.exit(0) fires a programmatic exit. The handler recognises programmatic exits by their exit code and lets them pass through without re-running cleanup.
  4. If the user triggers exit again while cleanup is still running (e.g. repeated Cmd+Q on macOS), prevent_exit() is called to keep the app alive until cleanup completes, but cleanup itself is not restarted.

@velocitysystems velocitysystems force-pushed the exit_bug branch 2 times, most recently from 892eaa1 to f147d05 Compare March 6, 2026 16:22
@jjhafer jjhafer merged commit 1e8be7e into silvermine:master Mar 10, 2026
1 check passed
@velocitysystems velocitysystems deleted the exit_bug branch March 12, 2026 10:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants