Skip to content

fix: preserve open filehandle data when file is unlinked#391

Draft
Koan-Bot wants to merge 1 commit intocpan-authors:mainfrom
atoomic:koan.atoomic/fix-unlink-open-handles
Draft

fix: preserve open filehandle data when file is unlinked#391
Koan-Bot wants to merge 1 commit intocpan-authors:mainfrom
atoomic:koan.atoomic/fix-unlink-open-handles

Conversation

@Koan-Bot
Copy link
Copy Markdown
Contributor

@Koan-Bot Koan-Bot commented May 5, 2026

What

Open filehandles now survive unlink() — reads/writes continue working until close.

Why

POSIX guarantees that open file descriptors survive unlink (data persists until all fds are closed). Previously, unlink() immediately cleared {'contents'} on the mock object, causing any open handle to see EOF on read or lose data on write. This violated basic Unix semantics that many programs rely on (temp files, log rotation, etc.).

How

Before clearing contents in unlink(), detect active filehandles via the mock's {'fhs'} list. For each live handle, replace the weak data reference with a strong reference to a detached hashref containing the file contents. The handles then operate on their own private copy, independent of the mock's existence state.

Testing

New t/unlink_open_handle.t with 5 subtests:

  • Read after unlink preserves data
  • Readline after unlink (multi-line, partial read before unlink)
  • Write after unlink (read back from same handle)
  • Multiple handles survive independently
  • tell/eof work correctly after unlink

Full suite passes.

🤖 Generated with Claude Code


Quality Report

Changes: 2 files changed, 128 insertions(+)

Code scan: clean

Tests: passed (OK)

Branch hygiene: clean

Generated by Kōan post-mission quality pipeline

POSIX guarantees that open file descriptors survive unlink() — the file
data remains accessible until all handles are closed. Previously, unlink()
immediately cleared contents, causing reads on open handles to return EOF.

Fix: before clearing contents, detach any open filehandles by giving them
a strong reference to a private copy of the data. Handles then operate
independently of the mock's "exists" state.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant