Skip to content

Commit ef2ed3c

Browse files
Merge pull request #54 from harness/FFM-12281
feat: [FFM-12281]: thread safety fixes
2 parents 766909d + 645a6a6 commit ef2ed3c

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

lib/ff/ruby/server/sdk/api/cf_client.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@
55

66
class CfClient < Closeable
77
include Singleton
8-
8+
9+
@@instance_mutex = Mutex.new
910
def init(api_key, config, connector = nil)
1011
# Only initialize if @client is nil to avoid reinitialization
11-
unless @client
12-
@config = config || ConfigBuilder.new.build
13-
@client = InnerClient.new(api_key, @config, connector)
14-
@config.logger.debug "Client initialized with API key: #{api_key}"
12+
13+
@@instance_mutex.synchronize do
14+
unless @client
15+
@config = config || ConfigBuilder.new.build
16+
@client = InnerClient.new(api_key, @config, connector)
17+
@config.logger.debug "Client initialized with API key: #{api_key}"
18+
end
1519
end
1620
@client
1721
end

lib/ff/ruby/server/sdk/api/inner_client.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,11 @@ def on_auth_success
120120
end
121121

122122
def on_auth_failed
123-
SdkCodes::warn_auth_failed_srv_defaults @config.logger
124-
@initialized = true
125-
@condition.signal
123+
@my_mutex.synchronize do
124+
SdkCodes::warn_auth_failed_srv_defaults @config.logger
125+
@initialized = true
126+
@condition.broadcast
127+
end
126128
end
127129

128130
def close
@@ -243,7 +245,7 @@ def on_processor_ready(processor)
243245

244246
SdkCodes.info_sdk_init_ok @config.logger
245247

246-
@condition.signal
248+
@condition.broadcast
247249
@initialized = true
248250
end
249251
end
@@ -257,7 +259,6 @@ def wait_for_initialization(timeout: nil)
257259
remaining = timeout ? timeout / 1000.0 : nil # Convert timeout to seconds
258260

259261
until @initialized
260-
261262
# Break if timeout has elapsed
262263
if remaining && remaining <= 0
263264
@config.logger.warn "The SDK has timed out waiting to initialize with supplied timeout #{timeout} ms. The SDK will continue to initialize in the background. Default variations will be served until the SDK initializes."

0 commit comments

Comments
 (0)