Skip to content

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

Open

Description

@matt-edmondson

What's wrong

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):

var undo = new SimpleUndoRedoProvider();
var nav = new Navigation<NavigationItem>(undo);
bool navOk = false, stateOk = false;
nav.NavigationChanged += (_, _) => navOk  = Task.Run(() => nav.CanGoBack).Wait(2000);
undo.StateChanged     += (_, _) => stateOk = Task.Run(() => nav.CanGoBack).Wait(2000);
nav.NavigateTo(new NavigationItem("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:

  1. A background thread calls nav.NavigateTo(...).
  2. undo.StateChanged += (_, _) => Dispatcher.Invoke(() => UndoButton.IsEnabled = undo.CanUndo); runs, holding _sync, and waits for the UI thread.
  3. 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.

Activity

  1. added
    bug
    on Oct 19, 2026
  2. matt-edmondson commented on Oct 19, 2026

    @matt-edmondson
    ContributorAuthor

    Triage

    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.


    Generated by Claude Code

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions