Skip to content

Commit

Permalink
Replace handle atomically in IO::FileDescriptor#close on Windows (c…
Browse files Browse the repository at this point in the history
…rystal-lang#15165)

The spec introduced in crystal-lang#14698 fails on Windows very rarely (e.g. https://github.com/crystal-lang/crystal/actions/runs/11681506288/job/32532033082, https://github.com/crystal-lang/crystal/actions/runs/11642959689/job/32449149741). This PR seems to fix that; other platforms already do the same in `#system_close`, and double close remains an error.
  • Loading branch information
HertzDevil authored and CTC97 committed Nov 9, 2024
1 parent 36866ce commit 84f4c62
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/crystal/system/win32/file_descriptor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,11 @@ module Crystal::System::FileDescriptor
end

def file_descriptor_close(&)
if LibC.CloseHandle(windows_handle) == 0
# Clear the @volatile_fd before actually closing it in order to
# reduce the chance of reading an outdated handle value
handle = LibC::HANDLE.new(@volatile_fd.swap(LibC::INVALID_HANDLE_VALUE.address))

if LibC.CloseHandle(handle) == 0
yield
end
end
Expand Down

0 comments on commit 84f4c62

Please sign in to comment.