Repository navigation
Close original standard descriptors and keep C++ streams attached - #579
perfloop-agent wants to merge 2 commits into
Conversation
3cb5fb0 to
2a70743
Compare
|
@perfloop-agent The actual fix is a bit more involved. We need to create a new Stream type to hold state. For example, streams must remember the floating point formatting, string padding, etc. |
Signed-off-by: Perfloop Agent <agent@perfloop.ai> Signed-off-by: Tomás Senart <ts@perfloop.ai>
Signed-off-by: Perfloop Agent <agent@perfloop.ai> Signed-off-by: Tomás Senart <ts@perfloop.ai>
2a70743 to
a5c0693
Compare
|
The stream wrapper now separates the mutable stream object from its I/O handle, so formatting and error state can live on the object. References and copied or returned iterators keep that same object instead of duplicating handles. Floating-point formatting and padding remain outside this PR. |
|
@perfloop-agent solve the merge conflict |
|
I can’t safely port this implementation unchanged: it can close a descriptor while Rust’s standard streams still lend it out. Registry locking cannot protect those independent loans, so I propose making ownership of the original standard descriptors an explicit unsafe boundary rather than exposing their closure as an unrestricted safe Rust operation. That requires revisiting the requested safe close API; would that direction be acceptable? |
|
@nunoplopes This is not a plain conflict. After the rebase, closing fd 0, 1 or 2 for real would invalidate borrows that libcc2rs and Rust's standard streams still hold, and the registry cannot see the standard-stream borrows. That would break the safe API. Without it, close(1) returns 0 but the descriptor stays open, so the next open() does not get 1 back as it does natively. Two options: (a) accept that difference and keep the safe API, or (b) add an explicit unsafe ownership boundary for fd 0–2 so close really closes them. Which do you prefer? If neither fits, I can close this PR. |
Summary
Closing a standard descriptor in refcount output now releases the OS descriptor with that number, so open can reuse it. C++ standard streams stay attached to the original descriptor and return I/O errors after close instead of panicking or using a duplicate.
CppStreamas a mutable stream object separate from its I/O handle, leaving a home for future formatting and error state.Generated refcount code remains safe Rust. The registry remains thread-local, with its existing
ub: bad fdpolicy for unregistered numbers, including a first close after another thread has closed that descriptor. This is not cross-thread ownership tracking.Floating-point formatting, padding, and sticky error flags remain unimplemented; insertion expressions still discard returned I/O errors. Application exposure and runtime performance remain unmeasured.
Testing
8 recorded checks pass with this change.
The runtime runner passes 71 unit tests, with no failures or ignored tests, and reports no doctests. The scoped lit runner passes 44 cases, with no failures or skips.
tests/unit/fd_close_standard.c compares close/open/read and descriptor-number reuse with native C. The unsafe model's descriptor-close behavior is unchanged.
tests/unit/iostream_closed.cpp compares output after closing stdout and stderr with native C++. tests/unit/stdcopy_ostream.cpp exercises copied and returned output iterators and reads the file back. tests/unit/stdcopy_istream.cpp checks binary file-to-string reads through temporary and copied input iterators. The runtime's
file_stream_iotest covers binary I/O and release of the owned file while stream references remain.The macOS
unit/max.cpp/refcountgenerated-output check remains owned by target CI.Case and evidence
Made by Perfloop. Human sponsor: Tomás Senart.