Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support IO::FileDescriptor#flock_* on non-blocking files on Windows #14943

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 35 additions & 31 deletions spec/std/file_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1232,46 +1232,50 @@ describe "File" do
end
end

it "#flock_shared" do
File.open(datapath("test_file.txt")) do |file1|
File.open(datapath("test_file.txt")) do |file2|
file1.flock_shared do
file2.flock_shared(blocking: false) { }
{true, false}.each do |blocking|
context "blocking: #{blocking}" do
it "#flock_shared" do
File.open(datapath("test_file.txt"), blocking: blocking) do |file1|
File.open(datapath("test_file.txt"), blocking: blocking) do |file2|
file1.flock_shared do
file2.flock_shared(blocking: false) { }
end
end
end
end
end
end

it "#flock_shared soft blocking fiber" do
File.open(datapath("test_file.txt")) do |file1|
File.open(datapath("test_file.txt")) do |file2|
done = Channel(Nil).new
file1.flock_exclusive
it "#flock_shared soft blocking fiber" do
File.open(datapath("test_file.txt"), blocking: blocking) do |file1|
File.open(datapath("test_file.txt"), blocking: blocking) do |file2|
done = Channel(Nil).new
file1.flock_exclusive

spawn do
file1.flock_unlock
done.send nil
end
spawn do
file1.flock_unlock
done.send nil
end

file2.flock_shared
done.receive
file2.flock_shared
done.receive
end
end
end
end
end

it "#flock_exclusive soft blocking fiber" do
File.open(datapath("test_file.txt")) do |file1|
File.open(datapath("test_file.txt")) do |file2|
done = Channel(Nil).new
file1.flock_exclusive
it "#flock_exclusive soft blocking fiber" do
File.open(datapath("test_file.txt"), blocking: blocking) do |file1|
File.open(datapath("test_file.txt"), blocking: blocking) do |file2|
done = Channel(Nil).new
file1.flock_exclusive

spawn do
file1.flock_unlock
done.send nil
end
spawn do
file1.flock_unlock
done.send nil
end

file2.flock_exclusive
done.receive
file2.flock_exclusive
done.receive
end
end
end
end
end
Expand Down
57 changes: 39 additions & 18 deletions src/crystal/system/win32/file_descriptor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -211,41 +211,62 @@ module Crystal::System::FileDescriptor
end

private def flock(exclusive, retry)
flags = LibC::LOCKFILE_FAIL_IMMEDIATELY
flags = 0_u32
flags |= LibC::LOCKFILE_FAIL_IMMEDIATELY if !retry || system_blocking?
flags |= LibC::LOCKFILE_EXCLUSIVE_LOCK if exclusive

handle = windows_handle
if retry
if retry && system_blocking?
until lock_file(handle, flags)
sleep 0.1
end
else
lock_file(handle, flags) || raise IO::Error.from_winerror("Error applying file lock: file is already locked")
lock_file(handle, flags) || raise IO::Error.from_winerror("Error applying file lock: file is already locked", target: self)
end
end

private def lock_file(handle, flags)
# lpOverlapped must be provided despite the synchronous use of this method.
overlapped = LibC::OVERLAPPED.new
# lock the entire file with offset 0 in overlapped and number of bytes set to max value
if 0 != LibC.LockFileEx(handle, flags, 0, 0xFFFF_FFFF, 0xFFFF_FFFF, pointerof(overlapped))
true
else
winerror = WinError.value
if winerror == WinError::ERROR_LOCK_VIOLATION
false
IOCP::OverlappedOperation.run(handle) do |operation|
result = LibC.LockFileEx(handle, flags, 0, 0xFFFF_FFFF, 0xFFFF_FFFF, operation)

if result == 0
case error = WinError.value
when .error_io_pending?
# the operation is running asynchronously; do nothing
when .error_lock_violation?
# synchronous failure
return false
else
raise IO::Error.from_os_error("LockFileEx", error, target: self)
end
else
raise IO::Error.from_os_error("LockFileEx", winerror, target: self)
return true
end

operation.wait_for_result(nil) do |error|
raise IO::Error.from_os_error("LockFileEx", error, target: self)
end

true
end
end

private def unlock_file(handle)
# lpOverlapped must be provided despite the synchronous use of this method.
overlapped = LibC::OVERLAPPED.new
# unlock the entire file with offset 0 in overlapped and number of bytes set to max value
if 0 == LibC.UnlockFileEx(handle, 0, 0xFFFF_FFFF, 0xFFFF_FFFF, pointerof(overlapped))
raise IO::Error.from_winerror("UnLockFileEx")
IOCP::OverlappedOperation.run(handle) do |operation|
result = LibC.UnlockFileEx(handle, 0, 0xFFFF_FFFF, 0xFFFF_FFFF, operation)

if result == 0
error = WinError.value
unless error.error_io_pending?
raise IO::Error.from_os_error("UnlockFileEx", error, target: self)
end
else
return
end

operation.wait_for_result(nil) do |error|
raise IO::Error.from_os_error("UnlockFileEx", error, target: self)
end
end
end

Expand Down
Loading