Skip to content

Commit 04b9790

Browse files
CopilotJack251970
andcommitted
Store and await indexing task to prevent incomplete program indexing
Co-authored-by: Jack251970 <[email protected]>
1 parent 118afd2 commit 04b9790

File tree

1 file changed

+62
-4
lines changed
  • Plugins/Flow.Launcher.Plugin.Program

1 file changed

+62
-4
lines changed

Plugins/Flow.Launcher.Plugin.Program/Main.cs

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ public class Main : ISettingProvider, IAsyncPlugin, IPluginI18n, IContextMenu, I
3131

3232
internal static PluginInitContext Context { get; private set; }
3333

34+
private static Task _indexingTask;
35+
private static readonly object _indexingTaskLock = new();
36+
3437
private static readonly List<Result> emptyResults = [];
3538

3639
private static readonly MemoryCacheOptions cacheOptions = new() { SizeLimit = 1560 };
@@ -78,6 +81,31 @@ public class Main : ISettingProvider, IAsyncPlugin, IPluginI18n, IContextMenu, I
7881

7982
public async Task<List<Result>> QueryAsync(Query query, CancellationToken token)
8083
{
84+
// Wait for initial indexing to complete if it's still running
85+
Task indexingTask;
86+
lock (_indexingTaskLock)
87+
{
88+
indexingTask = _indexingTask;
89+
}
90+
91+
if (indexingTask != null && !indexingTask.IsCompleted)
92+
{
93+
try
94+
{
95+
// Wait for indexing with a reasonable timeout to avoid blocking queries indefinitely
96+
await Task.WhenAny(indexingTask, Task.Delay(TimeSpan.FromSeconds(30), token)).ConfigureAwait(false);
97+
}
98+
catch (OperationCanceledException)
99+
{
100+
// Query was cancelled, return empty results
101+
return emptyResults.ToList();
102+
}
103+
catch (Exception e)
104+
{
105+
Context.API.LogException(ClassName, "Error waiting for indexing to complete", e);
106+
}
107+
}
108+
81109
var result = await cache.GetOrCreateAsync(query.Search, async entry =>
82110
{
83111
var resultList = await Task.Run(async () =>
@@ -277,11 +305,21 @@ static void MoveFile(string sourcePath, string destinationPath)
277305

278306
if (cacheEmpty || _settings.LastIndexTime.AddHours(30) < DateTime.Now)
279307
{
280-
_ = Task.Run(async () =>
308+
lock (_indexingTaskLock)
281309
{
282-
await IndexProgramsAsync().ConfigureAwait(false);
283-
WatchProgramUpdate();
284-
});
310+
_indexingTask = Task.Run(async () =>
311+
{
312+
try
313+
{
314+
await IndexProgramsAsync().ConfigureAwait(false);
315+
WatchProgramUpdate();
316+
}
317+
catch (Exception e)
318+
{
319+
Context.API.LogException(ClassName, "Failed to complete program indexing", e);
320+
}
321+
});
322+
}
285323
}
286324
else
287325
{
@@ -483,6 +521,26 @@ public async Task ReloadDataAsync()
483521

484522
public void Dispose()
485523
{
524+
// Wait for indexing to complete before disposing
525+
Task indexingTask;
526+
lock (_indexingTaskLock)
527+
{
528+
indexingTask = _indexingTask;
529+
}
530+
531+
if (indexingTask != null && !indexingTask.IsCompleted)
532+
{
533+
try
534+
{
535+
// Wait for indexing to complete with a reasonable timeout
536+
indexingTask.Wait(TimeSpan.FromSeconds(10));
537+
}
538+
catch (Exception e)
539+
{
540+
Context?.API?.LogException(ClassName, "Error waiting for indexing during dispose", e);
541+
}
542+
}
543+
486544
Win32.Dispose();
487545
}
488546
}

0 commit comments

Comments
 (0)