Search code examples
c#.netvb.netreliabilitycer

Are CER needed to merely protect shared managed states within an AppDomain?


I do have an operation that must be reliably performed as a whole or not be performed at all.

  1. The goal is only to preserve the consistency of some in-memory managed shared states.
  2. Those states are contained within an application domain. They are not visible outside of this domain.
  3. I therefore do not have to react when the domain or the process are teared down.
  4. I am writing a class library and the user may call my code from anywhere. However my code does not call any user code, not even virtual methods.
  5. The CLR may be hosted.


To my understanding I do not need constrained execution regions (CER) since:

  1. CER are only needed against the infamous OutOfMemoryException, ThreadAbortException and StackOverflowException.
  2. My code does not make any allocation, so I do not care about OutOfMemory (anyway allocations must not be done within a CER).
  3. If a stack overflow occurs the process will be teared down anyway (or the domain in some hosted scenarios).
  4. Thread aborts are already delayed until the end of a finally block and my code is already within one.


Am I correct on those points? Do you see other reasons why I should need CER?


Solution

  • I finally found at least one reason why a CER is still needed: even if my code does not do any allocation, the JIT compiler may have to allocate memory on the first execution.

    Therefore putting a CER is required to force the runtime to JIT everything beforehand and prevent a possible OOM.