{"record":{"id":"a29d849ed34b4e09","repo":"dotnet/maui","slug":"this-should-never-happen-please-file-a-bug-a29d84","errorCode":null,"errorMessage":"This should never happen, please file a bug","messagePattern":"This should never happen, please file a bug","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Compatibility/Core/src/GTK/Renderers/NavigationPageRenderer.cs","lineNumber":404,"sourceCode":"\t\t\t\t{\n\t\t\t\t\tWidget.RemoveFromContainer(target.Container);\n\t\t\t\t}\n\n\t\t\t\tFinishRemovePage(page, removeFromStack);\n\t\t\t}\n\t\t}\n\n\t\tprivate void InsertPageBefore(Page page, Page before)\n\t\t{\n\t\t\tif (before == null)\n\t\t\t\tthrow new ArgumentNullException(nameof(before));\n\t\t\tif (page == null)\n\t\t\t\tthrow new ArgumentNullException(nameof(page));\n\n\t\t\tint index = PageController.InternalChildren.IndexOf(before);\n\n\t\t\tif (index == -1)\n\t\t\t\tthrow new InvalidOperationException(\"This should never happen, please file a bug\");\n\n\t\t\tvar items = _currentStack.ToArray();\n\t\t\t_currentStack.Clear();\n\n\t\t\tint counter = 0;\n\n\t\t\tforeach (var item in items.Reverse())\n\t\t\t{\n\t\t\t\tif (counter == index)\n\t\t\t\t{\n\t\t\t\t\t_currentStack.Push(new NavigationChildPage(page));\n\n\t\t\t\t\tif (Platform.GetRenderer(page) == null)\n\t\t\t\t\t\tPlatform.SetRenderer(page, Platform.CreateRenderer(page));\n\t\t\t\t}\n\n\t\t\t\t_currentStack.Push(item);\n","sourceCodeStart":386,"sourceCodeEnd":422,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Compatibility/Core/src/GTK/Renderers/NavigationPageRenderer.cs#L386-L422","documentation":"In InsertPageBefore, the renderer looks up the 'before' page in PageController.InternalChildren (the NavigationPage's own model-level stack). If IndexOf returns -1, the page isn't in the model stack at all — an invariant violation between the model (NavigationPage.InternalChildren) and the call. The message asks for a bug report because the navigation framework should keep these in sync; a user-facing InsertPageBefore(page, nonStackPage) would surface a different error upstream.","triggerScenarios":"NavigationPage's renderer receives an InsertPageBefore request whose 'before' page is not present in the NavigationPage's InternalChildren. This usually reflects an internal desync rather than a direct API misuse, often triggered by rapid/reentrant navigation or a push/pop race on GTK.","commonSituations":"Programmatic navigation fired while a previous navigation animation/completion is still in flight. Custom navigation logic that calls InsertPageBefore with a page that was already removed. Shell or custom routers interacting with a NavigationPage on GTK.","solutions":["Serialize navigation mutations (avoid concurrent Push/Pop/InsertPageBefore on the same NavigationPage) so the model stack stays consistent.","Before calling Navigation.InsertPageBefore(page, before), verify the 'before' page is still on the stack: if (!Navigation.NavigationStack.Contains(before)) return;","If reproducible, file a bug with the navigation sequence as the message requests — this path indicates a framework invariant breach."],"exampleFix":"// before\nNavigation.InsertPageBefore(newPage, targetPage); // targetPage may already be popped\n\n// after\nif (Navigation.NavigationStack.Contains(targetPage))\n    Navigation.InsertPageBefore(newPage, targetPage);","handlingStrategy":"validation","validationCode":"// Ensure 'before' is still on the model stack before inserting.\nif (Navigation.NavigationStack.Contains(before))\n    Navigation.InsertPageBefore(page, before);","typeGuard":"static bool IsOnStack(Page p) => Navigation.NavigationStack.Contains(p);","tryCatchPattern":"try { Navigation.InsertPageBefore(page, before); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"never happen\"))\n{ /* log + skip; indicates stack desync */ }","preventionTips":["Serialize navigation mutations to avoid re-entrancy.","Verify the anchor page is still present before inserting.","Report deterministic reproductions as framework bugs."],"tags":["gtk","navigationpage","invariant","concurrency","renderer","maui"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}