emulator: run cleanup handlers on the quick_exit path #334
No reviewers
Labels
No labels
bug
documentation
duplicate
enhancement
GPU
GUI
help wanted
question
Shaders
Video
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/KytyPS5--KytyPS5!334
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/334/head"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@MehmetCambaz on GitHub · 2026-08-28 15:33 UTC · original
MehmetCambaz:fix/quick-exit-cleanup→main· +4 −0 · 1 files · 1 commits · merged 2026-08-29 00:44 UTCProblem
Run()registers a cleanup handler withatexit():but
Execute()terminates the process with:std::quick_exitinvokesat_quick_exithandlers only. It does not runatexithandlers, and it does not flush open C streams. Nothing insrc/registers anat_quick_exithandler, soKytyClosenever runs on a normal exit.Because
KytyCloseis what callsSubsystems::EmergencyShutdownActive(), none of the emergency handlers run either:FileSystem::EmergencyShutdown→g_files->CloseAll()— open guest files are never closedLog::Shutdown— the log is never flushed or closedProfiler::Shutdown— the capture is never endedThis is easy to confirm without a debugger:
KytyCloselogsdone!as its last action, anddone!does not appear anywhere in a log from a normal exit. I checked a 276 MB log from a full play session and the count is zero.The severity depends on the platform, because the file backends differ:
sysWindowsFileIO.cpp) usesWriteFile, so writes have already reached the OS and survive process termination.sysLinuxFileIO.cpp) usesfopen/fwrite. Sincequick_exitdoes not flush C streams, buffered guest writes are dropped — a save written shortly before quitting can be lost.Truncated logs have the same root cause, which matters given that the issue template asks for the complete log file.
Fix
Register a handler on the path the process actually takes.
The new handler runs
Subsystems::EmergencyShutdownActive()and deliberately does not callRuntimeLinker::Clear().I tried the obvious version first — registering
KytyCloseitself withat_quick_exit— and it hung the emulator on exit. Guest threads are still live atquick_exittime, andrt->Clear()deadlocks against a thread parked in a condition wait. The log tail showed it stuck inPthreadCondWait. That is very likely why this code ended up onquick_exitin the first place.The emergency handlers are the correct subset here: closing files and flushing the log is exactly what they exist for, and it is already what the fatal-error path in
assert.cppuses.Testing
Windows 11 25H2, clang-cl 20.1.8, Qt 6.10.3, RTX 2060 (driver 596.36). Tested against a commercial title (Tetris Forever, PPSA25646) booted through to gameplay and then quit.
ctest: 31/31 pass.KytyClosevariant described above.WM_CLOSEto process termination, two runs each:main(c52bf45)I did not find a practical way to cover this in the existing test suite. Verifying it requires observing real process termination, and
quick_exitends the process, so it would need a subprocess harness thattests/does not currently have. Happy to add one if you would like it.Only Windows is verified. The bug is worse on Linux and macOS because of the buffered
fwritebackend, but I have no machine to test those on. A second pair of eyes there would be welcome.AI use
This change was developed with AI assistance (Claude). The AI traced the
atexit/quick_exitmismatch, proposed the fix, and ran the builds, tests and timing measurements described above on my machine.I reviewed the diff and understand why it is correct:
atexithandlers are not invoked bystd::quick_exit, onlyat_quick_exithandlers are, so the existing registration could never fire on this exit path. I also understand whyRuntimeLinker::Clear()has to stay out of the quick-exit handler — I saw the hang it causes when it is included. I ran the emulator before and after the change and confirmed the exit behaviour myself.