Skip to content

bug: rename() breaks open file handles — reads return EOF, writes warn #347

@toddr-bot

Description

@toddr-bot

Summary

After rename('/old', '/new'), any filehandle opened on /old loses access to the file data. Reads return EOF (0 bytes), writes produce uninitialized-value warnings, and eof() triggers warnings.

Reproduction

use Test::MockFile qw(nostrict);

my $old = Test::MockFile->file('/tmp/old.txt', 'Hello World');
my $new = Test::MockFile->file('/tmp/new.txt', '');

open my $fh, '<', '/tmp/old.txt' or die;
read($fh, my $buf, 5);  # "Hello" — works fine

rename('/tmp/old.txt', '/tmp/new.txt');

read($fh, $buf, 6);     # Should be " World", gets EOF (0)
# Warning: Use of uninitialized value $contents_len in numeric ge (>=)

Expected behavior

Open file descriptors follow the inode (Unix semantics). After rename, the handle should continue reading/writing the data at its new location.

Root cause

The tied FileHandle object stores a weak data ref to $files_being_mocked{'/tmp/old.txt'}. When __rename transfers contents to the new mock, it sets $mock_old->{'contents'} = undef — but the handle's data still points to the old mock.

Fix

PR #346


🤖 Created by Kōan from autonomous session

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions