Skip to content

Commit 2adbc33

Browse files
committed
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.
1 parent 05c8dd2 commit 2adbc33

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

Flow.Launcher.Core/ExternalPlugins/PluginsManifest.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ public static class PluginsManifest
2626

2727
public static async Task<bool> UpdateManifestAsync(bool usePrimaryUrlOnly = false, CancellationToken token = default)
2828
{
29+
bool lockAcquired = false;
2930
try
3031
{
3132
await manifestUpdateLock.WaitAsync(token).ConfigureAwait(false);
33+
lockAcquired = true;
3234

3335
if (UserPlugins == null || usePrimaryUrlOnly || DateTime.Now.Subtract(lastFetchedAt) >= fetchTimeout)
3436
{
@@ -64,7 +66,8 @@ public static async Task<bool> UpdateManifestAsync(bool usePrimaryUrlOnly = fals
6466
}
6567
finally
6668
{
67-
manifestUpdateLock.Release();
69+
// Only release the lock if it was acquired
70+
if (lockAcquired) manifestUpdateLock.Release();
6871
}
6972

7073
return false;

Plugins/Flow.Launcher.Plugin.Explorer/Search/Everything/EverythingAPI.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,6 @@ public static async IAsyncEnumerable<SearchResult> SearchAsync(EverythingSearchO
133133
EverythingApiDllImport.Everything_SetRequestFlags(EVERYTHING_REQUEST_FULL_PATH_AND_FILE_NAME);
134134
}
135135

136-
137-
138136
if (token.IsCancellationRequested) yield break;
139137

140138
if (!EverythingApiDllImport.Everything_QueryW(true))

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,29 +86,41 @@ public async Task<List<Result>> QueryAsync(Query query, CancellationToken token)
8686
{
8787
// Preparing win32 programs
8888
List<Win32> win32s;
89+
bool win32LockAcquired = false;
8990
try
9091
{
9192
await _win32sLock.WaitAsync(token);
93+
win32LockAcquired = true;
9294
win32s = [.. _win32s];
9395
}
9496
catch (OperationCanceledException)
9597
{
9698
return emptyResults;
9799
}
98-
_win32sLock.Release();
100+
finally
101+
{
102+
// Only release the lock if it was acquired
103+
if (win32LockAcquired) _win32sLock.Release();
104+
}
99105

100106
// Preparing UWP programs
101107
List<UWPApp> uwps;
108+
bool uwpsLockAcquired = false;
102109
try
103110
{
104111
await _uwpsLock.WaitAsync(token);
112+
uwpsLockAcquired = true;
105113
uwps = [.. _uwps];
106114
}
107115
catch (OperationCanceledException)
108116
{
109117
return emptyResults;
110118
}
111-
_uwpsLock.Release();
119+
finally
120+
{
121+
// Only release the lock if it was acquired
122+
if (uwpsLockAcquired) _uwpsLock.Release();
123+
}
112124

113125
// Start querying programs
114126
try

0 commit comments

Comments
 (0)