Fixed the .NET server shutting down while the runtime still runs (#1237)

This commit is contained in:
Thorsten Sommer authored and GitHub committed 2026-11-26 12:01:08 +01:00
1 parent 9ebfce3855
commit 902e406e1d
22 files changed
+923 -115

No files matched your search

@@ -10,6 +10,12 @@ internal sealed partial class FileSourceIndexer
{
private const int WATCHER_DEBOUNCE_SECONDS = 2;
/// <summary>
/// How long the changes of a data source are counted after the one which was logged, before their summary is logged.
/// </summary>
private const int WATCHER_SUMMARY_SECONDS = 30;
private readonly WatcherChangeTally watcherChangeTally = new();
private readonly ConcurrentDictionary<string, DataSourceWatcherRegistration> watchers = new(StringComparer.OrdinalIgnoreCase);
private readonly Dictionary<string, CancellationTokenSource> watcherDebounceTokens = new(StringComparer.OrdinalIgnoreCase);
private readonly Lock watcherDebounceLock = new();
@@ -130,23 +136,52 @@ internal sealed partial class FileSourceIndexer
private void OnWatchedDataSourceChanged(string dataSourceId, DataSourceWatcherConfiguration configuration, FileSystemEventArgs args)
{
if (!IsRelevantWatcherEvent(configuration, args))
var isRelevant = IsRelevantWatcherEvent(configuration, args);
//
// Only the first change of a burst gets a line of its own, and the others are counted,
// cf. WatcherChangeTally:
//
if (this.watcherChangeTally.Count(dataSourceId, args.FullPath, isRelevant))
{
logger.LogDebug(
"Ignoring file system change for data source '{DataSourceId}' at '{Path}' (event={ChangeType}) because the path is not part of the RAG index.",
dataSourceId,
args.FullPath,
args.ChangeType);
return;
if (isRelevant)
logger.LogDebug(
"Detected relevant file system change for data source '{DataSourceId}' at '{Path}' (event={ChangeType}). Scheduling a debounced embedding run. Further changes within {Seconds} s are summarized.",
dataSourceId,
args.FullPath,
args.ChangeType,
WATCHER_SUMMARY_SECONDS);
else
logger.LogDebug(
"Ignoring file system change for data source '{DataSourceId}' at '{Path}' (event={ChangeType}) because the path is not part of the RAG index. Further changes within {Seconds} s are summarized.",
dataSourceId,
args.FullPath,
args.ChangeType,
WATCHER_SUMMARY_SECONDS);
this.ScheduleWatcherSummary(dataSourceId);
}
logger.LogDebug(
"Detected relevant file system change for data source '{DataSourceId}' at '{Path}' (event={ChangeType}). Scheduling a debounced embedding run.",
dataSourceId,
args.FullPath,
args.ChangeType);
if (isRelevant)
this.ScheduleWatchedDataSourceRefresh(dataSourceId);
}
this.ScheduleWatchedDataSourceRefresh(dataSourceId);
private void ScheduleWatcherSummary(string dataSourceId)
{
Task.Run(async () =>
{
await Task.Delay(TimeSpan.FromSeconds(WATCHER_SUMMARY_SECONDS));
var counts = this.watcherChangeTally.Close(dataSourceId);
if (counts.Total > 0)
logger.LogDebug(
"The watcher of data source '{DataSourceId}' reported {FurtherChanges} further file system change(s) within {Seconds} s: {Relevant} concern the RAG index, {Ignored} do not. The last one was at '{Path}'.",
dataSourceId,
counts.Total,
WATCHER_SUMMARY_SECONDS,
counts.Relevant,
counts.Ignored,
counts.LastPath);
}).Observe($"{nameof(FileSourceIndexer)}: summarizing the file system changes of a data source");
}
private void ScheduleWatchedDataSourceRefresh(string dataSourceId)
@@ -0,0 +1,30 @@
namespace AIStudio.Tools.Services.Indexing;
/// <summary>
/// The file system changes of one data source which WatcherChangeTally counted instead of logging each.
/// </summary>
/// <param name="Ignored">How many changes concerned no file of the RAG index.</param>
/// <param name="Relevant">How many changes concerned the RAG index.</param>
/// <param name="LastPath">The path of the last change, or an empty string without any.</param>
internal readonly record struct WatcherChangeCounts(int Ignored, int Relevant, string LastPath)
{
/// <summary>
/// No change counted.
/// </summary>
public static readonly WatcherChangeCounts NONE = new(0, 0, string.Empty);
/// <summary>
/// How many changes were counted.
/// </summary>
public int Total => this.Ignored + this.Relevant;
/// <summary>
/// Counts one more change.
/// </summary>
/// <param name="path">The path of the change.</param>
/// <param name="isRelevant">Whether the change concerns the RAG index.</param>
/// <returns>The counts including this change.</returns>
public WatcherChangeCounts Add(string path, bool isRelevant) => isRelevant
? this with { Relevant = this.Relevant + 1, LastPath = path }
: this with { Ignored = this.Ignored + 1, LastPath = path };
}
@@ -0,0 +1,49 @@
namespace AIStudio.Tools.Services.Indexing;
/// <summary>
/// Counts the file system changes which the watchers report, so the log gets a summary instead of one line per change.
/// </summary>
/// <remarks>
/// A build, a checkout, or a sync tool changes thousands of files within seconds. With one log line each, such a
/// burst buried the log, and the log events piled up on their way to the runtime until it closed the connection
/// of the .NET server. Therefore, the first change of a data source opens a window and is logged as it comes;
/// every further change is only counted until the window closes, and its summary takes one line.
/// </remarks>
internal sealed class WatcherChangeTally
{
private readonly Lock tallyLock = new();
private readonly Dictionary<string, WatcherChangeCounts> openWindows = new(StringComparer.OrdinalIgnoreCase);
/// <summary>
/// Counts a change of a data source.
/// </summary>
/// <param name="dataSourceId">The id of the data source.</param>
/// <param name="path">The path of the change.</param>
/// <param name="isRelevant">Whether the change concerns the RAG index.</param>
/// <returns>True when the change opened a window: the caller logs it as it comes and closes the window later.</returns>
public bool Count(string dataSourceId, string path, bool isRelevant)
{
lock (this.tallyLock)
{
if (this.openWindows.TryGetValue(dataSourceId, out var counts))
{
this.openWindows[dataSourceId] = counts.Add(path, isRelevant);
return false;
}
this.openWindows[dataSourceId] = WatcherChangeCounts.NONE;
return true;
}
}
/// <summary>
/// Closes the window of a data source, so its next change opens a new one.
/// </summary>
/// <param name="dataSourceId">The id of the data source.</param>
/// <returns>The changes counted after the one which opened the window.</returns>
public WatcherChangeCounts Close(string dataSourceId)
{
lock (this.tallyLock)
return this.openWindows.Remove(dataSourceId, out var counts) ? counts : WatcherChangeCounts.NONE;
}
}