Skip to content

Commit 8daf0d1

Browse files
hsbtclaude
andcommitted
Clean up the compact index tmpdir when the cache is not writable
When the gem home is not writable, compact_index_cache_dir falls back to Dir.mktmpdir but never removed it, leaking a directory under the system temp on every gem command. Remove it at process exit. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 0bf8967 commit 8daf0d1

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

lib/rubygems/source.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,10 @@ def compact_index_cache_dir(index_uri)
299299
"#{index_uri.host}%#{index_uri.port}", *escaped_path.split("/").reject(&:empty?)
300300
else
301301
require "tmpdir"
302-
Dir.mktmpdir "gem_compact_index"
302+
require "fileutils"
303+
dir = Dir.mktmpdir "gem_compact_index"
304+
at_exit { FileUtils.rm_rf dir }
305+
dir
303306
end
304307
end
305308

test/rubygems/test_gem_source.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,24 @@ def test_load_specs_compact_index_skips_invalid_version
195195
assert_equal %w[a-1], released
196196
end
197197

198+
def test_compact_index_cache_dir_removes_tmpdir_at_exit
199+
source = Gem::Source.new @gem_repo
200+
def source.update_cache?
201+
false
202+
end
203+
204+
cleanup = nil
205+
source.define_singleton_method(:at_exit) {|&block| cleanup = block }
206+
207+
dir = source.send(:compact_index_cache_dir, Gem::URI(@gem_repo))
208+
209+
assert_path_exist dir
210+
refute_nil cleanup, "expected a cleanup hook to be registered"
211+
212+
cleanup.call
213+
assert_path_not_exist dir
214+
end
215+
198216
def test_load_specs_falls_back_to_marshal_index
199217
# no compact index data set up, only the Marshal indexes from setup
200218
released = @source.load_specs(:released).map(&:full_name)

0 commit comments

Comments
 (0)