Skip to content

Commit 0363dda

Browse files
committed
Synchronize access to the Gem::Specification::LOAD_CACHE Hash
* It's accessed concurrently, notably when installing a C extension such as: $ jt ruby --vm.ea --vm.esa -S gem install oily_png
1 parent f5bde7e commit 0363dda

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-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: 8 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,9 @@ 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+
LOAD_CACHE[file] = _spec
1177+
end
11731178
return _spec
11741179
end
11751180

0 commit comments

Comments
 (0)