Skip to content

Lock the navigation stack so concurrent navigation keeps its history [patch] - #83

Merged
matt-edmondson merged 1 commit into
mainfrom
fix/57-thread-safe-navigation-stack
Oct 8, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
fix/57-thread-safe-navigation-stack

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #57

What

The README claims "Thread-safe operations", but Navigation<T> mutated _items and _currentIndex with no synchronization. Concurrent NavigateTo calls threw ArgumentException (the undo snapshot racing a List<T> resize) and could lose history. Per the maintainer's decision on the issue, this makes the claim true, scoped to NavigationStack:

  • One private Lock guards every read and write of _items and _currentIndex: Current, CanGoBack, CanGoForward, Count, NavigateTo, GoBack, GoForward, Clear, the history getters, RestoreState, LoadStateAsync and SaveStateAsync.
  • NavigationChanged is raised after the lock is released.
  • GetHistory() now returns a copy instead of a live AsReadOnly() view, so another thread's navigation can't change it while a caller enumerates it.
  • SaveStateAsync reads the items and the current index in one locked step, instead of two separate calls that another navigation could land between.
  • Undo actions are registered under the lock, so they reach the provider in the order the changes happened.
  • SimpleUndoRedoProvider is left alone because Delegate undo/redo to ktsu.UndoRedo #46 replaces it.

Test

NavigateTo_FromManyThreads_KeepsEveryItemAndAValidCurrent turns the issue's Parallel.For(0, 2000, …) repro into a test. It checks that nothing throws, that all 2000 distinct items are kept, that Current is the last item, and that the back stack holds 1999 entries.

  • Without the fix it fails with the ArgumentException from the issue. I checked this by stashing the source change and running the test again.
  • With the fix it passed 5 runs out of 5. The full suite passes: 44 tests, 2 skipped.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Qm7dX7JTJR6dTnKfSngfvp


Generated by Claude Code

…[patch]

NavigationStack mutated _items and _currentIndex with no synchronization, so
the README's "Thread-safe operations" claim did not hold: concurrent NavigateTo
calls threw ArgumentException from the undo snapshot racing a List resize and
could lose history entries.

Every read and write of the history and current index now happens under one
private lock, including the undo snapshots, GetHistory, RestoreState,
LoadStateAsync and SaveStateAsync. NavigationChanged is raised after the lock
is released. GetHistory now returns a copy rather than a live view, so it
cannot change under a caller enumerating it. Undo actions are registered under
the lock so they reach the provider in the order the changes were made.

Fixes #57

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Qm7dX7JTJR6dTnKfSngfvp
@sonarqubecloud

sonarqubecloud Bot commented Oct 7, 2026

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Concurrent NavigateTo throws ArgumentException and corrupts history, despite README's "Thread-safe operations"

2 participants