From 2c7266cbb3dcc0546d454c3d7b8644f397728835 Mon Sep 17 00:00:00 2001 From: Matthew Keeler Date: Thu, 12 Nov 2026 17:04:29 -0500 Subject: [PATCH 01/12] fix: Prevent the file data source from missing a file change made during startup Both file data sources read the files first and started the file watcher afterwards. A change between the read and the watcher start produced no update, so the SDK served the old data until the file changed again. FileDataSourceImpl#start and FileDataSourceV2#sync now start the watcher before the initial read. In FileDataSourceImpl, the watcher can now reload while the first load runs. A lock serializes the loads, so an older read cannot overwrite a newer one in the store. FileDataSourceV2 already serializes reads with its lock. FileDataSourceV2#sync stops the watcher if the initial read fails, as before when it never started one. stop now stops the watcher outside the lock, so a reload that waits for the lock cannot block it. --- .../impl/integrations/file_data_source.rb | 13 +++--- .../impl/integrations/file_data_source_v2.rb | 26 ++++++----- spec/integrations/file_data_source_spec.rb | 31 +++++++++++++ spec/integrations/file_data_source_v2_spec.rb | 43 +++++++++++++++++++ 4 files changed, 96 insertions(+), 17 deletions(-) diff --git a/lib/ldclient-rb/impl/integrations/file_data_source.rb b/lib/ldclient-rb/impl/integrations/file_data_source.rb index 1b3cbf8b..9f436e16 100644 --- a/lib/ldclient-rb/impl/integrations/file_data_source.rb +++ b/lib/ldclient-rb/impl/integrations/file_data_source.rb @@ -43,6 +43,7 @@ def initialize(data_store, data_source_update_sink, logger, options={}) @version_lock = Mutex.new @last_version = 1 + @load_lock = Mutex.new end def initialized? @@ -56,13 +57,9 @@ def start # the difference can be detected by checking "initialized?" ready.set - load_all + @listener = start_listener if @auto_update - if @auto_update - # If we're going to watch files, then the start event will be set the first time we get - # a successful load. - @listener = start_listener - end + load_all ready end @@ -74,6 +71,10 @@ def stop private def load_all + @load_lock.synchronize { load_all_locked } + end + + def load_all_locked all_data = { Impl::DataStore::FEATURES => {}, Impl::DataStore::SEGMENTS => {}, diff --git a/lib/ldclient-rb/impl/integrations/file_data_source_v2.rb b/lib/ldclient-rb/impl/integrations/file_data_source_v2.rb index 3e9753bd..e91b842a 100644 --- a/lib/ldclient-rb/impl/integrations/file_data_source_v2.rb +++ b/lib/ldclient-rb/impl/integrations/file_data_source_v2.rb @@ -116,9 +116,13 @@ def fetch(selector_store) # @return [void] # def sync(selector_store) - # First yield initial data + @lock.synchronize do + @listener = start_listener unless @closed + end + initial_fetch = fetch(selector_store) unless initial_fetch.success? + stop_listener yield LaunchDarkly::Interfaces::DataSystem::Update.new( state: LaunchDarkly::Interfaces::DataSource::Status::OFF, error: LaunchDarkly::Interfaces::DataSource::ErrorInfo.new( @@ -136,11 +140,6 @@ def sync(selector_store) change_set: initial_fetch.value.change_set ) - # Start watching for file changes - @lock.synchronize do - @listener = start_listener unless @closed - end - until @closed begin update = @update_queue.pop @@ -173,17 +172,22 @@ def stop @lock.synchronize do return if @closed @closed = true - - listener = @listener - @listener = nil - - listener&.stop end + stop_listener # Signal shutdown to sync generator @update_queue.push(nil) end + private def stop_listener + listener = @lock.synchronize do + l = @listener + @listener = nil + l + end + listener&.stop + end + # # Load all files and build a changeset. # diff --git a/spec/integrations/file_data_source_spec.rb b/spec/integrations/file_data_source_spec.rb index 4ba8e9b1..399863ef 100644 --- a/spec/integrations/file_data_source_spec.rb +++ b/spec/integrations/file_data_source_spec.rb @@ -328,6 +328,37 @@ def test_auto_reload(options) test_auto_reload({ auto_update: true, force_polling: true, poll_interval: 0.1 }) end + def test_reload_of_change_during_first_load(options) + file = make_temp_file(flag_only_json) + options[:paths] = [ file.path ] + + with_data_source(options) do |ds| + first_load = true + allow(ds).to receive(:load_all_locked).and_wrap_original do |original| + original.call + if first_load + first_load = false + sleep(1) + IO.write(file, all_properties_json) + end + end + + ds.start + + max_time = 10 + ok = wait_for_condition(max_time) { @store.all(LaunchDarkly::Impl::DataStore::SEGMENTS).keys == all_segment_keys } + expect(ok).to eq(true), "Waited #{max_time}s after modifying file during the first load and it did not reload" + end + end + + it "reloads a file modified during the first load if auto-update is on" do + test_reload_of_change_during_first_load({ auto_update: true }) + end + + it "reloads a file modified during the first load in polling mode" do + test_reload_of_change_during_first_load({ auto_update: true, force_polling: true, poll_interval: 0.1 }) + end + it "evaluates simplified flag with client as expected" do file = make_temp_file(all_properties_json) factory = FileData.data_source({ paths: file.path }) diff --git a/spec/integrations/file_data_source_v2_spec.rb b/spec/integrations/file_data_source_v2_spec.rb index 2f157999..bd1adea3 100644 --- a/spec/integrations/file_data_source_v2_spec.rb +++ b/spec/integrations/file_data_source_v2_spec.rb @@ -362,6 +362,49 @@ def no_selector_store end end + it "detects a file change made during the initial fetch" do + file = make_temp_file(flag_only_json) + + source = Impl::Integrations::FileDataSourceV2.new( + logger, + paths: [file.path], + poll_interval: 0.1 + ) + + first_load = true + allow(source).to receive(:load_all_to_changeset).and_wrap_original do |original| + result = original.call + if first_load + first_load = false + sleep 1 + IO.write(file, segment_only_json) + end + result + end + + updates = [] + update_received = Concurrent::Event.new + + begin + sync_thread = Thread.new do + source.sync(no_selector_store) do |update| + updates << update + update_received.set if updates.length >= 2 + break if updates.length >= 2 + end + end + + expect(update_received.wait(5)).to eq(true), "Did not receive an update for the change made during the initial fetch" + expect(updates[1].state).to eq(LaunchDarkly::Interfaces::DataSource::Status::VALID) + + segment_changes = updates[1].change_set.changes.select { |c| c.kind == LaunchDarkly::Interfaces::DataSystem::ObjectKind::SEGMENT } + expect(segment_changes.length).to eq(1) + ensure + source.stop + sync_thread&.join(2) + end + end + it "reports error on invalid file update" do file = make_temp_file(flag_only_json) From 500ed6f7b00838346239ccd3f36a24b1ca22350e Mon Sep 17 00:00:00 2001 From: Matthew Keeler Date: Fri, 13 Nov 2026 11:30:34 -0500 Subject: [PATCH 02/12] fix: Stop the file data source watcher if the first load raises FileDataSourceImpl#start now starts the watcher before the first load. If the store raises during that load, start raised to LDClient.new and nobody stopped the watcher. start now stops it and raises again. Also add a spec that writes the file before the first load updates the store. It fails without the load lock when listen is in use. In polling mode the poller reloads on every interval after a change, so the same spec passes there with or without the lock. --- .../impl/integrations/file_data_source.rb | 7 +++- spec/integrations/file_data_source_spec.rb | 42 +++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/lib/ldclient-rb/impl/integrations/file_data_source.rb b/lib/ldclient-rb/impl/integrations/file_data_source.rb index 9f436e16..49bb038f 100644 --- a/lib/ldclient-rb/impl/integrations/file_data_source.rb +++ b/lib/ldclient-rb/impl/integrations/file_data_source.rb @@ -59,7 +59,12 @@ def start @listener = start_listener if @auto_update - load_all + begin + load_all + rescue + stop + raise + end ready end diff --git a/spec/integrations/file_data_source_spec.rb b/spec/integrations/file_data_source_spec.rb index 399863ef..48a8c39d 100644 --- a/spec/integrations/file_data_source_spec.rb +++ b/spec/integrations/file_data_source_spec.rb @@ -359,6 +359,48 @@ def test_reload_of_change_during_first_load(options) test_reload_of_change_during_first_load({ auto_update: true, force_polling: true, poll_interval: 0.1 }) end + def test_reload_of_change_before_first_store_update(options) + file = make_temp_file(flag_only_json) + options[:paths] = [ file.path ] + sink = @config.data_source_update_sink + + with_data_source(options) do |ds| + first_init = true + allow(sink).to receive(:init).and_wrap_original do |original, data| + if first_init + first_init = false + IO.write(file, all_properties_json) + sleep(1.5) + end + original.call(data) + end + + ds.start + + max_time = 10 + ok = wait_for_condition(max_time) { @store.all(LaunchDarkly::Impl::DataStore::SEGMENTS).keys == all_segment_keys } + expect(ok).to eq(true), "Waited #{max_time}s and the older first load replaced the newer data" + end + end + + it "keeps a change made before the first load updates the store if auto-update is on" do + test_reload_of_change_before_first_store_update({ auto_update: true, poll_interval: 0.1 }) + end + + it "keeps a change made before the first load updates the store in polling mode" do + test_reload_of_change_before_first_store_update({ auto_update: true, force_polling: true, poll_interval: 0.1 }) + end + + it "stops watching if the first load raises" do + file = make_temp_file(flag_only_json) + sink = @config.data_source_update_sink + allow(sink).to receive(:init).and_raise(StandardError, "store failure") + ds = FileData.data_source({ paths: [ file.path ], auto_update: true }).call('', @config) + + expect { ds.start }.to raise_error(StandardError, "store failure") + expect(ds.instance_variable_get(:@listener).stopped?).to eq(true) + end + it "evaluates simplified flag with client as expected" do file = make_temp_file(all_properties_json) factory = FileData.data_source({ paths: file.path }) From e8ccc8b5bf4fd34d9bca4cf579b9afdc0ed1a6ed Mon Sep 17 00:00:00 2001 From: Matthew Keeler Date: Fri, 13 Nov 2026 11:30:34 -0500 Subject: [PATCH 03/12] fix: Report file watcher failures in the FDv2 file data source FileDataSourceV2#sync now starts the watcher before the initial fetch, which exposed two cases. When no configured path resolved, start_listener passed no directories to Listen.to, and listen watched the working directory recursively. start_listener now returns nil when no path resolves. When the watcher failed to start, sync raised before it yielded any update, so the data system got no status. sync now logs the error and yields OFF with an UNKNOWN error. Also run the startup change spec with the poller. --- .../impl/integrations/file_data_source_v2.rb | 21 ++++++-- spec/integrations/file_data_source_v2_spec.rb | 52 ++++++++++++++++++- 2 files changed, 69 insertions(+), 4 deletions(-) diff --git a/lib/ldclient-rb/impl/integrations/file_data_source_v2.rb b/lib/ldclient-rb/impl/integrations/file_data_source_v2.rb index e91b842a..50d86e6b 100644 --- a/lib/ldclient-rb/impl/integrations/file_data_source_v2.rb +++ b/lib/ldclient-rb/impl/integrations/file_data_source_v2.rb @@ -116,8 +116,22 @@ def fetch(selector_store) # @return [void] # def sync(selector_store) - @lock.synchronize do - @listener = start_listener unless @closed + begin + @lock.synchronize do + @listener = start_listener unless @closed + end + rescue => e + @logger.error { "[LDClient] Unable to watch data files for changes: #{e.message}" } + yield LaunchDarkly::Interfaces::DataSystem::Update.new( + state: LaunchDarkly::Interfaces::DataSource::Status::OFF, + error: LaunchDarkly::Interfaces::DataSource::ErrorInfo.new( + LaunchDarkly::Interfaces::DataSource::ErrorInfo::UNKNOWN, + 0, + "Unable to watch data files for changes: #{e.message}", + Time.now + ) + ) + return end initial_fetch = fetch(selector_store) @@ -366,7 +380,7 @@ def stop # # Start watching files for changes. # - # @return [Object] auto-updater instance + # @return [Object, nil] auto-updater instance, or nil if no path can be watched # private def start_listener resolved_paths = @paths.map do |p| @@ -377,6 +391,7 @@ def stop nil end end.compact + return nil if resolved_paths.empty? if @@have_listen start_listener_with_listen_gem(resolved_paths) diff --git a/spec/integrations/file_data_source_v2_spec.rb b/spec/integrations/file_data_source_v2_spec.rb index bd1adea3..3cb6ac0f 100644 --- a/spec/integrations/file_data_source_v2_spec.rb +++ b/spec/integrations/file_data_source_v2_spec.rb @@ -362,7 +362,7 @@ def no_selector_store end end - it "detects a file change made during the initial fetch" do + def expect_change_during_initial_fetch_detected file = make_temp_file(flag_only_json) source = Impl::Integrations::FileDataSourceV2.new( @@ -405,6 +405,56 @@ def no_selector_store end end + it "detects a file change made during the initial fetch" do + expect_change_during_initial_fetch_detected + end + + it "detects a file change made during the initial fetch in polling mode" do + have_listen = Impl::Integrations::FileDataSourceV2.class_variable_get(:@@have_listen) + Impl::Integrations::FileDataSourceV2.class_variable_set(:@@have_listen, false) + begin + expect_change_during_initial_fetch_detected + ensure + Impl::Integrations::FileDataSourceV2.class_variable_set(:@@have_listen, have_listen) + end + end + + def first_sync_update(source) + updates = [] + sync_thread = Thread.new do + source.sync(no_selector_store) do |update| + updates << update + break + end + end + expect(sync_thread.join(5)).not_to be_nil, "sync did not return" + updates[0] + ensure + source.stop + sync_thread&.join(2) + end + + it "does not watch the working directory when no path exists" do + expect(Listen).not_to receive(:to) if defined?(Listen) + source = Impl::Integrations::FileDataSourceV2.new(logger, paths: [File.join(@tmp_dir, "no-such-file")]) + + update = first_sync_update(source) + + expect(update.state).to eq(LaunchDarkly::Interfaces::DataSource::Status::OFF) + end + + it "reports an error if the file watcher cannot start" do + file = make_temp_file(all_properties_json) + source = Impl::Integrations::FileDataSourceV2.new(logger, paths: [file.path]) + allow(source).to receive(:start_listener).and_raise(StandardError, "watch failure") + + update = first_sync_update(source) + + expect(update.state).to eq(LaunchDarkly::Interfaces::DataSource::Status::OFF) + expect(update.error.kind).to eq(LaunchDarkly::Interfaces::DataSource::ErrorInfo::UNKNOWN) + expect(update.error.message).to include("watch failure") + end + it "reports error on invalid file update" do file = make_temp_file(flag_only_json) From 5fbfbe5883aa34f8dc842c0b6afacb42142afd90 Mon Sep 17 00:00:00 2001 From: Matthew Keeler Date: Fri, 13 Nov 2026 11:30:34 -0500 Subject: [PATCH 04/12] fix: Prevent the file override source from missing a file change made during startup FileOverrideSource#start read the files and then created the poller or watcher. The poller takes its baseline when it is created, and the watcher sees only events after its watch exists. A change between the read and that point produced no reload. start now creates the change detector first. A trigger that arrives during the initial load schedules a debounced reload, and the reloader serializes reloads and skips unchanged content. --- .../impl/integrations/file_override_source.rb | 15 ++++----- .../integrations/file_override_source_spec.rb | 33 +++++++++++++++++++ 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/lib/ldclient-rb/impl/integrations/file_override_source.rb b/lib/ldclient-rb/impl/integrations/file_override_source.rb index e9214f3d..30277a0b 100644 --- a/lib/ldclient-rb/impl/integrations/file_override_source.rb +++ b/lib/ldclient-rb/impl/integrations/file_override_source.rb @@ -52,8 +52,8 @@ def initialize(paths:, duplicate_keys_handling:, change_detection:, poll_interva attr_reader :poll_interval # - # Performs the initial load synchronously, so overrides present in the files are in effect - # when this method returns, then starts change detection. A file that does not exist yet + # Starts change detection, then performs the initial load synchronously, so overrides + # present in the files are in effect when this method returns. A file that does not exist yet # contributes no overrides. A file that cannot be read or parsed is not fatal: the client # runs with the last good overrides, the failure is logged, and the retry plus the change # signal recover once the file is readable. @@ -61,7 +61,7 @@ def initialize(paths:, duplicate_keys_handling:, change_detection:, poll_interva # (see LaunchDarkly::Interfaces::Overrides::OverrideSource#start) # def start(sink) - @lock.synchronize do + reloader = @lock.synchronize do return if @stopped @reloader = FileData::Reloader.new( @@ -77,12 +77,6 @@ def start(sink) log_overrides_in_effect(merged) end ) - end - - @reloader.reload_now - - @lock.synchronize do - return if @stopped trigger = @reloader.method(:trigger) @change_detector = @@ -91,7 +85,10 @@ def start(sink) else FileData::Poller.new(@paths, @poll_interval, trigger, @logger) end + @reloader end + + reloader.reload_now end # (see LaunchDarkly::Interfaces::Overrides::OverrideSource#stop) diff --git a/spec/integrations/file_override_source_spec.rb b/spec/integrations/file_override_source_spec.rb index bbc7a894..502cb6ff 100644 --- a/spec/integrations/file_override_source_spec.rb +++ b/spec/integrations/file_override_source_spec.rb @@ -95,6 +95,19 @@ def make_source(paths, duplicate_keys_handling: :fail, change_detection: :pollin ) end + # Writes the file once, right after the initial load reads the files. + def write_after_initial_load(name, content) + first = true + allow_any_instance_of(Impl::FileData::Reloader).to receive(:reload_now).and_wrap_original do |original, *args| + result = original.call(*args) + if first + first = false + File.write(path(name), content) + end + result + end + end + def with_source(paths, **options) source = make_source(paths, **options) sink = RecordingSink.new @@ -375,6 +388,16 @@ def deleted_message(category, key, file) end describe "polling change detection" do + it "applies a change made during the initial load" do + a = write("a.json", values_doc({ flag1: "a" })) + write_after_initial_load("a.json", values_doc({ flag1: "bb" })) + + with_source([a]) do |_source, sink| + expect(wait_for { sink.snapshots.length == 2 }).to be true + expect(sink.flag_values).to eq({ "flag1" => "bb" }) + end + end + it "applies a changed file" do a = write("a.json", values_doc({ flag1: "a" })) @@ -467,6 +490,16 @@ def deleted_message(category, key, file) skip "the listen gem is not installed" unless Impl::FileData::Watcher.available? end + it "applies a change made during the initial load" do + a = write("a.json", values_doc({ flag1: "a" })) + write_after_initial_load("a.json", values_doc({ flag1: "bb" })) + + with_source([a], change_detection: :watching) do |_source, sink| + expect(wait_for { sink.snapshots.length == 2 }).to be true + expect(sink.flag_values).to eq({ "flag1" => "bb" }) + end + end + it "applies a changed file" do a = write("a.json", values_doc({ flag1: "a" })) From 71e01031b3a0b1a71c0b6ab46e9d96225600d4a1 Mon Sep 17 00:00:00 2001 From: Matthew Keeler Date: Fri, 13 Nov 2026 11:30:34 -0500 Subject: [PATCH 05/12] docs: Note that listen can miss a file change made during startup On platforms other than Linux, listen builds its snapshot of the watched directories on its own thread after start returns, and gives no signal when the snapshot is complete. A change before the snapshot becomes part of it, so listen reports nothing. The data then stays old until the next change to a file. --- lib/ldclient-rb/integrations/file_data.rb | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/ldclient-rb/integrations/file_data.rb b/lib/ldclient-rb/integrations/file_data.rb index ebd82648..9ad7cb28 100644 --- a/lib/ldclient-rb/integrations/file_data.rb +++ b/lib/ldclient-rb/integrations/file_data.rb @@ -101,7 +101,9 @@ module FileData # Note that the default implementation of this feature is based on polling the filesystem, # which may not perform well. If you install the 'listen' gem (not included by default, to # avoid adding unwanted dependencies to the SDK), its native file watching mechanism will be - # used instead. + # used instead. On platforms other than Linux, the 'listen' gem can miss a change that occurs + # while the data source starts. The data source then uses the earlier contents until the + # next change to a file. # @option options [Float] :poll_interval The minimum interval, in seconds, between checks for # file modifications - used only if auto_update is true, and if the native file-watching # mechanism from 'listen' is not being used. The default value is 1 second. @@ -125,7 +127,9 @@ def self.data_source(options={}) # may be absolute paths or relative to the current working directory. (Required) # @option options [Float] :poll_interval The minimum interval, in seconds, between checks for # file modifications - used only if the native file-watching mechanism from 'listen' is not - # being used. The default value is 1 second. + # being used. The default value is 1 second. On platforms other than Linux, the 'listen' gem can miss a change that occurs + # while the data source starts. The data source then uses the earlier contents until the + # next change to a file. # @return [FileDataSourceV2Builder] a builder that can be used as an FDv2 initializer or synchronizer # # @example Using as an initializer @@ -225,7 +229,8 @@ def self.data_source_v2(options = {}) # including the update of a mounted Kubernetes ConfigMap. It does not follow a deployment that # points a link further along the path, or a configured directory, at another location, such as a # `current` link to a release directory, or that replaces a configured directory. Use polling for - # such deployments. + # such deployments. On platforms other than Linux, watching can miss a change that occurs while + # the source starts. The source then uses the earlier contents until the next change to a file. # @option options [Numeric] :poll_interval Seconds between examinations of the files in polling # mode. The default is 1. An interval below 1 is raised to 1 with a warning. # @return [FileOverrideSourceBuilder] a builder for {LaunchDarkly::DataSystem::ConfigBuilder#overrides} From 103faa37f777f53b6513fa21acd9558bc55802a1 Mon Sep 17 00:00:00 2001 From: Matthew Keeler Date: Fri, 13 Nov 2026 15:46:42 -0500 Subject: [PATCH 06/12] fix: Serve the file data when the FDv2 file watcher cannot start Since the watcher now starts before the initial fetch, a watcher failure made sync yield OFF before it read the files. FDv2 removes a synchronizer that reports OFF, so readable files never reached the store. The common cause on Linux is the inotify watch limit. sync now logs the failure and continues with the fetch, so the files are served without updates, as before the reorder. Also stop the listen listener when its start raises part way through. Otherwise the inotify notifier for the directories it already watched stays open. --- .../impl/integrations/file_data_source_v2.rb | 19 ++++++--------- spec/integrations/file_data_source_v2_spec.rb | 23 +++++++++++++++---- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/lib/ldclient-rb/impl/integrations/file_data_source_v2.rb b/lib/ldclient-rb/impl/integrations/file_data_source_v2.rb index 50d86e6b..ad321d9e 100644 --- a/lib/ldclient-rb/impl/integrations/file_data_source_v2.rb +++ b/lib/ldclient-rb/impl/integrations/file_data_source_v2.rb @@ -121,17 +121,7 @@ def sync(selector_store) @listener = start_listener unless @closed end rescue => e - @logger.error { "[LDClient] Unable to watch data files for changes: #{e.message}" } - yield LaunchDarkly::Interfaces::DataSystem::Update.new( - state: LaunchDarkly::Interfaces::DataSource::Status::OFF, - error: LaunchDarkly::Interfaces::DataSource::ErrorInfo.new( - LaunchDarkly::Interfaces::DataSource::ErrorInfo::UNKNOWN, - 0, - "Unable to watch data files for changes: #{e.message}", - Time.now - ) - ) - return + @logger.error { "[LDClient] Unable to watch data files for changes, so file changes will not be applied: #{e.message}" } end initial_fetch = fetch(selector_store) @@ -416,7 +406,12 @@ def stop on_file_change end end - l.start + begin + l.start + rescue + l.stop + raise + end l end end diff --git a/spec/integrations/file_data_source_v2_spec.rb b/spec/integrations/file_data_source_v2_spec.rb index 3cb6ac0f..8d65f213 100644 --- a/spec/integrations/file_data_source_v2_spec.rb +++ b/spec/integrations/file_data_source_v2_spec.rb @@ -443,16 +443,31 @@ def first_sync_update(source) expect(update.state).to eq(LaunchDarkly::Interfaces::DataSource::Status::OFF) end - it "reports an error if the file watcher cannot start" do + it "yields the file data if the file watcher cannot start" do file = make_temp_file(all_properties_json) source = Impl::Integrations::FileDataSourceV2.new(logger, paths: [file.path]) allow(source).to receive(:start_listener).and_raise(StandardError, "watch failure") update = first_sync_update(source) - expect(update.state).to eq(LaunchDarkly::Interfaces::DataSource::Status::OFF) - expect(update.error.kind).to eq(LaunchDarkly::Interfaces::DataSource::ErrorInfo::UNKNOWN) - expect(update.error.message).to include("watch failure") + expect(update.state).to eq(LaunchDarkly::Interfaces::DataSource::Status::VALID) + expect(update.change_set.changes).not_to be_empty + end + + it "stops a file watcher that fails part way through its start" do + file = make_temp_file(all_properties_json) + source = Impl::Integrations::FileDataSourceV2.new(logger, paths: [file.path]) + listener = nil + allow(Listen).to receive(:to).and_wrap_original do |original, *args, **opts, &block| + listener = original.call(*args, **opts, &block) + allow(listener).to receive(:start).and_raise(StandardError, "watch failure") + listener + end + + update = first_sync_update(source) + + expect(update.state).to eq(LaunchDarkly::Interfaces::DataSource::Status::VALID) + expect(listener.stopped?).to eq(true) end it "reports error on invalid file update" do From ddb72a9ca87c8ff25e46ee69571254c9e25d2c94 Mon Sep 17 00:00:00 2001 From: Matthew Keeler Date: Fri, 13 Nov 2026 15:46:42 -0500 Subject: [PATCH 07/12] fix: Prevent a second stop of the file data source from raising after a failed start When the first load raises, start stops the watcher. In polling mode a later stop then called Thread#run on the finished poller thread, which raised ThreadError. A rescued postfork failure followed by close reached it, and close then stopped before it flushed events. stop now clears the watcher before it stops it, so a second stop does nothing. Also stop the listen listener when its start raises part way through, as in the FDv2 file data source. --- .../impl/integrations/file_data_source.rb | 11 +++++++-- spec/integrations/file_data_source_spec.rb | 23 ++++++++++++++++--- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/lib/ldclient-rb/impl/integrations/file_data_source.rb b/lib/ldclient-rb/impl/integrations/file_data_source.rb index 49bb038f..c2b9ab9e 100644 --- a/lib/ldclient-rb/impl/integrations/file_data_source.rb +++ b/lib/ldclient-rb/impl/integrations/file_data_source.rb @@ -70,7 +70,9 @@ def start end def stop - @listener.stop unless @listener.nil? + listener = @listener + @listener = nil + listener&.stop end private @@ -180,7 +182,12 @@ def start_listener_with_listen_gem(resolved_paths) load_all end end - l.start + begin + l.start + rescue + l.stop + raise + end l end diff --git a/spec/integrations/file_data_source_spec.rb b/spec/integrations/file_data_source_spec.rb index 48a8c39d..89707db3 100644 --- a/spec/integrations/file_data_source_spec.rb +++ b/spec/integrations/file_data_source_spec.rb @@ -391,14 +391,31 @@ def test_reload_of_change_before_first_store_update(options) test_reload_of_change_before_first_store_update({ auto_update: true, force_polling: true, poll_interval: 0.1 }) end - it "stops watching if the first load raises" do + def test_stop_watching_if_first_load_raises(options) file = make_temp_file(flag_only_json) sink = @config.data_source_update_sink allow(sink).to receive(:init).and_raise(StandardError, "store failure") - ds = FileData.data_source({ paths: [ file.path ], auto_update: true }).call('', @config) + ds = FileData.data_source(options.merge(paths: [ file.path ], auto_update: true)).call('', @config) + listener = nil + allow(ds).to receive(:start_listener).and_wrap_original do |original| + listener = original.call + end expect { ds.start }.to raise_error(StandardError, "store failure") - expect(ds.instance_variable_get(:@listener).stopped?).to eq(true) + yield listener + expect { ds.stop }.not_to raise_error + end + + it "stops watching if the first load raises" do + test_stop_watching_if_first_load_raises({}) do |listener| + expect(listener.stopped?).to eq(true) + end + end + + it "stops polling if the first load raises" do + test_stop_watching_if_first_load_raises({ force_polling: true, poll_interval: 0.1 }) do |listener| + expect(listener.instance_variable_get(:@thread).join(2)).not_to be_nil + end end it "evaluates simplified flag with client as expected" do From 9617e0fe60f3b9683caf3e29389445a439f47f79 Mon Sep 17 00:00:00 2001 From: Matthew Keeler Date: Tue, 17 Nov 2026 12:09:34 -0500 Subject: [PATCH 08/12] test: Let the listen record build before the first-load lock spec writes the file On Windows without wdm, listen polls and builds its file record on a background thread after start returns. The spec wrote the file at once, so the record could take the new contents as its baseline and report no change. This is the documented startup limit, not the lock under test. Wait 1 second before the write, as the first-load spec does. --- spec/integrations/file_data_source_spec.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/integrations/file_data_source_spec.rb b/spec/integrations/file_data_source_spec.rb index 89707db3..aac36b68 100644 --- a/spec/integrations/file_data_source_spec.rb +++ b/spec/integrations/file_data_source_spec.rb @@ -369,6 +369,7 @@ def test_reload_of_change_before_first_store_update(options) allow(sink).to receive(:init).and_wrap_original do |original, data| if first_init first_init = false + sleep(1) IO.write(file, all_properties_json) sleep(1.5) end From cb7f7872ed6a526378a4253d0301e0ea40f31971 Mon Sep 17 00:00:00 2001 From: Matthew Keeler Date: Thu, 19 Nov 2026 09:47:16 -0500 Subject: [PATCH 09/12] fix: Start the FDv2 file watcher outside the data source lock When the listen gem fails part way through its start, the data source stops the listener. That stop waits for a file change callback that is in progress, and the callback waits for the data source lock. The sync method held that lock across the start, so the two could deadlock. Start the watcher without the lock, then take the lock only to record it. If the data source stopped in the meantime, stop the new watcher. --- .../impl/integrations/file_data_source_v2.rb | 12 +++++++--- spec/integrations/file_data_source_v2_spec.rb | 23 +++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/lib/ldclient-rb/impl/integrations/file_data_source_v2.rb b/lib/ldclient-rb/impl/integrations/file_data_source_v2.rb index ad321d9e..dff57529 100644 --- a/lib/ldclient-rb/impl/integrations/file_data_source_v2.rb +++ b/lib/ldclient-rb/impl/integrations/file_data_source_v2.rb @@ -116,13 +116,19 @@ def fetch(selector_store) # @return [void] # def sync(selector_store) + listener = nil begin - @lock.synchronize do - @listener = start_listener unless @closed - end + listener = start_listener unless @lock.synchronize { @closed } rescue => e @logger.error { "[LDClient] Unable to watch data files for changes, so file changes will not be applied: #{e.message}" } end + if listener + closed = @lock.synchronize do + @listener = listener unless @closed + @closed + end + listener.stop if closed + end initial_fetch = fetch(selector_store) unless initial_fetch.success? diff --git a/spec/integrations/file_data_source_v2_spec.rb b/spec/integrations/file_data_source_v2_spec.rb index 8d65f213..a4026a94 100644 --- a/spec/integrations/file_data_source_v2_spec.rb +++ b/spec/integrations/file_data_source_v2_spec.rb @@ -458,9 +458,14 @@ def first_sync_update(source) file = make_temp_file(all_properties_json) source = Impl::Integrations::FileDataSourceV2.new(logger, paths: [file.path]) listener = nil + lock_held_at_stop = nil allow(Listen).to receive(:to).and_wrap_original do |original, *args, **opts, &block| listener = original.call(*args, **opts, &block) allow(listener).to receive(:start).and_raise(StandardError, "watch failure") + allow(listener).to receive(:stop).and_wrap_original do |stop, *stop_args| + lock_held_at_stop = source.instance_variable_get(:@lock).owned? + stop.call(*stop_args) + end listener end @@ -468,6 +473,24 @@ def first_sync_update(source) expect(update.state).to eq(LaunchDarkly::Interfaces::DataSource::Status::VALID) expect(listener.stopped?).to eq(true) + expect(lock_held_at_stop).to eq(false) + end + + it "stops the file watcher if the source stops while the watcher starts" do + file = make_temp_file(all_properties_json) + source = Impl::Integrations::FileDataSourceV2.new(logger, paths: [file.path]) + listener = nil + allow(source).to receive(:start_listener).and_wrap_original do |original| + listener = original.call + source.stop + listener + end + + first_sync_update(source) + + expect(listener).not_to be_nil + expect(source.instance_variable_get(:@listener)).to be_nil + expect(listener.stopped?).to eq(true) end it "reports error on invalid file update" do From 486d95e3bd6127471d6ab46515c7dda192873c74 Mon Sep 17 00:00:00 2001 From: Matthew Keeler Date: Fri, 20 Nov 2026 11:16:48 -0500 Subject: [PATCH 10/12] fix: Load the data files when the file data source cannot watch them The v1 file data source now starts the file watcher before the first load. If the watcher failed to start, for example because a path did not exist or the system had no inotify watches left, start raised and no file was loaded. Log the failure and load the files without auto-update, as the FDv2 file data source does. A missing file now reports the load error and an interrupted status, as it does without auto-update. --- .../impl/integrations/file_data_source.rb | 8 +++++++- spec/integrations/file_data_source_spec.rb | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/ldclient-rb/impl/integrations/file_data_source.rb b/lib/ldclient-rb/impl/integrations/file_data_source.rb index c2b9ab9e..8dee68da 100644 --- a/lib/ldclient-rb/impl/integrations/file_data_source.rb +++ b/lib/ldclient-rb/impl/integrations/file_data_source.rb @@ -57,7 +57,13 @@ def start # the difference can be detected by checking "initialized?" ready.set - @listener = start_listener if @auto_update + if @auto_update + begin + @listener = start_listener + rescue => e + @logger.error { "[LDClient] Unable to watch data files for changes, so file changes will not be applied: #{e.message}" } + end + end begin load_all diff --git a/spec/integrations/file_data_source_spec.rb b/spec/integrations/file_data_source_spec.rb index aac36b68..f72f188f 100644 --- a/spec/integrations/file_data_source_spec.rb +++ b/spec/integrations/file_data_source_spec.rb @@ -419,6 +419,24 @@ def test_stop_watching_if_first_load_raises(options) end end + it "loads the files if the file watcher cannot start" do + file = make_temp_file(all_properties_json) + + with_data_source({ paths: [ file.path ], auto_update: true }) do |ds| + allow(ds).to receive(:start_listener).and_raise(StandardError, "watch failure") + expect { ds.start }.not_to raise_error + expect(@store.initialized?).to eq(true) + expect(@store.all(LaunchDarkly::Impl::DataStore::SEGMENTS).keys).to eq(all_segment_keys) + end + end + + it "reports a missing file instead of raising if auto-update is on" do + with_data_source({ paths: [ bad_file_path ], auto_update: true }) do |ds| + expect { ds.start }.not_to raise_error + expect(@store.initialized?).to eq(false) + end + end + it "evaluates simplified flag with client as expected" do file = make_temp_file(all_properties_json) factory = FileData.data_source({ paths: file.path }) From 58d1b2b96c76c4fffc2f7bce422f0fff4c142953 Mon Sep 17 00:00:00 2001 From: Matthew Keeler Date: Fri, 20 Nov 2026 11:17:26 -0500 Subject: [PATCH 11/12] fix: Stop the file watcher when the file data source start raises any exception The cleanup after a failed first load or a failed listen start used a bare rescue, which catches only StandardError. An exception such as Interrupt skipped it and left the watcher running. Use ensure so that the watcher stops for every exception. --- .../impl/integrations/file_data_source.rb | 14 ++++++++------ .../impl/integrations/file_data_source_v2.rb | 7 ++++--- spec/integrations/file_data_source_spec.rb | 12 +++++++++--- spec/integrations/file_data_source_v2_spec.rb | 19 +++++++++++++++++++ 4 files changed, 40 insertions(+), 12 deletions(-) diff --git a/lib/ldclient-rb/impl/integrations/file_data_source.rb b/lib/ldclient-rb/impl/integrations/file_data_source.rb index 8dee68da..63dfc37b 100644 --- a/lib/ldclient-rb/impl/integrations/file_data_source.rb +++ b/lib/ldclient-rb/impl/integrations/file_data_source.rb @@ -65,11 +65,12 @@ def start end end + loaded = false begin load_all - rescue - stop - raise + loaded = true + ensure + stop unless loaded end ready @@ -188,11 +189,12 @@ def start_listener_with_listen_gem(resolved_paths) load_all end end + started = false begin l.start - rescue - l.stop - raise + started = true + ensure + l.stop unless started end l end diff --git a/lib/ldclient-rb/impl/integrations/file_data_source_v2.rb b/lib/ldclient-rb/impl/integrations/file_data_source_v2.rb index dff57529..bfeb259e 100644 --- a/lib/ldclient-rb/impl/integrations/file_data_source_v2.rb +++ b/lib/ldclient-rb/impl/integrations/file_data_source_v2.rb @@ -412,11 +412,12 @@ def stop on_file_change end end + started = false begin l.start - rescue - l.stop - raise + started = true + ensure + l.stop unless started end l end diff --git a/spec/integrations/file_data_source_spec.rb b/spec/integrations/file_data_source_spec.rb index f72f188f..3959cdba 100644 --- a/spec/integrations/file_data_source_spec.rb +++ b/spec/integrations/file_data_source_spec.rb @@ -392,17 +392,17 @@ def test_reload_of_change_before_first_store_update(options) test_reload_of_change_before_first_store_update({ auto_update: true, force_polling: true, poll_interval: 0.1 }) end - def test_stop_watching_if_first_load_raises(options) + def test_stop_watching_if_first_load_raises(options, error_class = StandardError) file = make_temp_file(flag_only_json) sink = @config.data_source_update_sink - allow(sink).to receive(:init).and_raise(StandardError, "store failure") + allow(sink).to receive(:init).and_raise(error_class, "store failure") ds = FileData.data_source(options.merge(paths: [ file.path ], auto_update: true)).call('', @config) listener = nil allow(ds).to receive(:start_listener).and_wrap_original do |original| listener = original.call end - expect { ds.start }.to raise_error(StandardError, "store failure") + expect { ds.start }.to raise_error(error_class, "store failure") yield listener expect { ds.stop }.not_to raise_error end @@ -413,6 +413,12 @@ def test_stop_watching_if_first_load_raises(options) end end + it "stops watching if the first load raises an exception that is not a StandardError" do + test_stop_watching_if_first_load_raises({}, Interrupt) do |listener| + expect(listener.stopped?).to eq(true) + end + end + it "stops polling if the first load raises" do test_stop_watching_if_first_load_raises({ force_polling: true, poll_interval: 0.1 }) do |listener| expect(listener.instance_variable_get(:@thread).join(2)).not_to be_nil diff --git a/spec/integrations/file_data_source_v2_spec.rb b/spec/integrations/file_data_source_v2_spec.rb index a4026a94..f4e8db33 100644 --- a/spec/integrations/file_data_source_v2_spec.rb +++ b/spec/integrations/file_data_source_v2_spec.rb @@ -476,6 +476,25 @@ def first_sync_update(source) expect(lock_held_at_stop).to eq(false) end + it "stops a file watcher whose start raises an exception that is not a StandardError" do + file = make_temp_file(all_properties_json) + source = Impl::Integrations::FileDataSourceV2.new(logger, paths: [file.path]) + error_class = Interrupt + listener = nil + allow(Listen).to receive(:to).and_wrap_original do |original, *args, **opts, &block| + listener = original.call(*args, **opts, &block) + allow(listener).to receive(:start).and_raise(error_class, "watch failure") + listener + end + + begin + expect { source.sync(no_selector_store) { |_update| break } }.to raise_error(error_class) + expect(listener.stopped?).to eq(true) + ensure + source.stop + end + end + it "stops the file watcher if the source stops while the watcher starts" do file = make_temp_file(all_properties_json) source = Impl::Integrations::FileDataSourceV2.new(logger, paths: [file.path]) From ed20ec010db73e3603883994d9308044318c060e Mon Sep 17 00:00:00 2001 From: Matthew Keeler Date: Fri, 20 Nov 2026 11:18:38 -0500 Subject: [PATCH 12/12] test: Let listen finish its startup snapshot before the override startup spec writes the file Off Linux the override watcher uses listen, which builds its snapshot on a background thread after start returns. The spec wrote the file at once, so the snapshot could take the new contents as its baseline. Wait 1 second before the write, as the other startup specs do. The write still occurs before start returns. --- spec/integrations/file_override_source_spec.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/integrations/file_override_source_spec.rb b/spec/integrations/file_override_source_spec.rb index 502cb6ff..0535bdd9 100644 --- a/spec/integrations/file_override_source_spec.rb +++ b/spec/integrations/file_override_source_spec.rb @@ -102,6 +102,7 @@ def write_after_initial_load(name, content) result = original.call(*args) if first first = false + sleep(1) File.write(path(name), content) end result