Skip to content

Commit f3e7e34

Browse files
committed
fix: avoid race condition in singleton instance method
1 parent 8def74b commit f3e7e34

File tree

1 file changed

+6
-3
lines changed
  • instrumentation/base/lib/opentelemetry/instrumentation

1 file changed

+6
-3
lines changed

instrumentation/base/lib/opentelemetry/instrumentation/base.rb

+6-3
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,9 @@ class << self
6969
integer: ->(v) { v.is_a?(Integer) },
7070
string: ->(v) { v.is_a?(String) }
7171
}.freeze
72+
SINGLETON_MUTEX = Thread::Mutex.new
7273

73-
private_constant :NAME_REGEX, :VALIDATORS
74+
private_constant :NAME_REGEX, :VALIDATORS, :SINGLETON_MUTEX
7475

7576
private :new
7677

@@ -163,8 +164,10 @@ def option(name, default:, validate:)
163164
end
164165

165166
def instance
166-
@instance ||= new(instrumentation_name, instrumentation_version, install_blk,
167-
present_blk, compatible_blk, options)
167+
@instance || SINGLETON_MUTEX.synchronize do
168+
@instance ||= new(instrumentation_name, instrumentation_version, install_blk,
169+
present_blk, compatible_blk, options)
170+
end
168171
end
169172

170173
private

0 commit comments

Comments
 (0)