Skip to content

Commit

Permalink
Don't unlink in finalizer when already unlinked
Browse files Browse the repository at this point in the history
  • Loading branch information
peterzhu2118 committed Aug 20, 2024
1 parent ea2dec6 commit f677941
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/tempfile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,8 @@ def unlink
return
end

@finalizer_manager.unlinked = true

@unlinked = true
end
alias delete unlink
Expand Down Expand Up @@ -366,10 +368,13 @@ def inspect
attr_reader :unlinked, :mode, :opts, :finalizer_manager

class FinalizerManager # :nodoc:
attr_accessor :unlinked

def initialize(path)
@open_files = {}
@path = path
@pid = Process.pid
@unlinked = false
end

def register(obj, file)
Expand All @@ -381,7 +386,7 @@ def register(obj, file)
def call(object_id)
@open_files.delete(object_id).close

if @open_files.empty? && Process.pid == @pid
if @open_files.empty? && !@unlinked && Process.pid == @pid
$stderr.puts "removing #{@path}..." if $DEBUG
begin
File.unlink(@path)
Expand Down

0 comments on commit f677941

Please sign in to comment.