Bug 51313 - NavigationPage.PopAsync throwing IndexOutOfBounds
Summary: NavigationPage.PopAsync throwing IndexOutOfBounds
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 2.3.3
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2017-01-09 21:24 UTC by Brandon Risell
Modified: 2017-02-15 14:09 UTC (History)
4 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Brandon Risell 2017-01-09 21:24:25 UTC
We ran into a bug where NavigationPage.PopAsync() was called from a couple threads very quickly. Both threads are able to get past a safety check, because neither has started yet. They then both try to remove the calling page from the backing collection. One succeeds and the other throws an index out of bounds exception.

You can see where it's checking if a current navigation is happening:

and then both threads continue onto:

Here is a console app that reproduces the issue:

class MainClass
    private static ManualResetEvent _mre = new ManualResetEvent(false);
    private static NavigationPage _nav = new NavigationPage();

    public static void Main(string[] args)
        var iterationCount = 0;
        while (true)
            System.Console.WriteLine($"Attempt #{iterationCount} to trigger the thread issue");

            var page1 = new ContentPage { Content = new Label() };
            var page2 = new ContentPage { Content = new Label() };


            var thread1 = new Thread(ThreadToPopNav);
            var thread2 = new Thread(ThreadToPopNav);






    private static void ThreadToPopNav()
        // Wait for signal

        // Pop it like its hot

        System.Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}: {DateTime.Now.Ticks}");
Comment 1 adrianknight89 2017-01-10 01:39:36 UTC
CurrentNavigationTask needs to be dealt with properly. I think it leads to a memory leak. Not sure if it's related to your issue though.
Comment 2 Stephane Delcroix 2017-02-15 14:09:45 UTC
you're not supposed to Push() or Pop() from anything but the main thread. If you do you can get unexpected result, like this one, but also there is a high chance that the platform rendering won't work correctly.