Skip to content

Commit 6bf1747

Browse files
committed
[GR-16228] Synchronize access to the Gem::Specification::LOAD_CACHE Hash.
PullRequest: truffleruby/872
2 parents fe1daf6 + 4b68038 commit 6bf1747

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Bug fixes:
1616
* Procs will now yield to the block in their declaration context even when called with a block argument (#1657).
1717
* Fixed problems with calling POSIX methods if `Symbol#[]` is redefined (#1665).
1818
* Fixed sharing of `Array` and `Hash` elements for thread-safety of objects (#1601).
19+
* Fixed concurrent modifications of `Gem::Specification::LOAD_CACHE` (#1601).
1920
* Fix `TCPServer#accept` to set `#do_not_reverse_lookup` correctly on the created `TCPSocket`.
2021

2122
Compatibility

lib/mri/rubygems/specification.rb

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ class Gem::Specification < Gem::BasicSpecification
111111
# rubocop:disable Style/MutableConstant
112112
LOAD_CACHE = {} # :nodoc:
113113
# rubocop:enable Style/MutableConstant
114+
LOAD_CACHE_MUTEX = Mutex.new
114115

115116
private_constant :LOAD_CACHE if defined? private_constant
116117

@@ -759,7 +760,9 @@ def self._all # :nodoc:
759760
end
760761

761762
def self._clear_load_cache # :nodoc:
762-
LOAD_CACHE.clear
763+
LOAD_CACHE_MUTEX.synchronize do
764+
LOAD_CACHE.clear
765+
end
763766
end
764767

765768
def self.each_gemspec(dirs) # :nodoc:
@@ -1154,7 +1157,7 @@ def self._latest_specs(specs, prerelease = false) # :nodoc:
11541157
def self.load(file)
11551158
return unless file
11561159

1157-
_spec = LOAD_CACHE[file]
1160+
_spec = LOAD_CACHE_MUTEX.synchronize { LOAD_CACHE[file] }
11581161
return _spec if _spec
11591162

11601163
file = file.dup.untaint
@@ -1169,7 +1172,14 @@ def self.load(file)
11691172

11701173
if Gem::Specification === _spec
11711174
_spec.loaded_from = File.expand_path file.to_s
1172-
LOAD_CACHE[file] = _spec
1175+
LOAD_CACHE_MUTEX.synchronize do
1176+
prev = LOAD_CACHE[file]
1177+
if prev
1178+
_spec = prev
1179+
else
1180+
LOAD_CACHE[file] = _spec
1181+
end
1182+
end
11731183
return _spec
11741184
end
11751185

0 commit comments

Comments
 (0)