I do have an operation that must be reliably performed as a whole or not be performed at all.
- The goal is only to preserve the consistency of some in-memory managed shared states.
- Those states are contained within an application domain. They are not visible outside of this domain.
- I therefore do not have to react when the domain or the process are teared down.
- 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.
- The CLR may be hosted.
To my understanding I do not need constrained execution regions (CER) since:
- CER are only needed against the infamous
OutOfMemoryException
, ThreadAbortException
and StackOverflowException
.
- My code does not make any allocation, so I do not care about OutOfMemory (anyway allocations must not be done within a CER).
- If a stack overflow occurs the process will be teared down anyway (or the domain in some hosted scenarios).
- 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?
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.