Improve semaphore lock handling and code robustness

Added `lockAcquired` flags in `PluginsManifest.cs` and `Main.cs`
to ensure semaphore locks are only released if successfully
acquired, preventing potential runtime errors. Updated `finally`
blocks to conditionally release locks based on these flags.

Removed redundant cancellation check in `EverythingAPI.cs` to
simplify code, assuming cancellation is handled elsewhere. These
changes enhance reliability and maintainability of the codebase.
This commit is contained in:
Jack251970 2025-11-07 15:30:07 +08:00
parent 05c8dd2fe1
commit 2adbc334a2
3 changed files with 18 additions and 5 deletions

View file

@ -26,9 +26,11 @@ namespace Flow.Launcher.Core.ExternalPlugins
public static async Task<bool> UpdateManifestAsync(bool usePrimaryUrlOnly = false, CancellationToken token = default)
{
bool lockAcquired = false;
try
{
await manifestUpdateLock.WaitAsync(token).ConfigureAwait(false);
lockAcquired = true;
if (UserPlugins == null || usePrimaryUrlOnly || DateTime.Now.Subtract(lastFetchedAt) >= fetchTimeout)
{
@ -64,7 +66,8 @@ namespace Flow.Launcher.Core.ExternalPlugins
}
finally
{
manifestUpdateLock.Release();
// Only release the lock if it was acquired
if (lockAcquired) manifestUpdateLock.Release();
}
return false;

View file

@ -133,8 +133,6 @@ namespace Flow.Launcher.Plugin.Explorer.Search.Everything
EverythingApiDllImport.Everything_SetRequestFlags(EVERYTHING_REQUEST_FULL_PATH_AND_FILE_NAME);
}
if (token.IsCancellationRequested) yield break;
if (!EverythingApiDllImport.Everything_QueryW(true))

View file

@ -86,29 +86,41 @@ namespace Flow.Launcher.Plugin.Program
{
// Preparing win32 programs
List<Win32> win32s;
bool win32LockAcquired = false;
try
{
await _win32sLock.WaitAsync(token);
win32LockAcquired = true;
win32s = [.. _win32s];
}
catch (OperationCanceledException)
{
return emptyResults;
}
_win32sLock.Release();
finally
{
// Only release the lock if it was acquired
if (win32LockAcquired) _win32sLock.Release();
}
// Preparing UWP programs
List<UWPApp> uwps;
bool uwpsLockAcquired = false;
try
{
await _uwpsLock.WaitAsync(token);
uwpsLockAcquired = true;
uwps = [.. _uwps];
}
catch (OperationCanceledException)
{
return emptyResults;
}
_uwpsLock.Release();
finally
{
// Only release the lock if it was acquired
if (uwpsLockAcquired) _uwpsLock.Release();
}
// Start querying programs
try