"Exception in alarm clock event subscriber" error appears in Sitecore logs


Description

The following error might appear randomly in the Sitecore logs:

ERROR Exception in alarm clock event subscriber. 
Exception: System.InvalidOperationException 
Message: Collection was modified; enumeration operation may not execute. 
Source: mscorlib 
   at System.Collections.Hashtable.HashtableEnumerator.MoveNext() 
   at Sitecore.Collections.SafeDictionary`2.d__7.MoveNext() 
   at Sitecore.Pipelines.HealthMonitor.HealthMonitor.DumpRenderingsStatistics(PipelineArgs args) 
   at (Object , Object[] ) 
   at Sitecore.Pipelines.CorePipeline.Run(PipelineArgs args) 
   at Sitecore.Services.AlarmClock.Heartbeat_Beat(Object sender, EventArgs e)

The Sitecore HealthMonitor periodically enumerates the page rendering statistics collection to save the information into a file. The issue might occur if another thread tries to modify the collection concurrently, meaning, if a page is being rendered while the statistics are being saved.

The issue affects Sitecore XP 8.0.3 - 8.2.2, Sitecore XP 9.0.1, and later versions.

Solution

To resolve the issue, consider one of the following options:

Alternatively, to mitigate the issue, proceed with the following options: