fix(libkernel): close an unopened descriptor on Windows without the invalid-parameter report #3923

Merged
reyani-dev merged 2 commits from upstream-pr/3923 into main 2026-10-12 03:10:27 +00:00
reyani-dev commented 2026-10-11 19:10:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by reyani-dev on 2026-10-11T19:10:45Z.


What

On Windows, close_nid_postfix calls _close(d) directly. For a descriptor that isn't open, the UCRT runs the process's invalid-parameter handler before returning EBADF. In a relinked title that handler is libc's crash reporter (CrashReport.cpp), so a plain close() of an already-closed descriptor (legal POSIX: -1/EBADF) prints FATAL: UCRT invalid parameter on thread …, then every thread's stack, and the title carries on. close now runs _close under the thread-local no-op handler (WithoutParameterHandler), as dup and dup2 beside it already do. The return value and errno are unchanged.

Title: ProsperoLight (PPSA99002) prints that report on every launcher refresh. On main a11c8f7f it stops earlier (pthread_attr_getscope, #3494, then poll on a pipe, #3584 / #3649). With those, its log (download0/prosperolight-launcher.log) shows:

[debug:0] [PL] launcher: first frame after 8193 ms, splash hidden=1
FATAL: UCRT invalid parameter on thread 47556
    #0 0x00007ff975fda9e7 libc.prx+0xa9e7
    #1 0x00007ff97606f492 libc.prx+0x9f492
    #2 0x000000013b44a440
  all threads:
  ...

The stack scan from #3887 placed it in fclose → _close (ucrtbase _close+0x41) called from libkernel.prx. With this change the report is gone (0 in a 60 s run, 2 before) and the launcher renders 1303 frames.

Tested

Windows 11 (26200), WinLibs GCC 15.2.0 r7, Release, main a11c8f7f:

  • guest_dup (BadDescriptors): close of a closed descriptor and of -1 fail with EBADF (9). On Windows the test installs its own process-wide invalid-parameter handler around those calls and requires it not to run. That fails with main's Stdio.cpp and passes with this change.
  • ctest -E "^agc_|video_out": 450/453. The 3 failures also fail on main on Windows: windows_exceptions (#3411), guest_kernel_sys (#3510) and guest_filesystem (#3518).
  • ProsperoLight on main + #3494 + a pipe-poll fix, with and without this change (above).
  • tools/check_conventions.py: clean. tools/pr_overlap.py reports #3649, which adds File::ForgetPipeEnd(d); on the line above the _close call. That's a one-line rebase for whichever lands second, not the same change.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none (the title reaches it after #3494 and a pipe-poll fix)
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4821592021 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3923). Originally posted by [reyani-dev](https://github.com/reyani-dev) on 2026-10-11T19:10:45Z. --- ### What On Windows, `close_nid_postfix` calls `_close(d)` directly. For a descriptor that isn't open, the UCRT runs the process's invalid-parameter handler before returning `EBADF`. In a relinked title that handler is libc's crash reporter (`CrashReport.cpp`), so a plain `close()` of an already-closed descriptor (legal POSIX: -1/`EBADF`) prints `FATAL: UCRT invalid parameter on thread …`, then every thread's stack, and the title carries on. `close` now runs `_close` under the thread-local no-op handler (`WithoutParameterHandler`), as `dup` and `dup2` beside it already do. The return value and `errno` are unchanged. **Title:** ProsperoLight (PPSA99002) prints that report on every launcher refresh. On `main` a11c8f7f it stops earlier (`pthread_attr_getscope`, #3494, then `poll` on a pipe, #3584 / #3649). With those, its log (`download0/prosperolight-launcher.log`) shows: ``` [debug:0] [PL] launcher: first frame after 8193 ms, splash hidden=1 FATAL: UCRT invalid parameter on thread 47556 #0 0x00007ff975fda9e7 libc.prx+0xa9e7 #1 0x00007ff97606f492 libc.prx+0x9f492 #2 0x000000013b44a440 all threads: ... ``` The stack scan from #3887 placed it in `fclose` → `_close` (ucrtbase `_close+0x41`) called from `libkernel.prx`. With this change the report is gone (0 in a 60 s run, 2 before) and the launcher renders 1303 frames. ### Tested Windows 11 (26200), WinLibs GCC 15.2.0 r7, Release, `main` a11c8f7f: - `guest_dup` (`BadDescriptors`): `close` of a closed descriptor and of -1 fail with `EBADF` (9). On Windows the test installs its own process-wide invalid-parameter handler around those calls and requires it not to run. That fails with main's `Stdio.cpp` and passes with this change. - `ctest -E "^agc_|video_out"`: 450/453. The 3 failures also fail on `main` on Windows: `windows_exceptions` (#3411), `guest_kernel_sys` (#3510) and `guest_filesystem` (#3518). - ProsperoLight on `main` + #3494 + a pipe-`poll` fix, with and without this change (above). - `tools/check_conventions.py`: clean. `tools/pr_overlap.py` reports #3649, which adds `File::ForgetPipeEnd(d);` on the line above the `_close` call. That's a one-line rebase for whichever lands second, not the same change. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none (the title reaches it after #3494 and a pipe-`poll` fix) - [x] AI-assisted: yes (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Dextroid17 commented 2026-10-12 00:50:06 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-12T00:50:06Z.


The fix follows the file's own established pattern exactly - dup/dup2 in this same file already wrap their libc calls in WithoutParameterHandler for the identical reason (suppressing the UCRT invalid-parameter handler on a bad descriptor), so this isn't a new mechanism, just applying an existing one to a call site that was missed. The test's approach (install a recording handler, assert it never fires) directly proves the claimed mechanism rather than just testing the return value, which is the right level for this kind of host-API-suppression bug.

Written with AI assistance.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6115796469 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3923#issuecomment-6115796469). Originally posted by [Dextroid17](https://github.com/Dextroid17) on 2026-10-12T00:50:06Z. --- The fix follows the file's own established pattern exactly - `dup`/`dup2` in this same file already wrap their libc calls in `WithoutParameterHandler` for the identical reason (suppressing the UCRT invalid-parameter handler on a bad descriptor), so this isn't a new mechanism, just applying an existing one to a call site that was missed. The test's approach (install a recording handler, assert it never fires) directly proves the claimed mechanism rather than just testing the return value, which is the right level for this kind of host-API-suppression bug. Written with AI assistance.
babyhuey commented 2026-10-12 01:24:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by babyhuey on 2026-10-12T01:24:54Z.


A catalog title hits this. On Windows 11 (MinGW GCC 15.2), Cloud Play (PPSA99600, on main 7a84b222 with #3798 merged so it gets past getservbyname) prints FATAL: UCRT invalid parameter with a backtrace from close_nid_postfix → _close:

  • The trigger is a single close(32) on a descriptor that is no longer open, between two of its many open("/download0/debug.log.tmp", O_WRONLY|O_CREAT|O_TRUNC) calls.
  • The run then continues to its real stop: sysctl_nid_postfix: unsupported MIB 1.37.

With this PR merged on top (6eacac2e, clean):

  • The invalid-parameter block is gone, and Cloud Play still reaches the same sysctl stop.
  • guest_dup passes 5/5 (--repeat until-fail:5), with no warnings in Stdio.cpp or GuestDup.cpp.
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6116102016 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3923#issuecomment-6116102016). Originally posted by [babyhuey](https://github.com/babyhuey) on 2026-10-12T01:24:54Z. --- A catalog title hits this. On Windows 11 (MinGW GCC 15.2), Cloud Play (PPSA99600, on `main` 7a84b222 with #3798 merged so it gets past `getservbyname`) prints `FATAL: UCRT invalid parameter` with a backtrace from `close_nid_postfix` → `_close`: - The trigger is a single `close(32)` on a descriptor that is no longer open, between two of its many `open("/download0/debug.log.tmp", O_WRONLY|O_CREAT|O_TRUNC)` calls. - The run then continues to its real stop: `sysctl_nid_postfix: unsupported MIB 1.37`. With this PR merged on top (6eacac2e, clean): - The invalid-parameter block is gone, and Cloud Play still reaches the same `sysctl` stop. - `guest_dup` passes 5/5 (`--repeat until-fail:5`), with no warnings in `Stdio.cpp` or `GuestDup.cpp`.
github-actions[bot] commented 2026-10-12 02:38:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-12T02:38:35Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
#3649 core/libs/prx/libkernel/File/src/Stdio.cpp L472-473
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6116756051 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3923#issuecomment-6116756051). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-12T02:38:35Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #3649 | `core/libs/prx/libkernel/File/src/Stdio.cpp` [L472-473](https://github.com/boykopovar/AnyPS5/blob/34a16b7f087d923b5ce9069c8d2382d9ffecd421/core/libs/prx/libkernel/File/src/Stdio.cpp#L472-L473) | | |
Sign in to join this conversation.
No reviewers
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!3923
No description provided.