fix(libkernel): fail a write to a pipe without a reader with EPIPE on Windows #11040

Merged
harisibrar1 merged 1 commit from upstream-pr/11040 into main 2026-10-26 21:36:09 +00:00
harisibrar1 commented 2026-10-25 14:54:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-25T14:54:44Z.


What

On Windows, a write to a pipe whose read end is closed fails with EINVAL instead of EPIPE.

pipe gives the guest a host anonymous pipe. When the reader is gone, WriteFile fails with ERROR_NO_DATA (232). The UCRT has no errno mapping for that error and falls back to EINVAL. A standalone check with the same calls (_pipe, _close of the read end, _write) gives _write=-1 errno=22 _doserrno=232, with GetFileType reporting FILE_TYPE_PIPE. FreeBSD 11.4's pipe_write returns EPIPE once the read side is lost. So a guest that checks for EPIPE to notice its peer has gone gets an "invalid argument" instead.

The two Windows write paths now map the error:

  • NativeWrite in Open.cpp (write, _write, sceKernelWrite)
  • NativeTransferBuffer in Stdio.cpp (writev, sceKernelWritev)

When _write fails on a FILE_TYPE_PIPE handle with ERROR_NO_DATA or ERROR_BROKEN_PIPE, errno becomes EPIPE. A full non-blocking pipe isn't affected: it reports success with 0 bytes, which main already maps to EAGAIN on the line above.

dofilewrite also sends SIGPIPE. Nothing is raised on Windows, as main already documents for sockets. The new debt entry records that.

Tested

Windows 11, WinLibs GCC 15.2.0 r7, full Release build on main 3a2c51afd2 with this change: no failed steps.

  • New Windows-only case in GuestPosixScalarIo.cpp: after closing the read end, write and _write fail with EPIPE (32), sceKernelWrite and sceKernelWritev return 0x80020020, and writev fails with EPIPE.
  • guest_posix_scalar_io, guest_file_vector, guest_kernel_sys and guest_select pass, 3 runs of 4/4. guest_kernel_sys_pselect couldn't start on this machine in that run: Smart App Control blocked the executable. It doesn't write to a closed pipe.
  • Negative control: with main's Open.cpp and Stdio.cpp, guest_posix_scalar_io fails at the first new check with guest errno 22 (2 runs).
  • tools/check_conventions.py --base origin/main passes.
  • Not tested: Linux and macOS. The change is in _WIN32-only code; Linux and macOS hosts already return EPIPE.

Overlap

  • pr_overlap reports #11002 (@SandstromPL, which lets a guest ignore or catch SIGPIPE on Linux) on the neighbouring debt line. The new entry says the guest can't ignore the host's SIGPIPE on Linux "as for sockets above". If #11002 lands first, I'll update that clause to match.
  • #7201, #9019, #9161 and #9823 share Open.cpp/Stdio.cpp but conflict with main and don't touch these lines.

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 under Silent stubs in their library's technical debt file
  • 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 (debt wording with #11002 above)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5005804212 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11040). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-25T14:54:44Z. --- ### What On Windows, a write to a pipe whose read end is closed fails with `EINVAL` instead of `EPIPE`. `pipe` gives the guest a host anonymous pipe. When the reader is gone, `WriteFile` fails with `ERROR_NO_DATA` (232). The UCRT has no `errno` mapping for that error and falls back to `EINVAL`. A standalone check with the same calls (`_pipe`, `_close` of the read end, `_write`) gives `_write=-1 errno=22 _doserrno=232`, with `GetFileType` reporting `FILE_TYPE_PIPE`. FreeBSD 11.4's [`pipe_write`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/sys_pipe.c#L1062-L1069) returns `EPIPE` once the read side is lost. So a guest that checks for `EPIPE` to notice its peer has gone gets an "invalid argument" instead. The two Windows write paths now map the error: - `NativeWrite` in `Open.cpp` (`write`, `_write`, `sceKernelWrite`) - `NativeTransferBuffer` in `Stdio.cpp` (`writev`, `sceKernelWritev`) When `_write` fails on a `FILE_TYPE_PIPE` handle with `ERROR_NO_DATA` or `ERROR_BROKEN_PIPE`, `errno` becomes `EPIPE`. A full non-blocking pipe isn't affected: it reports success with 0 bytes, which main already maps to `EAGAIN` on the line above. [`dofilewrite`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/sys_generic.c#L597-L601) also sends `SIGPIPE`. Nothing is raised on Windows, as main already documents for sockets. The new debt entry records that. ### Tested Windows 11, WinLibs GCC 15.2.0 r7, full Release build on main 3a2c51afd2 with this change: no failed steps. - New Windows-only case in `GuestPosixScalarIo.cpp`: after closing the read end, `write` and `_write` fail with `EPIPE` (32), `sceKernelWrite` and `sceKernelWritev` return `0x80020020`, and `writev` fails with `EPIPE`. - `guest_posix_scalar_io`, `guest_file_vector`, `guest_kernel_sys` and `guest_select` pass, 3 runs of 4/4. `guest_kernel_sys_pselect` couldn't start on this machine in that run: Smart App Control blocked the executable. It doesn't write to a closed pipe. - Negative control: with main's `Open.cpp` and `Stdio.cpp`, `guest_posix_scalar_io` fails at the first new check with guest errno 22 (2 runs). - `tools/check_conventions.py --base origin/main` passes. - Not tested: Linux and macOS. The change is in `_WIN32`-only code; Linux and macOS hosts already return `EPIPE`. ### Overlap - `pr_overlap` reports #11002 (@SandstromPL, which lets a guest ignore or catch `SIGPIPE` on Linux) on the neighbouring debt line. The new entry says the guest can't ignore the host's `SIGPIPE` on Linux "as for sockets above". If #11002 lands first, I'll update that clause to match. - #7201, #9019, #9161 and #9823 share `Open.cpp`/`Stdio.cpp` but conflict with main and don't touch these lines. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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 under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [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 (debt wording with #11002 above) - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-10-25 15:17:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T15:17:26Z.

Review: APPROVED


Reviewed exact head aebd8881d4. Both Windows write paths map ERROR_NO_DATA and ERROR_BROKEN_PIPE to EPIPE only after a failed write and only for FILE_TYPE_PIPE handles, leaving other errors and non-pipe descriptors untouched. The added Windows regression covers scalar, underscore, kernel, and vector writes. I compiled Open.cpp, Stdio.cpp, and GuestPosixScalarIo.cpp with MinGW syntax-only; MinGW needed a compatibility define for the unrelated missing _fflush_nolock declaration. The author reports Windows negative-control and repeat runs. Hosted product jobs are skipped by path policy, so I did not claim a Windows runtime test here.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5643828491 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11040#pullrequestreview-5643828491). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T15:17:26Z. **Review:** APPROVED --- Reviewed exact head aebd8881d47522350e91b23183068b9c8c406518. Both Windows write paths map ERROR_NO_DATA and ERROR_BROKEN_PIPE to EPIPE only after a failed write and only for FILE_TYPE_PIPE handles, leaving other errors and non-pipe descriptors untouched. The added Windows regression covers scalar, underscore, kernel, and vector writes. I compiled Open.cpp, Stdio.cpp, and GuestPosixScalarIo.cpp with MinGW syntax-only; MinGW needed a compatibility define for the unrelated missing _fflush_nolock declaration. The author reports Windows negative-control and repeat runs. Hosted product jobs are skipped by path policy, so I did not claim a Windows runtime test here.
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!11040
No description provided.