From f919f0cf4856826fb4da423cb5d2da5e21eb7097 Mon Sep 17 00:00:00 2001 From: Julien Portalier Date: Thu, 5 Sep 2024 12:28:20 +0200 Subject: [PATCH] Fix: `#file_descriptor_close` should set `@closed` (UNIX) --- src/crystal/system/unix/file_descriptor.cr | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/crystal/system/unix/file_descriptor.cr b/src/crystal/system/unix/file_descriptor.cr index 56a9eee80dd5..759802f4323e 100644 --- a/src/crystal/system/unix/file_descriptor.cr +++ b/src/crystal/system/unix/file_descriptor.cr @@ -121,6 +121,13 @@ module Crystal::System::FileDescriptor end def file_descriptor_close(&) : Nil + # It would usually be set by IO::Buffered#unbuffered_close but we sometimes + # close file descriptors directly (i.e. signal/process pipes) and the IO + # object wouldn't be marked as closed, leading IO::FileDescriptor#finalize + # to try to close the fd again (pointless) and lead to other issues if we + # try to do more cleanup in the finalizer (error) + @closed = true + # Clear the @volatile_fd before actually closing it in order to # reduce the chance of reading an outdated fd value _fd = @volatile_fd.swap(-1)