You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
IUndoRedoProvider.StateChanged handlers run while the stack's lock is held, so a handler that marshals to the UI thread (Dispatcher.Invoke) deadlocks against a UI-thread read of Current/CanGoBack #90
NavigateTo, GoBack, GoForward, Clear and LoadStateAsync in Navigation/Services/NavigationStack.cs (lines ~102, 132, 158, 177, 275) call RegisterUndo(...) inside lock (_sync). RegisterUndo (lines ~340-349) calls undoRedoProvider.RegisterAction(...), and SimpleUndoRedoProvider.RegisterAction (Navigation/Services/SimpleUndoRedoProvider.cs:58-64) raises StateChanged synchronously. So every StateChanged handler runs with _sync held.
The class remarks (NavigationStack.cs:19-20) promise the stack is usable from several threads and that NavigationChanged is raised after the lock is released. StateChanged gets no such guarantee, and the obvious handler for it — enabling an Undo button — typically marshals to another thread.
Failure scenario
Repro (verified on .NET 10):
varundo=newSimpleUndoRedoProvider();varnav=newNavigation<NavigationItem>(undo);boolnavOk=false,stateOk=false;nav.NavigationChanged+=(_,_)=>navOk=Task.Run(()=>nav.CanGoBack).Wait(2000);undo.StateChanged+=(_,_)=>stateOk=Task.Run(()=>nav.CanGoBack).Wait(2000);nav.NavigateTo(newNavigationItem("a","A"));// navOk == true, stateOk == false: the other-thread read blocks on _sync until the handler returns
In a UI app this becomes a real deadlock:
A background thread calls nav.NavigateTo(...).
undo.StateChanged += (_, _) => Dispatcher.Invoke(() => UndoButton.IsEnabled = undo.CanUndo); runs, holding _sync, and waits for the UI thread.
The UI thread is reading nav.Current / nav.CanGoBack (e.g. via a binding) and waits for _sync.
Neither thread can proceed.
Suggested fix
Stop calling RegisterAction while _sync is held, while keeping actions registered in the order the changes were made (the reason the call is currently under the lock, per the RegisterUndo remarks):
Simplest: a dedicated outer _registerSync lock that only orders registrations — take _registerSync, take _sync, mutate and build the action, release _sync, call RegisterAction, release _registerSync. Handlers then run without _sync, so reads from any thread don't block; only other mutations wait.
Document which events run under which lock.
Acceptance criteria
The repro above sets stateOk == true.
A regression test with a StateChanged handler that reads the stack from another thread and waits with a timeout.
Undo order is unchanged under concurrent mutations.
Related but distinct: #89 (provider lock vs. stack lock when several stacks share a provider) touches the same code path.
Priority: High. It is a real deadlock for the library's main use case: a UI app with a StateChanged handler that marshals to the UI thread (Dispatcher.Invoke) while the UI thread reads Current/CanGoBack. The deadlock needs no unusual input, there is no workaround short of avoiding synchronous marshalling in the handler, and it contradicts the class remarks that the stack is safe to use from several threads.
Area:Navigation/Services/NavigationStack.cs (RegisterUndo called under lock (_sync) from NavigateTo/GoBack/GoForward/Clear/LoadStateAsync) and SimpleUndoRedoProvider.RegisterAction raising StateChanged synchronously.
Next step: the proposed outer _registerSync ordering lock (mutate under _sync, release it, then RegisterAction) looks right. Add the repro as a regression test with a timeout, plus a concurrent-mutation test asserting undo order.
Activity
matt-edmondson commentedon Oct 19, 2026
Triage
StateChangedhandler that marshals to the UI thread (Dispatcher.Invoke) while the UI thread readsCurrent/CanGoBack. The deadlock needs no unusual input, there is no workaround short of avoiding synchronous marshalling in the handler, and it contradicts the class remarks that the stack is safe to use from several threads.Navigation/Services/NavigationStack.cs(RegisterUndocalled underlock (_sync)fromNavigateTo/GoBack/GoForward/Clear/LoadStateAsync) andSimpleUndoRedoProvider.RegisterActionraisingStateChangedsynchronously.NavigationChangedhandler during Undo) touch the sameRegisterUndopath, so a fix here should be designed with them in mind. Ideally one lock-ordering change covers Stacks sharing a SimpleUndoRedoProvider (the NavigationStackFactory default) lose undo entries or throw NullReferenceException under concurrent navigation #89 and IUndoRedoProvider.StateChanged handlers run while the stack's lock is held, so a handler that marshals to the UI thread (Dispatcher.Invoke) deadlocks against a UI-thread read of Current/CanGoBack #90 together. Navigation is pinned to the retired ktsu.UndoRedo.Core 1.1.4, and moving to ktsu.UndoRedo 3.x makes a failed undo re-run an unrelated action (tests stay green) #82 (move toktsu.UndoRedo3.x) may change the provider's event semantics, so check which lands first.Next step: the proposed outer
_registerSyncordering lock (mutate under_sync, release it, thenRegisterAction) looks right. Add the repro as a regression test with a timeout, plus a concurrent-mutation test asserting undo order.Generated by Claude Code