Skip to content

Stacks sharing a SimpleUndoRedoProvider (the NavigationStackFactory default) lose undo entries or throw NullReferenceException under concurrent navigation #89

Open

Description

@matt-edmondson

What's wrong

SimpleUndoRedoProvider (Navigation/Services/SimpleUndoRedoProvider.cs) wraps an unsynchronized undo/redo service. Its RegisterAction (about line 57), Undo (about line 66) and Redo (about line 92) take no lock.

NavigationStackFactory (Navigation/Services/NavigationStackFactory.cs, about lines 16 and 27) gives every stack it creates the same default provider. NavigationStack.NavigateTo calls RegisterAction under the stack's own lock (about line 355), but that lock does nothing for a provider shared across stacks. provider.Undo() runs outside any stack lock at all.

The README advertises "✅ Thread-safe operations". #57 asked for the same protection on SimpleUndoRedoProvider, but it was closed by #83, which locked only Navigation<T>.

Failure scenario (reproduced, 20/20 runs failed)

var factory = new NavigationStackFactory(new SimpleUndoRedoProvider(100_000));
var a = factory.Create...(); var b = factory.Create...();
Parallel.Invoke(
    () => { for (int i = 0; i < 5000; i++) a.NavigateTo(...); },
    () => { for (int i = 0; i < 5000; i++) b.NavigateTo(...); });
// expected provider.GetUndoStack().Count == 10000

Most runs ended with 9,939 to 9,999 entries, and three threw NullReferenceException. A concurrent provider.Undo() while a single stack navigates hits the same race.

Suggested fix

  • Add a private lock in SimpleUndoRedoProvider covering RegisterAction, Clear, CanUndo/CanRedo and GetUndoStack/GetRedoStack.
  • For Undo/Redo, do not run the action while holding the provider lock. action.Undo → RestoreState takes the stack lock, while NavigateTo holds the stack lock and then calls RegisterAction, so that ordering would deadlock. Instead, pop the action and move the position under the lock, release it, run the action, and re-lock to revert if it throws.
  • Alternatively, if the provider is not meant to be shared across threads, document that, stop sharing a single default instance across factory-created stacks, and qualify the README claim.

Acceptance criteria

  • The parallel repro always yields 10,000 undo entries with no exception.
  • A concurrent Undo + NavigateTo test neither deadlocks nor throws.

Activity

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

    @matt-edmondson
    ContributorAuthor

    Triage


    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