fix: preserve file contents for open handles on unlink#328
Draft
toddr-bot wants to merge 2 commits intocpan-authors:mainfrom
Draft
fix: preserve file contents for open handles on unlink#328toddr-bot wants to merge 2 commits intocpan-authors:mainfrom
toddr-bot wants to merge 2 commits intocpan-authors:mainfrom
Conversation
Demonstrates that unlinking a mocked file while handles are open loses the file contents and triggers warnings. Real Unix preserves data for open handles until the last fd is closed. Tests: read, eof, multiple handles, write, sysread, getc — all fail against current code (9/12 failures). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Unix semantics: unlink removes the directory entry but open file descriptors continue to see the original data until closed. Previously, unlink set contents to undef immediately, breaking reads through open handles and triggering "uninitialized value" warnings in EOF/READLINE. The fix snapshots contents into each open FileHandle before clearing the mock's contents. FileHandle methods now use _contents_ref() which falls back to this snapshot when the mock's contents are gone. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Open filehandles now retain access to file data after
unlink(), matching Unix semantics.Why
On real Unix,
unlinkremoves the directory entry but open fds continue to read/write the file data until closed. The mock was settingcontentstoundefimmediately, causing:undefinstead of the original dataeof()warnings: "Use of uninitialized value in numeric ge"sysreadreturning 0 bytes instead of the file contentsThis matters for any code that opens a file, unlinks it (e.g., for atomicity or temp file patterns), and continues using the handle.
How
Before clearing
contentsinunlink(), each openFileHandlesnapshots the data into_orphaned_contents. A new_contents_ref()method inFileHandlereturns a reference to the effective contents — live data when available, orphaned snapshot after unlink.Two-commit structure: first commit adds failing tests (9/12 fail), second commit adds the fix (all pass).
Testing
fh-ref-leak.t)Generated by Kōan
Quality Report
Changes: 3 files changed, 168 insertions(+), 16 deletions(-)
Code scan: clean
Tests: skipped
Branch hygiene: clean
Generated by Kōan post-mission quality pipeline