Fix incorrect download locking.

This commit is contained in:
Markus Reiter 2024-11-23 23:45:17 +01:00
parent f0c746a0b9
commit 4bd75d4235
No known key found for this signature in database
GPG Key ID: 245293B51702655B
3 changed files with 96 additions and 59 deletions

View File

@ -392,6 +392,7 @@ class CurlDownloadStrategy < AbstractFileDownloadStrategy
end_time = Time.now + timeout if timeout end_time = Time.now + timeout if timeout
download_lock = DownloadLock.new(temporary_path) download_lock = DownloadLock.new(temporary_path)
begin
download_lock.lock download_lock.lock
urls = [url, *mirrors] urls = [url, *mirrors]
@ -419,7 +420,9 @@ class CurlDownloadStrategy < AbstractFileDownloadStrategy
meta[:headers]&.delete_if { |header| header.start_with?("Authorization") } if is_redirection meta[:headers]&.delete_if { |header| header.start_with?("Authorization") } if is_redirection
# The cached location is no longer fresh if Last-Modified is after the file's timestamp # The cached location is no longer fresh if Last-Modified is after the file's timestamp
use_cached_location = false if cached_location.exist? && last_modified && last_modified > cached_location.mtime if cached_location.exist? && last_modified && last_modified > cached_location.mtime
use_cached_location = false
end
if use_cached_location if use_cached_location
puts "Already downloaded: #{cached_location}" puts "Already downloaded: #{cached_location}"
@ -444,8 +447,8 @@ class CurlDownloadStrategy < AbstractFileDownloadStrategy
raise Timeout::Error, "Timed out downloading #{self.url}: #{e}" raise Timeout::Error, "Timed out downloading #{self.url}: #{e}"
end end
ensure ensure
download_lock&.unlock download_lock.unlock(unlink: true)
download_lock&.path&.unlink end
end end
def clear_cache def clear_cache

View File

@ -15,19 +15,30 @@ class LockFile
@lockfile = nil @lockfile = nil
end end
sig { void }
def lock def lock
@path.parent.mkpath ignore_interrupts do
create_lockfile create_lockfile
return if @lockfile.flock(File::LOCK_EX | File::LOCK_NB)
next if @lockfile.flock(File::LOCK_EX | File::LOCK_NB)
raise OperationInProgressError, @locked_path raise OperationInProgressError, @locked_path
end end
end
def unlock sig { params(unlink: T::Boolean).void }
return if @lockfile.nil? || @lockfile.closed? def unlock(unlink: false)
ignore_interrupts do
next if @lockfile.nil? || @lockfile.closed?
@lockfile.flock(File::LOCK_UN) @lockfile.flock(File::LOCK_UN)
@lockfile.close @lockfile.close
if unlink
@path.unlink if @path.exist?
@lockfile = nil
end
end
end end
def with_lock def with_lock
@ -42,6 +53,8 @@ class LockFile
def create_lockfile def create_lockfile
return if @lockfile.present? && !@lockfile.closed? return if @lockfile.present? && !@lockfile.closed?
@path.dirname.mkpath
begin begin
@lockfile = @path.open(File::RDWR | File::CREAT) @lockfile = @path.open(File::RDWR | File::CREAT)
rescue Errno::EMFILE rescue Errno::EMFILE

View File

@ -5,18 +5,26 @@ require "lock_file"
RSpec.describe LockFile do RSpec.describe LockFile do
subject(:lock_file) { described_class.new(:lock, Pathname("foo")) } subject(:lock_file) { described_class.new(:lock, Pathname("foo")) }
let(:lock_file_copy) { described_class.new(:lock, Pathname("foo")) }
describe "#lock" do describe "#lock" do
it "does not raise an error when already locked" do it "ensures the lock file is created" do
expect(lock_file.path).not_to exist
lock_file.lock
expect(lock_file.path).to exist
end
it "does not raise an error when the same instance is locked multiple times" do
lock_file.lock lock_file.lock
expect { lock_file.lock }.not_to raise_error expect { lock_file.lock }.not_to raise_error
end end
it "raises an error if a lock already exists" do it "raises an error if another instance is already locked" do
lock_file.lock lock_file.lock
expect do expect do
described_class.new(:lock, Pathname("foo")).lock lock_file_copy.lock
end.to raise_error(OperationInProgressError) end.to raise_error(OperationInProgressError)
end end
end end
@ -30,7 +38,20 @@ RSpec.describe LockFile do
lock_file.lock lock_file.lock
lock_file.unlock lock_file.unlock
expect { described_class.new(:lock, Pathname("foo")).lock }.not_to raise_error expect { lock_file_copy.lock }.not_to raise_error
end
it "allows deleting a lock file only by the instance that locked it" do
lock_file.lock
expect(lock_file.path).to exist
expect(lock_file_copy.path).to exist
lock_file_copy.unlock(unlink: true)
expect(lock_file_copy.path).to exist
expect(lock_file.path).to exist
lock_file.unlock(unlink: true)
expect(lock_file.path).not_to exist
end end
end end
end end