{"record":{"id":"9971ed08a91adc6a","repo":"dotnet/wpf","slug":"sr-invalidoperation-cannotclearfwdstack","errorCode":null,"errorMessage":"SR.InvalidOperation_CannotClearFwdStack","messagePattern":"SR\\.InvalidOperation_CannotClearFwdStack","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Navigation/Journal.cs","lineNumber":327,"sourceCode":"            _uncommittedCurrentIndex = _currentEntryIndex;\n\n            if (!ClearForwardStack())\n            {\n                // If ClearForwardStack() didn't change the journal, UpdateView() needs to be\n                // called here to enable the Back button.\n                UpdateView();\n            }\n        }\n\n        internal bool ClearForwardStack()\n        {\n            Debug.Assert(ValidateIndexes());\n\n            if (_currentEntryIndex >= TotalCount)\n                return false; // nothing to do\n\n            if(_uncommittedCurrentIndex > _currentEntryIndex)\n                throw new InvalidOperationException(SR.InvalidOperation_CannotClearFwdStack);\n\n            _journalEntryList.RemoveRange(_currentEntryIndex, _journalEntryList.Count - _currentEntryIndex);\n            UpdateView();\n            return true;\n        }\n\n        internal void CommitJournalNavigation(JournalEntry navigated)\n        {\n            NavigateTo(navigated);\n        }\n\n        internal void AbortJournalNavigation()\n        {\n            _uncommittedCurrentIndex = _currentEntryIndex;\n            UpdateView();\n        }\n\n        /// <summary>","sourceCodeStart":309,"sourceCodeEnd":345,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Navigation/Journal.cs#L309-L345","documentation":"Journal.ClearForwardStack() removes all entries after the current position, but refuses to do so while there is an uncommitted forward navigation in progress (_uncommittedCurrentIndex > _currentEntryIndex). WPF throws InvalidOperationException because clearing the forward stack during an uncommitted navigation would corrupt journal indexes.","triggerScenarios":"RecordNewNavigation processing a navigation while an earlier uncommitted navigation left _uncommittedCurrentIndex ahead of _currentEntryIndex; interleaving AddBackEntry/navigation calls such that a new navigation is recorded before the previous one committed.","commonSituations":"Calling AddBackEntry or manipulating navigation during an in-flight Navigate; hosting frames with rapid programmatic navigation; navigation reentrancy from Navigating event handlers.","solutions":["Avoid calling navigation/journal APIs while a navigation is in flight; wait for NavigationService navigation to complete (LoadCompleted/NavigationFailed)","Serialize navigations; do not start a new Navigate before the previous one finishes","Remove explicit AddBackEntry calls that race with normal navigation recording"],"exampleFix":"// before\nframe.NavigationService.AddBackEntry(myState);\nframe.NavigationService.Navigate(page); // races with uncommitted entry\n// after\nframe.NavigationService.Navigate(page);\nframe.NavigationService.LoadCompleted += (s, e) => { /* safe to inspect journal */ };","handlingStrategy":"validation","validationCode":"// allow navigation only when idle\nbool canNavigate = navService.Content == null || lastNavCompleted;\nif (!canNavigate) return; // defer or queue the navigation","typeGuard":null,"tryCatchPattern":"try { frame.NavigationService.Navigate(uri); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"forward stack\")) {\n    // queue navigation until current one completes\n}","preventionTips":["Serialize navigations; wait for LoadCompleted before starting another","Avoid AddBackEntry during in-flight navigations","Do not trigger navigation recursively from Navigating handlers"],"tags":["wpf","navigation","journal","state-machine"],"backgroundTag":"invalid-state-transition","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}