fix(libc): reopen a closed stream in freopen instead of aborting #9763
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/9763"
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?
Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-22T13:27:00Z.
What
freopenon a stream that had been closed aborted the process:FileStream::Reopenstarts withGetHandle(), which throwsFileStream: closed streamwhen the handle is gone. PPSA99630 (OpenMW, the Morrowind port in the homebrew catalog,v1.0.1, bring-up thread #9762) does that when/download0cannot be used: its firstfreopenofstdoutfails and leaves the stream closed, and a laterfreopenof it dies onmainbefore the title prints anything:(
0x35ddaintolibc.prx,freopen_nid_postfix.) FreeBSD'sfreopenkeeps a closedFILEand reopens it: withfp->_flags == 0it "holds on to it", opens the path on a new descriptor and makesfpthat stream (freopen.c, releng/11.4). A failed open leaves it closed, with the open'serrno.Reopennow does the same for a closed stream: it opens the path through the caller's opener and adopts the handle, or fails with the open's error (ENOENTfor an empty path, asopen("")gives there). A path the resolver rejects before any open (an overlong name, an unavailable mount) used to reachClose()on the closed stream and throw the same way;freopennow closes only a stream that is open there and returns the resolver's error. A null filename on a closed stream answersEBADF, as a PS5 does (an open stream keeps answeringENOTSUP, since mode-only reopening is not supported here). The guest state reset and descriptor bookkeeping that the open-stream path did at its end moved intoAdopt, which both paths use. A closed stream whose reopen fails keeps answeringEBADF, as before. Only the standard streams can reach this, since a dynamic stream is freed by itsfclose.The debt entry records the differences from the console: the new descriptor is whatever the host hands out, where a PS5 takes the lowest free one, and a write to the closed stream answers
EBADFhere (as onmain), where the console'sfputs/fputcleaveerrnounchanged and itsfprintfgivesEINVAL.Tested
fclose(stdout)orfclose(stderr),freopeninto a missing directory and of""givesENOENT, of a 320-byte pathENAMETOOLONG, of a null filenameEBADF; a good path returns the sameFILE*on the lowest free descriptor and reads back what was written. Verify run on144298bfca:guest_standard_streams*pass; its AGC failures aremain's on that runner.maina6dcea9279with no usable/download0dies with the lines above; with this change it runs through to its own data check and exits 0, as it does when/download0works (details on #9762). With a complete package the title doesn't reach this path, so this isUnblocks: none; the trace is the title's, the behaviour is FreeBSD's.guest_standard_streamsgainsCheckClosedStreamReopen(a closedtmpfilestream: a failed reopen into a missing directory givesENOENT, one with a 300-byte name givesENAMETOOLONGfrom the resolver, a null filename givesEINVAL, the stream staysEBADFafter each; then a reopen for writing, a write, a reopen for reading and a read, and the file holds the text) and the--reopen-closed-stdoutcase (fclose(stdout),printffails withEBADF, the overlong reopen fails withENAMETOOLONGandprintfstillEBADF,freopenreturnsstdout,printfandputsland in the file). Hosted Linux runner: the first verify run passed those tests on the diff before the resolver fix; the run on1c2eed30a2passes them too (Ubuntu 24.04, GCC, Release).ci/macos-series-2integration tree:--reopen-closed-stdout(with the overlong-path check),--closed-stdoutand--exclusive-stdoutpass; the plain case runsCheckClosedStreamReopenand then stops at that tree's pre-existingCheckFlushWhileReadingfailure (anfflushon a reading stream changes the next read there, before and after this change; noted for #1).1c2eed30a2merged ontomain11f976b27c: the fourguest_standard_streams*tests pass 5 of 5, including the new case andCheckClosedStreamReopen; withFileStream.hppandFileAndHeap.cpprestored frommainthe two closed-stream cases abort withwhat(): FileStream: closed stream. The full suite passes 1887 of 1895, the 8 failures being AGC tests that fail on that machine's AMD GPU onmain(#7451, #9056, #8310).check_conventions.pypasses.pr_overlap.pyreports no conflicting lines; the shared files it could not compare belong to PRs that conflict withmain.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-22T13:34:42Z.
Rebased onto current
main(8b3e39832a): heada783c27f41, same diff.Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-22T13:40:44Z.
Review: COMMENTED
Checked against main
8b3e39832aand FreeBSD 11.4 lib/libc/stdio/freopen.c:Reopen→GetHandle()on a closed stream, so it isn't duplicated. The new branch matches FreeBSD's_flags == 0path: it opens on a new descriptor, and on failure the stream stays free with the open's errno.ResolvePathError_nid_no_patchfails,freopen_nid_postfix(FileAndHeap.cpp:365-367) callsstream->Close()for anything that isn'tIsHostStandard(). A closed stream has a null handle, so it lands there, andClose()→GetHandle()throwsFileStream: closed stream. For example, afterfclose(stdout),freopen(<1024-byte path>, "w", stdout)still terminates, and so does a path under a blocked mount such as an unavailable/download0. FreeBSD returns NULL withENAMETOOLONG/ENOENTand leavesfpfree. Guarding that branch withstream->IsOpen()and adding the case toCheckClosedStreamReopenwould close it.EINVALwhenfpis closed, where main returns 45 before looking at the stream.Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T14:48:36Z.
Review: CHANGES_REQUESTED
The closed-stream reopen path still has a failure case on the current head. I verified the control flow against this commit; the existing test covers an open failure after path resolution, but not a path-resolution error.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T17:01:45Z.
Review: COMMENTED
The latest head
1c2eed30a2d18862352c02b79f6fc7046349fb47addresses both issues I raised: path-resolution failure only closes an open stream, and a null filename on a closed stream returnsEINVAL. The new ordinary-stream and stdout tests cover resolver errors,ENAMETOOLONG, failed writes, successful subsequent reopen, and retained host stdout behavior. The author reports the focused Linux suite passing on this head; current hosted relinker/register-context/write-watch checks also pass, while product Windows is skipped. Current-main conventions, diff-check and merge-tree pass. This branch is based on8b3e39832a, 32 commits behind currentmain(11f976b27c16ed91633f00039dba5dfd97dd7cc1); please refresh and rerun the stream tests before merge. I did not rerun them locally.