Skip to content

fix: Prevent the file data source from missing a file change made during startup - #497

Open
keelerm84 wants to merge 12 commits into
mainfrom
mk/SDK-3567/file-watcher-race
Open

keelerm84 wants to merge 12 commits into
mainfrom
mk/SDK-3567/file-watcher-race

Conversation

@keelerm84

@keelerm84 keelerm84 commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

fix: Prevent the file override 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.


Note

Overview
Fixes a startup race where file-backed flag/override sources read files before starting change detection, so edits between read and watcher start were ignored until the next change.

File data (v1 and FDv2) now start the watcher (or poller) before the first load/sync fetch. V1 adds a @load_lock so concurrent reloads during startup cannot let an older read overwrite newer data in the store. Startup also stops the watcher if the first load fails, logs and continues when watching cannot start, and cleans up partially started Listen instances; FDv2 stops listeners outside the mutex and skips watching when no valid paths exist.

File override source follows the same idea: the change detector is registered before reload_now, so changes during the initial read are picked up.

Docs note that on non-Linux platforms, listen may still miss a change that happens exactly during startup until the file changes again.

Reviewed by Cursor Bugbot for commit ed20ec0. Bugbot is set up for automated code reviews on this repo. Configure here.

…ing 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.
@keelerm84
keelerm84 requested a review from a team as a code owner November 12, 2026 22:06

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

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.
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.
… 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.
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.
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.
… 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.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit ddb72a9. Configure here.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Risk: medium.

Automated risk review (dry run): Medium

This PR would not be automatically approved. Human approval required.

Why: The PR changes watcher startup, load locking, and shutdown in FileDataSourceImpl, FileDataSourceV2, and FileOverrideSource, and Cursor Bugbot has an unresolved medium-severity deadlock finding on watcher stop. Human review is needed and no reviewers were assigned.

If you disagree with this review, share this PR in #feedback-agent-approvals or submit a changes-requested review explaining why.

Open in Web View Automation 

Sent by Cursor Approval Agent: Dry-Run PR Approver

@cursor cursor Bot added the risk:medium label Nov 17, 2026
…tes 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.
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.

@kinyoklion kinyoklion left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that if the watcher fails to start we still need to at least still try to load the file.

We may also want to address these:

 - FDv1 start uses a bare rescue. If the first load raises something that isn't a StandardError (such as Interrupt), the watcher it already started is never stopped. The base didn't
     have this leak because it started the watcher after the load.
 - The new override watching test can flake off Linux. It writes the file before listen has finished its startup snapshot. Windows CI passed, but the race is real.

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.
… 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.
…tup 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.
@keelerm84

Copy link
Copy Markdown
Member Author

Thanks. All three are fixed:

  • 486d95e: if the v1 file watcher cannot start, the data source logs the error and still loads the files, as FDv2 does since 103faa3. A missing path with auto-update now reports the load error instead of raising from start.
  • 58d1b2b: the cleanup in v1 start and in both listen starts now uses ensure, so the watcher stops for any exception, including Interrupt.
  • ed20ec0: the override startup specs now wait 1 second before the write, so listen can finish its snapshot. The write still occurs before start returns, so the specs still fail on the old order.

@keelerm84
keelerm84 requested a review from kinyoklion November 20, 2026 19:40

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants