From 4e287fd01d5057d0345183b6845dcc58683a46e4 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sat, 21 Feb 2026 20:42:08 +0200 Subject: fix: close_range path resolution - lookup fd before deleting from map Move e.files[fd] lookup before the close_range fd cleanup loop in the FdEvent handler so the file path is resolved while the fd still exists in the map. Previously, close_range events always fell through to file.NewFdWithPid() which couldn't resolve the path since the fd was already closed. Also add unit test assertion verifying ep.File resolves to the correct filename for close_range events. Task: 349 Amp-Thread-ID: https://ampcode.com/threads/T-019c8180-1da4-7048-9200-ae93bdd90cab Co-authored-by: Amp --- internal/eventloop.go | 16 ++++++++-------- internal/eventloop_test.go | 6 ++++++ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/internal/eventloop.go b/internal/eventloop.go index 169f20f..010b892 100644 --- a/internal/eventloop.go +++ b/internal/eventloop.go @@ -258,6 +258,14 @@ func (e *eventLoop) tracepointExited(exitEv event.Event, ch chan<- *event.Pair) case *FdEvent: fd := ep.EnterEv.(*FdEvent).Fd + if file_, ok := e.files[fd]; ok { + ep.File = file_ + if ep.Is(SYS_ENTER_CLOSE) { + delete(e.files, fd) + } + } else { + ep.File = file.NewFdWithPid(fd, v.Pid) + } if ep.Is(SYS_ENTER_CLOSE_RANGE) { // close_range provides (first, last), but fd_event only carries the first // argument, so we approximate by closing all tracked fds >= first. @@ -270,14 +278,6 @@ func (e *eventLoop) tracepointExited(exitEv event.Event, ch chan<- *event.Pair) } } } - if file_, ok := e.files[fd]; ok { - ep.File = file_ - if ep.Is(SYS_ENTER_CLOSE) { - delete(e.files, fd) - } - } else { - ep.File = file.NewFdWithPid(fd, v.Pid) - } ep.Comm = e.comm(ep.EnterEv.GetTid()) if !e.filter.eventPair(ep) { ep.Recycle() diff --git a/internal/eventloop_test.go b/internal/eventloop_test.go index 7daa32b..60047fe 100644 --- a/internal/eventloop_test.go +++ b/internal/eventloop_test.go @@ -487,6 +487,12 @@ func makeCloseRangeEventTestData(t *testing.T) (td testData) { if !exitCloseRange.Equals(ep.ExitEv) { t.Errorf("Expected '%v' but got '%v'", exitCloseRange, ep.ExitEv) } + // close_range's fd is the first fd in the range (fd2); its path must resolve + if ep.File == nil { + t.Errorf("Expected file to be set for close_range event") + } else if ep.File.Name() != filename2 { + t.Errorf("Expected file name '%v' but got '%v'", filename2, ep.File.Name()) + } verifyFileDescriptor(t, el, fd1, filename1) verifyFdNotTracked(t, el, fd2) -- cgit v1.2.3