Skip to content

Comments

fix: support multiple file handles to the same mocked file (#27)#202

Open
Koan-Bot wants to merge 1 commit intocpanel:masterfrom
atoomic:koan.atoomic/fix-multi-handle
Open

fix: support multiple file handles to the same mocked file (#27)#202
Koan-Bot wants to merge 1 commit intocpanel:masterfrom
atoomic:koan.atoomic/fix-multi-handle

Conversation

@Koan-Bot
Copy link
Contributor

Summary

Fixes #27 — allows opening multiple independent file handles to the same mocked file.

Problem

The %files_being_mocked hash tracked a single fh per mocked path. Opening a second handle to the same file overwrote the first, and closing any handle cleared tracking entirely.

Solution

  • Replace single {'fh'} with {'fhs'} arrayref of weak references
  • Each open/sysopen pushes to the array instead of overwriting
  • CLOSE removes only the specific handle (matched by tied object identity)
  • _fh_to_file() scans all entries, cleaning dead weakrefs during traversal
  • Directory handles (opendir/closedir) are unchanged — they use a separate pattern

Test plan

  • New t/multi_handle.t with 4 test groups:
    • Two read handles reading independently
    • Read + append handles sharing contents
    • Handle tracking (fhs array count after open/close)
    • Sysopen dual handles
  • Updated t/readline.t assertion for new fhs structure

🤖 Generated with Claude Code

@atoomic atoomic marked this pull request as ready for review February 22, 2026 16:49
@Koan-Bot Koan-Bot force-pushed the koan.atoomic/fix-multi-handle branch 2 times, most recently from f83c57e to 00ac6a1 Compare February 24, 2026 07:23
Replace single 'fh' tracking with 'fhs' array of weak references,
allowing multiple independent handles (each with its own tell position)
to be open on the same mocked file simultaneously.

- MockFile.pm: _fh_to_file scans fhs array, __open/__sysopen push to it
- FileHandle.pm: CLOSE removes only the specific handle via tied identity
- New test t/multi_handle.t covers dual read, read+write, sysopen cases
- Updated t/readline.t for new fhs structure

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@Koan-Bot Koan-Bot force-pushed the koan.atoomic/fix-multi-handle branch from 00ac6a1 to 3e255b8 Compare February 24, 2026 21:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Can't handle 2 file handles against the same file.

1 participant