Random page rendering error: A stray renderRoot() invocation is causing bubbling of attached assets to break I have a D8 site that's in development and I am getting this error thrown at random on pages which most of the time render quite happily. There doesn't seem to be any real pattern in it.

I can see that it looks like it's designed to prevent the renderer creating an infinite loop (the code is on line 133 of core/lib/Drupal/Core/Render/Renderer.php), but it isn't really clear what's actually causing it. The log error is pretty useless really.

Anyone seen this and found out the cause? Thanks
The error is caused by Renderer::renderRoot() in a wrong place, see the docs:


  Renders final HTML given a structured array tree.
  
  Calls ::render() in such a way that placeholders are replaced.
  
  Should therefore only be used in occasions where the final rendering
  is happening, just before sending a Response:
  
  
  system internals that are responsible for rendering the final HTML
  render arrays for non-HTML responses, such as feeds
  
  
  (Cannot be executed within another render context.)


Look for renderRoot() in custom code. Check if you need to render at all (most times you need not) and don't render too early, only in theming when there is a render context collecting the bubbled up metadata. Then replace it with Renderer::render(). 

The random behavior is probably because of caching.