The reason this sometimes fails is because LazyThreadSafetyMode.None
doesn't give any guarantees about correctness when accessed over multiple threads. The documentation for LazyThreadSafetyMode.None
states:
The Lazy instance is not thread safe; if the instance is accessed from multiple threads, its behavior is undefined. Use this mode only when high performance is crucial and the Lazy instance is guaranteed never to be initialized from more than one thread.
You incorrectly assumed that since your delegate always returned the same value, no thread-safety was needed, but this isn't the case.
You should initialize the Lazy<T>
with either PublicationOnly
or ExecutionAndPublication
.