Skip to content

Commit e3aa5b0

Browse files
bertyhellclaude
andcommitted
fix: do not read selected commits from background threads when generating the graph
- Collect the selected commits used as extra graph heads on the UI thread before starting background work - Regenerate the graph if the selection changed while a history refresh was running - Guard against `_histories` being null in `Repository.RefreshCommits` Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 5bc64f6 commit e3aa5b0

2 files changed

Lines changed: 35 additions & 11 deletions

File tree

‎src/ViewModels/Histories.cs‎

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ public void ToggleShowFlag(Models.HistoryShowFlags flag)
249249
_repo.RefreshCommits();
250250
}
251251

252-
public void SetCommits(List<Models.Commit> commits, Models.CommitGraph graph)
252+
public void SetCommits(List<Models.Commit> commits, Models.CommitGraph graph, HashSet<string> graphExtraHeads)
253253
{
254254
if (_cancellationRefreshGraph is { IsCancellationRequested: false })
255255
_cancellationRefreshGraph.Cancel();
@@ -258,20 +258,34 @@ public void SetCommits(List<Models.Commit> commits, Models.CommitGraph graph)
258258

259259
if (SetProperty(ref _commits, commits, nameof(Commits)))
260260
PostCommitsChanged();
261+
262+
// The selection may have changed while the graph was generated in background.
263+
if (!graphExtraHeads.SetEquals(CollectGraphExtraHeads()))
264+
RefreshGraph();
261265
}
262266

263-
public Models.CommitGraph GenerateGraph(List<Models.Commit> commits)
267+
/// <summary>
268+
/// Must be called on the UI thread: `_selectedCommits` is only modified there.
269+
/// </summary>
270+
public HashSet<string> CollectGraphExtraHeads()
264271
{
265-
var firstParentOnly = _repo.UIStates.HistoryShowFlags.HasFlag(Models.HistoryShowFlags.FirstParentOnly);
266-
var highlighting = _repo.UIStates.GraphHighlighting;
267272
var extraHeads = new HashSet<string>();
268-
269-
if (highlighting >= Models.CommitGraphHighlighting.SelectedCommitsOnly)
273+
if (_repo.UIStates.GraphHighlighting >= Models.CommitGraphHighlighting.SelectedCommitsOnly)
270274
{
271275
foreach (var c in _selectedCommits)
272276
extraHeads.Add(c.SHA);
273277
}
274278

279+
return extraHeads;
280+
}
281+
282+
/// <summary>
283+
/// Thread-safe as long as `extraHeads` is not shared with the UI thread.
284+
/// </summary>
285+
public Models.CommitGraph GenerateGraph(List<Models.Commit> commits, HashSet<string> extraHeads)
286+
{
287+
var firstParentOnly = _repo.UIStates.HistoryShowFlags.HasFlag(Models.HistoryShowFlags.FirstParentOnly);
288+
var highlighting = _repo.UIStates.GraphHighlighting;
275289
return Models.CommitGraph.Generate(commits, firstParentOnly, highlighting, extraHeads);
276290
}
277291

@@ -596,10 +610,11 @@ private void RefreshGraph()
596610
_cancellationRefreshGraph = new CancellationTokenSource();
597611
var token = _cancellationRefreshGraph.Token;
598612
var commits = _commits;
613+
var extraHeads = CollectGraphExtraHeads();
599614

600615
Task.Run(() =>
601616
{
602-
var graph = GenerateGraph(commits);
617+
var graph = GenerateGraph(commits, extraHeads);
603618
Dispatcher.UIThread.Post(() =>
604619
{
605620
if (!token.IsCancellationRequested)

‎src/ViewModels/Repository.cs‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,7 +1255,15 @@ public void RefreshCommits()
12551255

12561256
Task.Run(async () =>
12571257
{
1258-
await Dispatcher.UIThread.InvokeAsync(() => _histories.IsLoading = true);
1258+
var graphExtraHeads = new HashSet<string>();
1259+
await Dispatcher.UIThread.InvokeAsync(() =>
1260+
{
1261+
if (_histories == null)
1262+
return;
1263+
1264+
_histories.IsLoading = true;
1265+
graphExtraHeads = _histories.CollectGraphExtraHeads();
1266+
});
12591267

12601268
var builder = new StringBuilder();
12611269
builder
@@ -1292,10 +1300,11 @@ public void RefreshCommits()
12921300
}
12931301
}
12941302

1295-
if (token.IsCancellationRequested)
1303+
var histories = _histories;
1304+
if (token.IsCancellationRequested || histories == null)
12961305
return;
12971306

1298-
var graph = _histories.GenerateGraph(commits);
1307+
var graph = histories.GenerateGraph(commits, graphExtraHeads);
12991308

13001309
Dispatcher.UIThread.Invoke(() =>
13011310
{
@@ -1305,7 +1314,7 @@ public void RefreshCommits()
13051314
if (_histories != null)
13061315
{
13071316
_histories.IsLoading = false;
1308-
_histories.SetCommits(commits, graph);
1317+
_histories.SetCommits(commits, graph, graphExtraHeads);
13091318
BisectState = _histories.UpdateBisectInfo();
13101319

13111320
if (!string.IsNullOrEmpty(_navigateToCommitDelayed))

0 commit comments

Comments
 (0)