[PTRun] Implement thread-safety in query results task with lock (#34009)

* [PTRun] Implement thread-safety in query results task with lock

* [PTRun] Lock is moved to Unit Converter Query function.

* [PTRun]  _updateSource.Token is used instead of local currentCancellationToken to avoid dangling reference.
This commit is contained in:
gokcekantarci 2024-08-22 17:11:59 +03:00 committed by GitHub
parent 744c53cfcd
commit bfa35d65a4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 19 deletions

View File

@ -50,7 +50,6 @@ namespace Community.PowerToys.Run.Plugin.UnitConverter
return new List<Result>(); return new List<Result>();
} }
// Convert
return UnitHandler.Convert(convertModel) return UnitHandler.Convert(convertModel)
.Select(x => GetResult(x)) .Select(x => GetResult(x))
.ToList(); .ToList();

View File

@ -593,8 +593,7 @@ namespace PowerLauncher.ViewModel
_updateSource?.Dispose(); _updateSource?.Dispose();
var currentUpdateSource = new CancellationTokenSource(); var currentUpdateSource = new CancellationTokenSource();
_updateSource = currentUpdateSource; _updateSource = currentUpdateSource;
var currentCancellationToken = _updateSource.Token; _updateToken = _updateSource.Token;
_updateToken = currentCancellationToken;
var queryText = QueryText.Trim(); var queryText = QueryText.Trim();
var pluginQueryPairs = QueryBuilder.Build(queryText); var pluginQueryPairs = QueryBuilder.Build(queryText);
@ -631,7 +630,7 @@ namespace PowerLauncher.ViewModel
query.SelectedItems = _userSelectedRecord.GetGenericHistory(); query.SelectedItems = _userSelectedRecord.GetGenericHistory();
var results = PluginManager.QueryForPlugin(plugin, query); var results = PluginManager.QueryForPlugin(plugin, query);
resultPluginPair[plugin.Metadata] = results; resultPluginPair[plugin.Metadata] = results;
currentCancellationToken.ThrowIfCancellationRequested(); _updateToken.ThrowIfCancellationRequested();
} }
catch (OperationCanceledException) catch (OperationCanceledException)
{ {
@ -642,7 +641,7 @@ namespace PowerLauncher.ViewModel
} }
else else
{ {
currentCancellationToken.ThrowIfCancellationRequested(); _updateToken.ThrowIfCancellationRequested();
// To execute a query corresponding to each plugin // To execute a query corresponding to each plugin
foreach (KeyValuePair<PluginPair, Query> pluginQueryItem in pluginQueryPairs) foreach (KeyValuePair<PluginPair, Query> pluginQueryItem in pluginQueryPairs)
@ -652,7 +651,7 @@ namespace PowerLauncher.ViewModel
query.SelectedItems = _userSelectedRecord.GetGenericHistory(); query.SelectedItems = _userSelectedRecord.GetGenericHistory();
var results = PluginManager.QueryForPlugin(plugin, query); var results = PluginManager.QueryForPlugin(plugin, query);
resultPluginPair[plugin.Metadata] = results; resultPluginPair[plugin.Metadata] = results;
currentCancellationToken.ThrowIfCancellationRequested(); _updateToken.ThrowIfCancellationRequested();
} }
} }
@ -664,11 +663,11 @@ namespace PowerLauncher.ViewModel
Results.Clear(); Results.Clear();
foreach (var p in resultPluginPair) foreach (var p in resultPluginPair)
{ {
UpdateResultView(p.Value, queryText, currentCancellationToken); UpdateResultView(p.Value, queryText, _updateToken);
currentCancellationToken.ThrowIfCancellationRequested(); _updateToken.ThrowIfCancellationRequested();
} }
currentCancellationToken.ThrowIfCancellationRequested(); _updateToken.ThrowIfCancellationRequested();
numResults = Results.Results.Count; numResults = Results.Results.Count;
if (!doFinalSort) if (!doFinalSort)
{ {
@ -677,7 +676,7 @@ namespace PowerLauncher.ViewModel
} }
} }
currentCancellationToken.ThrowIfCancellationRequested(); _updateToken.ThrowIfCancellationRequested();
if (!doFinalSort) if (!doFinalSort)
{ {
UpdateResultsListViewAfterQuery(queryText); UpdateResultsListViewAfterQuery(queryText);
@ -689,7 +688,7 @@ namespace PowerLauncher.ViewModel
if (!delayedExecution.HasValue || delayedExecution.Value) if (!delayedExecution.HasValue || delayedExecution.Value)
{ {
// Run the slower query of the DelayedExecution plugins // Run the slower query of the DelayedExecution plugins
currentCancellationToken.ThrowIfCancellationRequested(); _updateToken.ThrowIfCancellationRequested();
Parallel.ForEach(plugins, (plugin) => Parallel.ForEach(plugins, (plugin) =>
{ {
try try
@ -697,7 +696,7 @@ namespace PowerLauncher.ViewModel
Query query; Query query;
pluginQueryPairs.TryGetValue(plugin, out query); pluginQueryPairs.TryGetValue(plugin, out query);
var results = PluginManager.QueryForPlugin(plugin, query, true); var results = PluginManager.QueryForPlugin(plugin, query, true);
currentCancellationToken.ThrowIfCancellationRequested(); _updateToken.ThrowIfCancellationRequested();
if ((results?.Count ?? 0) != 0) if ((results?.Count ?? 0) != 0)
{ {
lock (_addResultsLock) lock (_addResultsLock)
@ -705,16 +704,16 @@ namespace PowerLauncher.ViewModel
// Using CurrentCultureIgnoreCase since this is user facing // Using CurrentCultureIgnoreCase since this is user facing
if (queryText.Equals(_currentQuery, StringComparison.CurrentCultureIgnoreCase)) if (queryText.Equals(_currentQuery, StringComparison.CurrentCultureIgnoreCase))
{ {
currentCancellationToken.ThrowIfCancellationRequested(); _updateToken.ThrowIfCancellationRequested();
// Remove the original results from the plugin // Remove the original results from the plugin
Results.Results.RemoveAll(r => r.Result.PluginID == plugin.Metadata.ID); Results.Results.RemoveAll(r => r.Result.PluginID == plugin.Metadata.ID);
currentCancellationToken.ThrowIfCancellationRequested(); _updateToken.ThrowIfCancellationRequested();
// Add the new results from the plugin // Add the new results from the plugin
UpdateResultView(results, queryText, currentCancellationToken); UpdateResultView(results, queryText, _updateToken);
currentCancellationToken.ThrowIfCancellationRequested(); _updateToken.ThrowIfCancellationRequested();
numResults = Results.Results.Count; numResults = Results.Results.Count;
if (!doFinalSort) if (!doFinalSort)
{ {
@ -722,7 +721,7 @@ namespace PowerLauncher.ViewModel
} }
} }
currentCancellationToken.ThrowIfCancellationRequested(); _updateToken.ThrowIfCancellationRequested();
if (!doFinalSort) if (!doFinalSort)
{ {
UpdateResultsListViewAfterQuery(queryText, noInitialResults, true); UpdateResultsListViewAfterQuery(queryText, noInitialResults, true);
@ -751,7 +750,7 @@ namespace PowerLauncher.ViewModel
}; };
PowerToysTelemetry.Log.WriteEvent(queryEvent); PowerToysTelemetry.Log.WriteEvent(queryEvent);
}, },
currentCancellationToken); _updateToken);
if (doFinalSort) if (doFinalSort)
{ {
@ -763,7 +762,7 @@ namespace PowerLauncher.ViewModel
Results.SelectedItem = Results.Results.FirstOrDefault(); Results.SelectedItem = Results.Results.FirstOrDefault();
UpdateResultsListViewAfterQuery(queryText, false, false); UpdateResultsListViewAfterQuery(queryText, false, false);
}, },
currentCancellationToken); _updateToken);
} }
} }
} }