Skip to content

Commit 75f51c6

Browse files
authored
fix: Update FileDataSource to process reload triggers under lock. (#199)
Put the load under lock so only a single load is happening concurrently. <!-- CURSOR_SUMMARY --> --- > [!NOTE] > Synchronizes `LoadAll()` in `FileDataSource` with a lock to prevent concurrent reloads from file watcher events. > > - **Server SDK – File data loading**: > - Add `_updateLock` and wrap `LoadAll()` body in `lock` to serialize reloads and avoid concurrent loads from file watcher. > - All versioning, parsing, and `_dataSourceUpdates.Init(...)` now occur within the critical section in `pkgs/sdk/server/src/Internal/DataSources/FileDataSource.cs`. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit b32dc5a. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent 6c63264 commit 75f51c6

File tree

1 file changed

+30
-25
lines changed

1 file changed

+30
-25
lines changed

pkgs/sdk/server/src/Internal/DataSources/FileDataSource.cs

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ internal sealed class FileDataSource : IDataSource
2626
private volatile bool _started;
2727
private volatile bool _loadedValidData;
2828
private volatile int _lastVersion;
29+
private object _updateLock = new object();
2930

3031
public FileDataSource(IDataSourceUpdates dataSourceUpdates, FileDataTypes.IFileReader fileReader,
3132
List<string> paths, bool autoUpdate, Func<string, object> alternateParser, bool skipMissingPaths,
@@ -88,35 +89,39 @@ private void Dispose(bool disposing)
8889

8990
private void LoadAll()
9091
{
91-
var version = Interlocked.Increment(ref _lastVersion);
92-
var flags = new Dictionary<string, ItemDescriptor>();
93-
var segments = new Dictionary<string, ItemDescriptor>();
94-
foreach (var path in _paths)
92+
lock (_updateLock)
9593
{
96-
try
97-
{
98-
var content = _fileReader.ReadAllText(path);
99-
_logger.Debug("file data: {0}", content);
100-
var data = _parser.Parse(content, version);
101-
_dataMerger.AddToData(data, flags, segments);
102-
}
103-
catch (FileNotFoundException) when (_skipMissingPaths)
94+
var version = Interlocked.Increment(ref _lastVersion);
95+
var flags = new Dictionary<string, ItemDescriptor>();
96+
var segments = new Dictionary<string, ItemDescriptor>();
97+
foreach (var path in _paths)
10498
{
105-
_logger.Debug("{0}: {1}", path, "File not found");
106-
}
107-
catch (Exception e)
108-
{
109-
LogHelpers.LogException(_logger, "Failed to load " + path, e);
110-
return;
99+
try
100+
{
101+
var content = _fileReader.ReadAllText(path);
102+
_logger.Debug("file data: {0}", content);
103+
var data = _parser.Parse(content, version);
104+
_dataMerger.AddToData(data, flags, segments);
105+
}
106+
catch (FileNotFoundException) when (_skipMissingPaths)
107+
{
108+
_logger.Debug("{0}: {1}", path, "File not found");
109+
}
110+
catch (Exception e)
111+
{
112+
LogHelpers.LogException(_logger, "Failed to load " + path, e);
113+
return;
114+
}
111115
}
116+
117+
var allData = new FullDataSet<ItemDescriptor>(
118+
ImmutableDictionary.Create<DataKind, KeyedItems<ItemDescriptor>>()
119+
.SetItem(DataModel.Features, new KeyedItems<ItemDescriptor>(flags))
120+
.SetItem(DataModel.Segments, new KeyedItems<ItemDescriptor>(segments))
121+
);
122+
_dataSourceUpdates.Init(allData);
123+
_loadedValidData = true;
112124
}
113-
var allData = new FullDataSet<ItemDescriptor>(
114-
ImmutableDictionary.Create<DataKind, KeyedItems<ItemDescriptor>>()
115-
.SetItem(DataModel.Features, new KeyedItems<ItemDescriptor>(flags))
116-
.SetItem(DataModel.Segments, new KeyedItems<ItemDescriptor>(segments))
117-
);
118-
_dataSourceUpdates.Init(allData);
119-
_loadedValidData = true;
120125
}
121126

122127
private void TriggerReload()

0 commit comments

Comments
 (0)