kernel: act on thread cancellation through PthreadExit on Windows #2017
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!2017
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/2017/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?
@lukanatroshvili67 on GitHub · 2026-10-31 12:48 UTC · original
lukanatroshvili67:fix/windows-thread-cancel→main· +25 −2 · 1 files · 1 commits · closed without mergingFixes #2015.
On Windows, a guest thread acting on a pending cancel killed the process with
0xC0000028(STATUS_BAD_STACK).PthreadCancel/PthreadTestcancelforwarded to winpthread, which terminates the thread by unwinding its stack. That unwind can't get through the guest stack, whereRunOnGuestStackhas cleared the TEB stack bounds.Change (Windows only; Linux and macOS are unchanged)
PthreadCancelrecords the request in a newPthreadPrivate::cancel_requestedflag instead of callingpthread_cancel.PthreadTestcancelchecks the flag and the current cancel state, which it reads without changing it. If cancellation is enabled, it exits throughPthreadExit. That path already returns to theRunOnGuestStackframe and restores the TEB fields.(void*)-1, the same as the glibcPTHREAD_CANCELEDthe Linux build reports. winpthread's value is0xDEADBEEF.Behavior note: on Windows, cancellation is now acted on at
PthreadTestcancel. A thread blocked in a host wait isn't woken by a cancel. That path crashed before, so nothing that used to work regresses.Testing (Windows 11, clang-cl 19.1.5, RTX 3060)
kernel_file_system_tests --mutex-only: was0xC0000028on every run, now passes 5/5. That's the mutex handoff and deferred-cancellation cases inTestMutexHandoff.sync_on_addressandevent_queue_lifetime) still pass, andkyty_emulatorbuilds.With the crash gone, the full
kernel_file_systemrun gets further and stops on an unrelated Windows difference:truncate("/savedata0/truncate-save.dat/child", 0)returnsENOENTinstead ofENOTDIR. I'll handle that separately.@coderabbitai[bot] on GitHub · 2026-10-31 12:56 UTC · original
Findings
6e23b832-38fb-4ddc-b060-850386d76982Reviewing files that changed from the base of the PR and between
026189166fand26a7f07ee9.src/kernel/pthread.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.
Walkthrough
On Windows,
PthreadCancelsets a per-thread atomic cancellation request instead of callingpthread_cancel.PthreadTestcancelchecks the request and callsPthreadExitwith the canceled value when cancellation is enabled. Other platforms retain their native cancellation paths.Suggested reviewers:
nmzikChange: Bug fix · Severity of issue fixed: Medium
Merge Risk:
Moderate · up to
26a7fThis fixes the Windows cancellation crash, but a reused thread record can inherit a stale cancel request and exit unexpectedly. Asynchronously canceled threads that never call PthreadTestcancel are not stopped, so joins on them can hang. Fix the record reuse bug before merging; the other two limits should be fixed or explicitly accepted.
Pre-merge checks:
4 ·
1
#2015. On Windows,PthreadCancelstorescancel_requestedinstead of callingpthread_cancel.PthreadTestcancelreads the request and cancellation state, then calls `PthreadExit(rei…src/kernel/pthread.cpp. It adds Windows-only cancellation state and changes the Windows implementations ofPthreadCancelandPthreadTestcancel. The non-Windows code remains…Full details: Linked Issues check
Explanation
The PR meets
#2015. On Windows,PthreadCancelstorescancel_requestedinstead of callingpthread_cancel.PthreadTestcancelreads the request and cancellation state, then callsPthreadExit(reinterpret_cast<void*>(-1))when cancellation is enabled. The change avoids host stack unwinding from the guest stack. The change is guarded for Windows, so the native Linux and macOS paths remain unchanged. The reported--mutex-onlyresult changed fromSTATUS_BAD_STACKto 5/5 passes.Full details: Out of Scope Changes check
Explanation
The diff changes only
src/kernel/pthread.cpp. It adds Windows-only cancellation state and changes the Windows implementations ofPthreadCancelandPthreadTestcancel. The non-Windows code remains the native path. These changes directly support#2015and do not include unrelated truncate behavior or other out-of-scope work.Full details: Docstring Coverage
Explanation
Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 2 functions across 1 files.
Resolution
Write docstrings for the functions missing them to satisfy the coverage threshold.
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Share
@ -414,1 +413,3 @@uintptr_t guest_host_gs10;uintptr_t guest_host_gs8;uintptr_t guest_host_gs10;std::atomic_bool cancel_requested {false};@coderabbitai[bot] on GitHub · 2026-10-31 12:56 UTC · original
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 1576
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 13462
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 4876
Reset cancellation state when reusing a thread record.
PthreadPool::Createreturns a previously freed record after setting onlyfree = false. A canceled thread leavescancel_requestedset, so a new thread using that record can exit at its firstPthreadTestcancel.Suggested fix
@ -3400,0 +3402,4 @@// winpthread acts on a cancel by unwinding the thread's stack, which fails with// STATUS_BAD_STACK on the guest stack. PthreadTestcancel exits through PthreadExit instead.thread->cancel_requested.store(true, std::memory_order_release);int result = 0;@coderabbitai[bot] on GitHub · 2026-10-31 12:56 UTC · original
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 461
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 23357
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 41963
Web query:
winpthreads 0.5.0 pthread_setcanceltype PTHREAD_CANCEL_ASYNCHRONOUS pthread_cancel implementationResult:
Web query:
"pthread_setcanceltype" "pthread_cancel" winpthreads thread.c asynchronousResult:
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 42346
Preserve asynchronous cancellation or reject it explicitly.
PthreadSetcanceltype(2)succeeds and selects asynchronous cancellation. If a guest thread then runs withoutPthreadTestcancel, WindowsPthreadCancelonly setscancel_requested; no code consumes that request.PthreadJoincan therefore wait indefinitely.Before this change, bundled winpthread
pthread_cancelhad a separate asynchronous path that redirected the target to_pthread_invoke_cancelwithout requiringpthread_testcancel. The0xC0000028failure in the deferredPthreadTestcancelpath does not make asynchronous cancellation unreachable or unsupported.Support asynchronous delivery without guest-stack unwinding, or return an error instead of reporting
PthreadSetcanceltype(2)as supported.@ -3555,0 +3568,4 @@// Read the cancel state without changing it.int state = PTHREAD_CANCEL_ENABLE;pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &state);pthread_setcancelstate(state, nullptr);@coderabbitai[bot] on GitHub · 2026-10-31 12:56 UTC · original
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 4148
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 43762
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 9857
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 11284
Keep cancellation disabled during cancellation cleanup.
PthreadTestcancelrestores the enabled state beforePthreadExit. A destructor can callPthreadTestcancelagain whileCleanupThreadis running. The second call can terminate the thread before the remaining cleanup and bookkeeping complete.Suggested fix
Committable suggestion
@coderabbitai[bot] on GitHub · 2026-10-31 12:56 UTC · original · reviewed
Actionable comments posted: 3
6e23b832-38fb-4ddc-b060-850386d76982Reviewing files that changed from the base of the PR and between
026189166fand26a7f07ee9.src/kernel/pthread.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.
Pull request closed