"Cannot redirect after HTTP headers have been sent" error while redirecting to login page


Description

Errors might occur if a logged-in user tries to perform a new request after a period of inactivity. For example, if a logged-in user has not been doing anything on the Sitecore Launchpad for some time and tries to open any application after the user session has expired.
The following MVC errors can be found in the log records:

4132 13:08:18 INFO  AUDIT (sitecore\**): Logout
4132 13:08:18 WARN  Protected page accessed with no current user
4132 13:08:18 ERROR Failed to render rendering
Exception: System.InvalidOperationException
Message: Error while rendering view: '/sitecore/shell/client/Speak/Layouts/Renderings/Common/PageCodes/PageCode.cshtml' (model: 'Sitecore.Mvc.Presentation.RenderingModel, Sitecore.Mvc').
Source: Sitecore.Mvc
   at Sitecore.Mvc.Presentation.ViewRenderer.Render(TextWriter writer)
   at Sitecore.Mvc.Pipelines.Response.RenderRendering.ExecuteRenderer.Render(Renderer renderer, TextWriter writer, RenderRenderingArgs args)
Nested Exception
Exception: System.Web.HttpException
Message: Cannot redirect after HTTP headers have been sent.
Source: System.Web
   at System.Web.HttpResponse.Redirect(String url, Boolean endResponse, Boolean permanent)
   at Sitecore.Shell.Web.ShellPage.GotoLoginPage(HttpContext httpContext, Boolean returnAfterLogin)
  ... 4132 13:08:18 ERROR Failed to render rendering
Exception: System.InvalidOperationException
Message: Error while rendering view: '/sitecore/shell/client/Speak/Layouts/Layouts/Speak-Layout.cshtml' (model: 'Sitecore.Mvc.Presentation.RenderingModel, Sitecore.Mvc').
Source: Sitecore.Mvc
  ...

Solution

To resolve the issue, download and install the patch compatible with the affected product version found on this page:
https://github.com/SitecoreSupport/Sitecore.Support.101417/releases.
To get a solution for other releases, contact Sitecore Support.