[rubygems/rubygems] Protect binstub access during creation with a flock

https://github.com/rubygems/rubygems/commit/88e3f1d23c
This commit is contained in:
David Rodríguez 2024-06-26 16:47:06 +02:00 committed by git
parent 091a6ea8c1
commit 5c826ebea5
2 changed files with 20 additions and 17 deletions

View File

@ -802,6 +802,25 @@ An Array (#{env.inspect}) was passed in from #{caller[3]}
end
end
##
# Open a file with given flags, and protect access with flock
def self.open_file_with_flock(path, flags, &block)
File.open(path, flags) do |io|
begin
io.flock(File::LOCK_EX)
rescue Errno::ENOSYS, Errno::ENOTSUP
end
yield io
end
rescue Errno::ENOLCK # NFS
if Thread.main != Thread.current
raise
else
open_file_without_flock(path, flags, &block)
end
end
##
# The path to the running Ruby interpreter.
@ -1298,22 +1317,6 @@ An Array (#{env.inspect}) was passed in from #{caller[3]}
private
def open_file_with_flock(path, flags, &block)
File.open(path, flags) do |io|
begin
io.flock(File::LOCK_EX)
rescue Errno::ENOSYS, Errno::ENOTSUP
end
yield io
end
rescue Errno::ENOLCK # NFS
if Thread.main != Thread.current
raise
else
open_file_without_flock(path, flags, &block)
end
end
def open_file_without_flock(path, flags, &block)
File.open(path, flags, &block)
end

View File

@ -222,7 +222,7 @@ class Gem::Installer
ruby_executable = false
existing = nil
File.open generated_bin, "rb" do |io|
Gem.open_file_with_flock generated_bin, "rb+" do |io|
line = io.gets
shebang = /^#!.*ruby/o