sceKernelRaiseException is lost for a target that busy-waits in short sceKernelUsleep calls (Windows) #12131

Closed
opened 2026-10-27 22:31:43 +00:00 by 2Swagger · 0 comments
2Swagger commented 2026-10-27 22:31:43 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by 2Swagger on 2026-10-27T22:31:43Z.


What happens

On Windows, sceKernelRaiseException returns 0, but the handler never runs: the target keeps running (its loop counter keeps
advancing) and no delivery arrives within 3 s. The first one or two raises usually arrive; a later one is lost.
It happens when the target spends almost all its time in host code with waitCount == 0, as a thread looping on
sceKernelUsleep(200) does: on Windows a sleep under TimedWait's 0.5 ms LEAD_NANOS busy-waits in SleepUntil
(YieldProcessor loop) rather than an alertable wait, so the raise takes the host-code path added in #10666.

Repro

Windows 11, WinLibs GCC 15.2.0, Release, Intel i5-10500H; main f5fefb982.

  1. Add this test as core/libs/tests/GuestRaiseLostRepro.cpp and register it in the Windows-only test block of
    core/libs/prx/libkernel/CMakeLists.txt like guest_raise_in_host_code (TIMEOUT 60, libkernel on PATH):
#include "prx/libkernel/include/Api.hpp"
#include "prx/libkernel/Time/include/Time.hpp"
#include "SceTypes.hpp"
#include <atomic>
#include <chrono>
#include <cstdio>
#include <cstdlib>
#include <thread>

static std::atomic<int> deliveries{0};
static std::atomic<bool> stop{false};
static std::atomic<unsigned long long> progress{0};

static void APS5_VABI Handler(int, void*) { deliveries.fetch_add(1); }

static void* APS5_VABI Nap(void*) {
    while (!stop.load()) {
        sceKernelUsleep_nid_postfix(200);
        progress.fetch_add(1);
    }
    return nullptr;
}

int main() {
    if (sceKernelInstallExceptionHandler(30, reinterpret_cast<void*>(&Handler)) != 0) std::abort();
    Pthread thread = nullptr;
    if (scePthreadCreate(&thread, nullptr, Nap, nullptr, "nap") != 0) std::abort();
    while (progress.load() == 0) std::this_thread::yield();
    for (int round = 0; round < 200; ++round) {
        const int before = deliveries.load();
        if (sceKernelRaiseException(thread, 30) != 0) std::abort();
        const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(3);
        while (deliveries.load() == before) {
            if (std::chrono::steady_clock::now() >= deadline) {
                std::fprintf(stderr, "LOST delivery in round %d (worker progress %llu, still advancing)\n", round, progress.load());
                std::_Exit(2);
            }
            std::this_thread::yield();
        }
    }
    stop.store(true);
    scePthreadJoin(thread, nullptr);
    std::printf("all 200 deliveries arrived\n");
    return 0;
}
  1. cmake --build build --target guest_raise_lost_repro_tests and ctest --test-dir build -R ^guest_raise_lost_repro$ --output-on-failure.

Results

  • main f5fefb982 (with #10666): 5 of 5 runs fail after about 3 s with LOST delivery in round 0 or round 1
    (worker progress about 14,700-14,800, still advancing).
  • #10666's head 273c8c256 before merging: 5 of 5 runs fail the same way.
  • main a120df348, before #10666 merged: 5 of 5 runs deliver all 200 (15.7-20.3 s, about 80 ms per raise: the old
    "redirect in host code after the 50 ms limit" behaviour #10666 removed as unsafe).

What it affects

  • No existing test covers it: guest_raise_in_host_code and guest_raise_leaves_host_code pass, and use targets that
    return to guest code from longer or alertable waits.
  • Titles that raise exceptions at threads polling with short sleeps. Unity's GC thread-stop (the Graveyard Keeper 2
    case #10666 fixed) raises at every thread and waits for all acknowledgements, so a lost delivery there would hang the
    stop; not observed on a title yet.

Best guess at the cause (unverified)

With the target in host code and waitCount == 0, delivery depends on the return-address breakpoint (DR0) firing at the
return into guest code, with the deliverer thread's sampling as a fallback. The target is in guest code for only a few
nanoseconds per 200 us, so sampling essentially never sees it there. A lost delivery therefore means the breakpoint did
not fire or was declined: either ArmReturnBreak/UnwindToGuest did not arm it from the busy-wait frames (unwind stopped
or the return site was rejected), or ReturnBreakHandler declined the hit (the review of #10666 noted it "can decline the
DR0 hit when neither the live entry nor the remembered ..." and saw 3 of 300 STATUS_SINGLE_STEP exits). A next step:
log SetReturnBreak's result, whether DR0 is set after arming, and whether ReturnBreakHandler sees the hit, for the
lost round.

Regression introduced by #10666 (@WallisSkill): main before it delivered all 200 raises, main after it loses them.

Found while testing pthread_suspend_np, which does not use this path.

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:6068869663 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/12131). Originally posted by [2Swagger](https://github.com/2Swagger) on 2026-10-27T22:31:43Z. --- ### What happens On Windows, `sceKernelRaiseException` returns 0, but the handler never runs: the target keeps running (its loop counter keeps advancing) and no delivery arrives within 3 s. The first one or two raises usually arrive; a later one is lost. It happens when the target spends almost all its time in host code with `waitCount == 0`, as a thread looping on `sceKernelUsleep(200)` does: on Windows a sleep under TimedWait's 0.5 ms `LEAD_NANOS` busy-waits in `SleepUntil` (`YieldProcessor` loop) rather than an alertable wait, so the raise takes the host-code path added in #10666. ### Repro Windows 11, WinLibs GCC 15.2.0, Release, Intel i5-10500H; main f5fefb982. 1. Add this test as `core/libs/tests/GuestRaiseLostRepro.cpp` and register it in the Windows-only test block of `core/libs/prx/libkernel/CMakeLists.txt` like `guest_raise_in_host_code` (TIMEOUT 60, libkernel on PATH): ```cpp #include "prx/libkernel/include/Api.hpp" #include "prx/libkernel/Time/include/Time.hpp" #include "SceTypes.hpp" #include <atomic> #include <chrono> #include <cstdio> #include <cstdlib> #include <thread> static std::atomic<int> deliveries{0}; static std::atomic<bool> stop{false}; static std::atomic<unsigned long long> progress{0}; static void APS5_VABI Handler(int, void*) { deliveries.fetch_add(1); } static void* APS5_VABI Nap(void*) { while (!stop.load()) { sceKernelUsleep_nid_postfix(200); progress.fetch_add(1); } return nullptr; } int main() { if (sceKernelInstallExceptionHandler(30, reinterpret_cast<void*>(&Handler)) != 0) std::abort(); Pthread thread = nullptr; if (scePthreadCreate(&thread, nullptr, Nap, nullptr, "nap") != 0) std::abort(); while (progress.load() == 0) std::this_thread::yield(); for (int round = 0; round < 200; ++round) { const int before = deliveries.load(); if (sceKernelRaiseException(thread, 30) != 0) std::abort(); const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(3); while (deliveries.load() == before) { if (std::chrono::steady_clock::now() >= deadline) { std::fprintf(stderr, "LOST delivery in round %d (worker progress %llu, still advancing)\n", round, progress.load()); std::_Exit(2); } std::this_thread::yield(); } } stop.store(true); scePthreadJoin(thread, nullptr); std::printf("all 200 deliveries arrived\n"); return 0; } ``` 2. `cmake --build build --target guest_raise_lost_repro_tests` and `ctest --test-dir build -R ^guest_raise_lost_repro$ --output-on-failure`. ### Results - main f5fefb982 (with #10666): 5 of 5 runs fail after about 3 s with `LOST delivery in round 0` or `round 1` (worker progress about 14,700-14,800, still advancing). - #10666's head 273c8c256 before merging: 5 of 5 runs fail the same way. - main a120df348, before #10666 merged: 5 of 5 runs deliver all 200 (15.7-20.3 s, about 80 ms per raise: the old "redirect in host code after the 50 ms limit" behaviour #10666 removed as unsafe). ### What it affects - No existing test covers it: `guest_raise_in_host_code` and `guest_raise_leaves_host_code` pass, and use targets that return to guest code from longer or alertable waits. - Titles that raise exceptions at threads polling with short sleeps. Unity's GC thread-stop (the Graveyard Keeper 2 case #10666 fixed) raises at every thread and waits for all acknowledgements, so a lost delivery there would hang the stop; not observed on a title yet. ### Best guess at the cause (unverified) With the target in host code and `waitCount == 0`, delivery depends on the return-address breakpoint (DR0) firing at the return into guest code, with the deliverer thread's sampling as a fallback. The target is in guest code for only a few nanoseconds per 200 us, so sampling essentially never sees it there. A lost delivery therefore means the breakpoint did not fire or was declined: either `ArmReturnBreak`/`UnwindToGuest` did not arm it from the busy-wait frames (unwind stopped or the return site was rejected), or `ReturnBreakHandler` declined the hit (the review of #10666 noted it "can decline the DR0 hit when neither the live entry nor the remembered ..." and saw 3 of 300 `STATUS_SINGLE_STEP` exits). A next step: log `SetReturnBreak`'s result, whether DR0 is set after arming, and whether `ReturnBreakHandler` sees the hit, for the lost round. Regression introduced by #10666 (@WallisSkill): main before it delivered all 200 raises, main after it loses them. Found while testing pthread_suspend_np, which does not use this path.
Sign in to join this conversation.
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5#12131
No description provided.