Microsoft MVP성태의 닷넷 이야기
.NET Framework: 815. CER(Constrained Execution Region)이란? [링크 복사], [링크+제목 복사]
조회: 12590
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
(연관된 글이 2개 있습니다.)

CER(Constrained Execution Region)이란?

현재 CER에 대해 가장 잘 설명하고 있는 문서는 무려 2005년에 나온 다음의 글입니다.

2005년 10월 MSDN Magazine
Keep Your Code Running with the Reliability Features of the .NET Framework
; https://learn.microsoft.com/en-us/archive/msdn-magazine/2005/october/using-the-reliability-features-of-the-net-framework

CHM
; http://download.microsoft.com/download/3/a/7/3a7fa450-1f33-41f7-9e6d-3aa95b5a6aea/MSDNMagazineOctober2005en-us.chm

이참에 정리를 한번 해볼까요? ^^

우선 이 기능이 도입된 배경 설명이 나오는데요, CER이 .NET 2.0에 도입될 당시 SQL 서버에 닷넷 CLR을 호스팅하면서 필요해졌다고 합니다. SQL 서버의 특성상 장애를 최소화해야 하고 이를 위해 이전에 회피 불가능한 3가지 예외(OutOfMemoryException, StackOverflowException, ThreadAbortException)에 대한 처리 능력이 필요하게 된 것입니다. (문서에서는 해당 예외들을 Asynchronous exceptions로 지칭합니다.) 가령 StackOverflowException이 .NET 코드에서 발생했다고 해서 SQL 서버 프로세스가 종료되어서는 안 된다는 것입니다.

참고로, 일반적인 .NET Framework 응용 프로그램에서는 저런 처리 능력이 100% 갖춰져 있지는 않습니다. 왜냐하면 CLR 호스팅 시 정책적으로 관련 옵션들을 설정해야 하는데 SQL 서버의 호스팅 환경에서만 저 정책들이 켜져 있기 때문입니다.

일례로, 일반적인 .NET Framework 응용 프로그램에서는 ThreadAbortException이 finally 절을 처리하는 도중에는 발생하지 못합니다. 왜냐하면 thread abort 작업이 gracful한 방식으로 종료하기 때문입니다. 실제로 CER을 사용하지 않고도 다음의 코드는 finally 절이라는 이유만으로 절대 Abort가 발생하지 않으므로 무한 대기 상태에 빠지게 됩니다.

using System;
using System.Threading;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Thread t = new Thread(threadFunc);
            t.Start();
            Thread.Sleep(1000);

            {
                Console.WriteLine("Aborting...");
                t.Abort();
                Console.WriteLine("Aborted.");
            }

            Console.WriteLine("Press any key to exit...");
            Console.ReadLine();
        }

        private static void threadFunc()
        {
            try { }
            finally
            {
                Thread.Sleep(-1);
            }
        }
    }
}

반면, 위와 같은 상황이 SQL 서버에서 호스팅되면 finally 절임에도 thread abort가 발생할 수 있습니다. 만약 그런 환경에서도 finally 절의 코드가 실행되는 것을 보장받고 싶다면 CER이 되도록 RuntimeHelpers.PrepareConstrainedRegions(); 코드를 사용해야 합니다.




다음으로 문제가 되는 2가지 예외(OutOfMemoryException, StackOverflowException)에 CER의 재미있는 사례를 다음의 코드에서 볼 수 있습니다.

.NET Internals Cookbook Part 2 - GC-related things
; https://blog.adamfurmanek.pl/2019/02/23/net-internals-cookbook-part-2/

예제 코드를,


using System;
using System.Threading;

class Program
{
    static void Main(string[] args)
    {
        var cerThread = new Thread(() =>
        {
            try
            {
                Do();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        });

        cerThread.Start();
        cerThread.Join();
    }

    private static void Do()
    {
        Console.WriteLine("Can you see this message?");
        try
        {
            OutOfMemory();
            Console.WriteLine("Do Something");
        }
        finally
        {
            Console.WriteLine("In finally: " + DateTime.Now);
        }
    }

    static void OutOfMemory()
    {
        Big big;
    }
}

unsafe struct Big
{
    public fixed byte Bytes[int.MaxValue]; // fixed를 썼지만, CER 내에서 계산할 때는 GC Heap을 대상으로 판단
}

실행하면 다음과 같은 결과가 나옵니다.

Can you see this message?
In finally: 2019-03-19 오전 10:45:56
System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
   at Program.OutOfMemory()
   at Program.Do() in c:\cer_jitted\ConsoleApp1\ConsoleApp2\Program.cs:line 30
   at Program.<>c.<Main>b__0_0() in c:\cer_jitted\ConsoleApp1\ConsoleApp2\Program.cs:line 12

일반적으로 우리가 예상할 수 있는 결과입니다. 하지만 이것을 CER 영역에서 실행하면,

private static void DoCER()
{
    Console.WriteLine("Can you see this message?");
    RuntimeHelpers.PrepareConstrainedRegions();
    try
    {
    }
    finally // RuntimeHelpers.PrepareConstrainedRegions + finally는 CER로 처리
    {
        Console.WriteLine("In finally: " + DateTime.Now);
        OutOfMemory();
        Console.WriteLine("Do Something");
    }
}

[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
static void OutOfMemory()
{
    Big big;
}

다음과 같은 출력 결과를 보게 됩니다.

System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
   at Program.DoInCER()
   at Program.<>c.<Main>b__0_0() in c:\cer_jitted\ConsoleApp1\ConsoleApp3\Program.cs:line 15

보다시피 finally 내의 Console.WriteLine 코드가 전혀 실행되지 않았습니다. 즉, CLR은 CER 내의 코드를 미리 JIT 컴파일하게 되고 그 와중에 OutOfMemoryException 예외가 미리 발생해 CER 내의 코드가 부분적으로 실행하다 마는 문제를 최소화하는 것입니다.

물론, 이러한 처리가 CLR의 Jitter에 의해 이뤄지는 것이므로 런타임 시에 발생하는 메모리 할당까지 알 수는 없습니다. 가령 코드 내에서 다음과 같은 상황으로 발생하는 OutOfMemoryException은 어쩔 수 없이 코드 실행 중에 발생하게 됩니다.

  • explicit allocations
  • boxing
  • virtual method calls (unless the target of the virtual method call has already been prepared)
  • method calls through reflection
  • use of Monitor.Enter (or the lock keyword in C# and SyncLock in Visual Basic)
  • isinst and castclass instructions on COM objects
  • field access through transparent proxies, serialization, and multidimensional array accesses.

결국 이런 수준의 제약을 만족하는 코드는 복잡할 수 없으므로, 상대적으로 적은 코드의 영역을 대상으로 CER 제약을 가하는 것이 권장됩니다.




코드가 CER 내에서 실행되는 조건은, 다음과 같이 try 바로 직전에 PrepareConstrainedRegions 메서드 호출 코드를 넣은 경우 catch와 finally 절로 제한됩니다.

... // will not be prepared
RuntimeHelpers.PrepareConstrainedRegions();
try 
{
    ... // will not be prepared
} 
/*
catch, fault, and filter blocks will be prepared.
*/
finally
{
    ... // will be prepared. your noninterruptible code here
}
... // will not be prepared

또한 CER 내에서의 중첩 함수 호출은 ReliabilityContract가,

ReliabilityContractAttribute(Consistency, Cer)
; https://learn.microsoft.com/en-us/dotnet/api/system.runtime.constrainedexecution.reliabilitycontractattribute.-ctor?view=netframework-4.7.2

다음의 3가지로 설정되어 있는 경우에만 Jitter의 CER 처리를 받게 됩니다.

[ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]

* 지정되지 않은 경우의 기본값이, "Consistency.MayCorruptProcess, Cer.None"이므로 CER 처리 대상에서 제외됨

// Consistency: what kind of state corruption could result from asynchronous exceptions being thrown during the method's execution, 
public enum Consistency
{
    MayCorruptProcess,
    MayCorruptAppDomain,
    MayCorruptInstance,
    WillNotCorruptState
}

// Cer: what kind of completion guarantees the method can make if it were to run in a CER.
[Serializable]
public enum Cer
{
    None,
    MayFail,
    Success
}

/*
A Cer value of MayFail is used to signal that when faced with asynchronous exceptions, 
the code may not complete in an expected fashion. 
Since thread aborts are being delayed over constrained execution regions, 
this really means that your code is doing something that may cause memory to be allocated or that might result in a stack overflow.
*/

따라서 위의 예제 코드에서 OutOfMemory 메서드의 ReliabilityContract를 다른 값으로 바꿔보면 finally 절의 Console.WriteLine이 실행되는 것을 확인할 수 있습니다.




하지만 CER로 지정되었다고 해서 무조건 안정성을 보장받는다고 할 수는 없습니다. 일례로, .NET Framework 응용 프로그램에서는 CER 내에서 StackOverflowException이 발생하면 응용 프로그램이 비정상 종료하게 됩니다. 예를 들어, 위에서 든 예제 코드 중 Big 타입의 Bytes 필드를 다음과 같이 바꾸면,

unsafe struct Big
{
    public fixed byte Bytes[1_048_576];  // fixed를 썼지만, CER 내에서 계산할 때는 GC Heap을 대상으로 판단
                                         // 따라서 CER에 걸리지 않고 실행되므로 (기본값인 경우 스택 메모리의 크기가 1MB라서) StackOverflowException 발생
}

CER을 명시했음에도 일반적인 .NET 응용 프로그램에서는 비정상 종료하게 됩니다. 반면 CLR 호스팅 정책을 바꾼 SQL 서버에서는 비정상 종료를 하지 않는다고 합니다.




(첨부 파일은 이 글의 예제 코드를 포함합니다.)

정리해 보면, .NET Framework 환경에서는 CER을 사용해도 중간 정도의 애매한 안정성을 확보하게 됩니다. 만약 여러분들이 만드는 응용 프로그램이 SQL 서버와 같은 고-안정성을 필요로 한다면 직접 호스팅 코드를 만들어 Asynchronous exceptions 관련 정책들을 설정해야 합니다.




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]

[연관 글]






[최초 등록일: ]
[최종 수정일: 12/1/2022]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




... 16  17  18  19  20  [21]  22  23  24  25  26  27  28  29  30  ...
NoWriterDateCnt.TitleFile(s)
13094정성태7/6/20225981오류 유형: 816. Golang - "short write" 오류 원인
13093정성태7/5/20226908.NET Framework: 2029. C# - HttpWebRequest로 localhost 접속 시 2초 이상 지연
13092정성태7/3/20227835.NET Framework: 2028. C# - HttpWebRequest의 POST 동작 방식파일 다운로드1
13091정성태7/3/20226675.NET Framework: 2027. C# - IPv4, IPv6를 모두 지원하는 서버 소켓 생성 방법
13090정성태6/29/20225800오류 유형: 815. PyPI에 업로드한 패키지가 반영이 안 되는 경우
13089정성태6/28/20226291개발 환경 구성: 646. HOSTS 파일 변경 시 Edge 브라우저에 반영하는 방법
13088정성태6/27/20225418개발 환경 구성: 645. "Developer Command Prompt for VS 2022" 명령행 환경의 폰트를 바꾸는 방법
13087정성태6/23/20228349스크립트: 41. 파이썬 - FastAPI / uvicorn 호스팅 환경에서 asyncio 사용하는 방법 [1]
13086정성태6/22/20227785.NET Framework: 2026. C# 11 - 문자열 보간 개선 2가지파일 다운로드1
13085정성태6/22/20227851.NET Framework: 2025. C# 11 - 원시 문자열 리터럴(raw string literals)파일 다운로드1
13084정성태6/21/20226463개발 환경 구성: 644. Windows - 파이썬 2.7을 msi 설치 없이 구성하는 방법
13083정성태6/20/20227027.NET Framework: 2024. .NET 7에 도입된 GC의 메모리 해제에 대한 segment와 region의 차이점 [2]
13082정성태6/19/20226059.NET Framework: 2023. C# - Process의 I/O 사용량을 보여주는 GetProcessIoCounters Win32 API파일 다운로드1
13081정성태6/17/20226128.NET Framework: 2022. C# - .NET 7 Preview 5 신규 기능 - System.IO.Stream ReadExactly / ReadAtLeast파일 다운로드1
13080정성태6/17/20226757개발 환경 구성: 643. Visual Studio 2022 17.2 버전에서 C# 11 또는 .NET 7.0 preview 적용
13079정성태6/17/20224544오류 유형: 814. 파이썬 - Error: The file/path provided (...) does not appear to exist
13078정성태6/16/20226552.NET Framework: 2021. WPF - UI Thread와 Render Thread파일 다운로드1
13077정성태6/15/20226888스크립트: 40. 파이썬 - PostgreSQL 환경 구성
13075정성태6/15/20225842Linux: 50. Linux - apt와 apt-get의 차이 [2]
13074정성태6/13/20226142.NET Framework: 2020. C# - NTFS 파일에 사용자 정의 속성값 추가하는 방법파일 다운로드1
13073정성태6/12/20226345Windows: 207. Windows Server 2022에 도입된 WSL 2
13072정성태6/10/20226629Linux: 49. Linux - ls 명령어로 출력되는 디렉터리 색상 변경 방법
13071정성태6/9/20227215스크립트: 39. Python에서 cx_Oracle 환경 구성
13070정성태6/8/20227035오류 유형: 813. Windows 11에서 입력 포커스가 바뀌는 문제 [1]
13069정성태5/26/20229257.NET Framework: 2019. C# - .NET에서 제공하는 3가지 Timer 비교 [2]
13068정성태5/24/20227717.NET Framework: 2018. C# - 일정 크기를 할당하는 동안 GC를 (가능한) 멈추는 방법 [1]파일 다운로드1
... 16  17  18  19  20  [21]  22  23  24  25  26  27  28  29  30  ...