{"record":{"id":"5fbd71a44d52c8a0","repo":"tui-cs/Terminal.Gui","slug":"attempt-to-run-the-runnable-that-s-already-the-top","errorCode":null,"errorMessage":"Attempt to Run the runnable that's already the top runnable.","messagePattern":"Attempt to Run the runnable that's already the top runnable\\.","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"Terminal.Gui/App/ApplicationImpl.Run.cs","lineNumber":160,"sourceCode":"        // Ensure the mouse is ungrabbed\n        Mouse.UngrabMouse ();\n\n        Navigation?.SetFocused (null);\n\n        IRunnable? previousTop = null;\n\n        // CRITICAL SECTION - Atomic stack + cached state update\n        lock (_sessionStackLock)\n        {\n            // Get the previous top BEFORE pushing new token\n            if (SessionStack?.TryPeek (out SessionToken? previousToken) == true && previousToken.Runnable is { })\n            {\n                previousTop = previousToken.Runnable;\n            }\n\n            if (previousTop == runnable)\n            {\n                throw new ArgumentOutOfRangeException (nameof (runnable), runnable, @\"Attempt to Run the runnable that's already the top runnable.\");\n            }\n\n            // Push token onto SessionStack\n            SessionStack?.Push (token);\n\n            TopRunnable = runnable;\n\n            // Update cached state atomically - IsRunning and IsModal are now consistent\n            SessionBegun?.Invoke (this, new SessionTokenEventArgs (token));\n            runnable.SetIsRunning (true);\n            runnable.SetIsModal (true);\n\n            // Previous top is no longer modal\n            previousTop?.SetIsModal (false);\n        }\n\n        // END CRITICAL SECTION - IsRunning/IsModal now thread-safe\n","sourceCodeStart":142,"sourceCodeEnd":178,"githubUrl":"https://github.com/tui-cs/Terminal.Gui/blob/2e47b11478db083499917f2ad27c34d30efb0df1/Terminal.Gui/App/ApplicationImpl.Run.cs#L142-L178","documentation":"Thrown by ApplicationImpl.Begin when the runnable being started is identical (reference-equal) to the runnable currently on top of the session stack (TopRunnable). Pushing the same runnable again would corrupt the stack and produce duplicate IsRunning transitions. This is an ArgumentOutOfRangeException on the runnable parameter.","triggerScenarios":"Calling Run (or Begin) on the same runnable that is already the TopRunnable, e.g. re-running the top Window without first ending its session; recursive dialog logic that re-launches the active top-level.","commonSituations":"Menu/shortcut handlers that re-invoke Run on the currently active top-level; event-driven code that re-enters a runnable; incorrect session management when chaining modal flows.","solutions":["Before starting a runnable, verify it is not the current TopRunnable: check app.TopRunnable != runnable.","End or RequestStop the current top session before re-running the same runnable.","Use distinct runnable instances for nested modal flows rather than reusing the same object."],"exampleFix":"// before\napp.Run (sameRunnable); // already the top runnable\n\n// after\nif (app.TopRunnable != sameRunnable)\n{\n    app.Run (sameRunnable);\n}","handlingStrategy":"validation","validationCode":"if (app.TopRunnable != runnable)\n{\n    app.Run (runnable);\n}","typeGuard":"static bool IsNotTopRunnable (IApplication app, IRunnable r) => app.TopRunnable != r;","tryCatchPattern":null,"preventionTips":["Verify the runnable is not already the TopRunnable before Run/Begin.","End the current top session before re-running the same runnable.","Use distinct instances for nested modal flows."],"tags":["session","runnable","lifecycle","stack"],"backgroundTag":null,"analyzedSha":"2e47b11478db083499917f2ad27c34d30efb0df1","analyzedAt":"2026-08-13T19:20:08.826Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}