Microsoft MVP성태의 닷넷 이야기
닷넷: 2274. IIS - (프로세스 종료 없는) AppDomain Recycle [링크 복사], [링크+제목 복사],
조회: 7128
글쓴 사람
정성태 (seongtaejeong at gmail.com)
홈페이지
첨부 파일
 

(시리즈 글이 7개 있습니다.)
.NET Framework: 174. 작업자 프로세스(w3wp.exe)가 재시작되는 시점을 알 수 있는 방법
; https://www.sysnet.pe.kr/2/0/841

.NET Framework: 447. w3wp.exe AppPool 재생(recycle)하는 방법 정리
; https://www.sysnet.pe.kr/2/0/1704

개발 환경 구성: 246. IIS 작업자 프로세스의 20분 자동 재생(Recycle)을 끄는 방법
; https://www.sysnet.pe.kr/2/0/1774

.NET Framework: 643. 작업자 프로세스(w3wp.exe)가 재시작되는 시점을 알 수 있는 방법 - 두 번째 이야기
; https://www.sysnet.pe.kr/2/0/11145

개발 환경 구성: 702. IIS - AppPool의 "Disable Overlapped Recycle" 옵션
; https://www.sysnet.pe.kr/2/0/13514

닷넷: 2196. IIS - AppPool의 "Disable Overlapped Recycle" 옵션의 부작용
; https://www.sysnet.pe.kr/2/0/13516

닷넷: 2274. IIS - (프로세스 종료 없는) AppDomain Recycle
; https://www.sysnet.pe.kr/2/0/13672




IIS - (프로세스 종료 없는) AppDomain Recycle

보통 IIS Recycling은 특정 조건을 만족하면 프로세스(EXE) 자체가 재실행되는 방식으로 이뤄집니다. 그런데, 상황에 따라서는 AppDomain 단위로도 수행이 되는데요,

Application Pool vs Application Domain Recycling in Asp.net?
; https://stackoverflow.com/questions/60105130/application-pool-vs-application-domain-recycling-in-asp-net

대략 다음의 조건에서 그렇다고 하니,

  1. Modification to web.config or Global.asax
  2. Change to the contents of the application's bin directory
  3. Change to the physical path of the virtual directory
  4. Deletion of the subdirectory of the application
  5. The number of re-compilations (aspx, ascx or asax) exceeds the limit specified by the setting in machine.config or web.config (default of 15)

실제로 테스트를 해볼까요? ^^

이를 위해 .NET Framework의 기본 Web Application 프로젝트를 하나 만들고 Global.asax.cs 파일에 다음과 같은 코드를 추가합니다.

using System;
using System.Diagnostics;
using System.Web;
using System.Web.Optimization;
using System.Web.Routing;

namespace WebApplication1
{
    public class Global : HttpApplication
    {
        void Application_Start(object sender, EventArgs e)
        {
            System.Diagnostics.Trace.WriteLine("===== Global.asax - Application_Start =====");
            // Code that runs on application startup
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }

        void Application_End(object sender, EventArgs e)
        {
            System.Diagnostics.Trace.WriteLine("===== Global.asax - Application_End =====");
        }
    }
}

이후, IIS 10이 설치된 Windows Server 2022에서 호스팅하면 최초 페이지 방문 시 다음과 같은 로그가 찍히는 것을 (DebugView를 이용해) 확인할 수 있습니다.

[7696] ===== Global.asax - Application_Start ===== 

그 상태에서 IIS Recycle을 하면 End 메시지가 뜨고,

[7696] ===== Global.asax - Application_End ===== 

다시 웹 페이지를 방문하면 Process ID가 바뀐 Start 로그가 나옵니다.

[7932] ===== Global.asax - Application_Start ===== 




자, 그럼 첫 번째 항목(Modification to web.config or Global.asax)을 테스트해 볼까요? ^^

우선 web.config에서 적당한 내용을 수정해 볼 텐데요, 음... Antlr3.Runtime의 oldVersion을 0.0.0.0에서 1.0.0.0으로 바꾸고 저장해 봅니다.

...[생략]...
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Antlr3.Runtime" publicKeyToken="eb42632606e9261f" />
        <bindingRedirect oldVersion="1.0.0.0-3.5.0.2" newVersion="3.5.0.2" />
      </dependentAssembly>
      ...[생략]...
    </assemblyBinding>
  </runtime>
...[생략]...

저장하는 동시에, 디버그 화면에는 End 메시지가 뜨고, 웹 페이지를 방문하면 Process ID가 바뀌지 않은 채로 Start 메시지가 찍힙니다.

[7932] ===== Global.asax - Application_End ===== 
[7932] ===== Global.asax - Application_Start ===== 

마찬가지로 Global.asax 파일 역시, 마땅히 수정할 것이 없으니 그냥 공백 문자 하나 추가한 다음 저장하면 역시나 Process ID가 바뀌지 않는 Recycle이 발생합니다.

[7932] ===== Global.asax - Application_End ===== 
[7932] ===== Global.asax - Application_Start ===== 




그다음 조건은 /bin 디렉터리의 변경입니다. 일례로 "test.txt" 파일을 만들면 그와 동시에 End가 뜨고 웹 페이지를 방문하면 Start가 나옵니다. 역시나 이번에도 Process ID는 그대로입니다.

[7932] ===== Global.asax - Application_End ===== 
[7932] ===== Global.asax - Application_Start ===== 

3번째 조건인 virtual directory의 경로를 바꾸는 것은, 당연히 recycle이 될 테니 넘어가고...

4번째 조건인 subdirectory 삭제는, 현재 Visual Studio에서 빌드한 그대로 IIS에 올렸으니, /obj/Debug 디렉터리를 삭제해 보면 될 텐데요, 음... Recycle 메시지가 안 나옵니다. /obj 디렉터리를 삭제해도 마찬가지고, 임의로 /test 디렉터를 만들고 삭제해도 아무런 변화가 없습니다.

사용하지 않는 App_Data, App_Start, Properties 디렉터리를 삭제해도 마찬가지인데, 각종 css, map 파일을 가지고 있던 "Content" 디렉터리나 "Scripts\WebForms\MSAjax" 디렉터리를 삭제했더니 Recycle이 발생했습니다. 아마도, 사용 중인 자원을 담은 디렉터리가 없어지면 Recycle이 발생하는 것으로 보입니다.

마지막 조건의 경우, numRecompilesBeforeAppRestart를 초과하는 aspx, ascx, asax 파일에 대한 수정으로 (기본 값이 15회라고 알려진) 재컴파일 횟수가 넘어가면 Recycle이 발생한다는 건데요, 테스트를 위해 "d.aspx"라는 파일을 추가한 다음, 메모장으로 열어 아무 내용이나 넣고 15번을 수정하면 Recycle이 발생합니다.

이때 중요한 것은, 내용만 바꿔서 저장하는 것뿐만 아니라, 그 바뀐 내용을 ASP.NET 엔진이 컴파일할 수 있도록 요청해야 한다는 점입니다. 그랬을 때에만 14번째 요청 후, 15번째 파일 내용을 변경해 저장하는 순간 Recycle이 발생합니다.




참고로, Application Pool에 설정할 수 있는 "Generate Recycle Event Log Entry"의 경우,

iis_apppool_recycle_event_log_1.png

AppDomain Recycle 시에는 적용되지 않습니다. 해당 항목을 보더라도,

  • Application Pool Configuration Changed (default: No) - Event is logged when the application pool recycles due to a change in its configuration
  • ISAPI Reported Unhealthy (default: No) - Event is logged because an ISAPI extension has reported itself as unhealthy
  • Manual Recycle (default: No) - Event is logged when the application pool has been manually recycled
  • Private Memory Limit Exceeded (default: Yes) - Event is logged when the application pool recycles after exceeding its private memory limit
  • Regular Time Interval (default: Yes) - Event is logged when the application pool recycles on its scheduled interval
  • Request Limit Exceeded (default: Yes) - Event is logged when the application pool recycles after exceeding its request limit
  • Specific Time (default: Yes) - Event is logged when the application pool recycles at a scheduled time
  • Virtual Memory Limit Exceeded (default: Yes) - Event is logged when the application pool recycles after exceeding its virtual memory limits

AppDomain Recycle의 조건에 해당하는 것이 없습니다. 즉, 이번 글에서 테스트한 Recycle의 경우에는 이벤트 로그에 전혀 남지 않습니다.

해당 원인을 알고 싶다면, 예전에 설명한 대로,

작업자 프로세스(w3wp.exe)가 재시작되는 시점을 알 수 있는 방법 - 두 번째 이야기
; https://www.sysnet.pe.kr/2/0/11145#code

Application_End에 HttpRuntime 인스턴스의 Reflection을 통해 _shutDownMessage를 가져오면 됩니다. 오랜만에 .NET Framework 4.8 환경에서 테스트했더니 여전히 해당 소스코드는 잘 동작합니다. ^^

약간 callstack의 깊이는 달라졌지만 web.config을 변경하는 경우 다음과 같은 메시지를 가져올 수 있고,

_shutDownMessage=CONFIG change 
HostingEnvironment initiated shutdown 
HostingEnvironment caused shutdown 
 
_shutDownStack=   at System.Environment.GetStackTrace(Exception e, Boolean needFileInfo) 
   at System.Environment.get_StackTrace() 
   at System.Web.Hosting.HostingEnvironment.InitiateShutdownInternal() 
   at System.Web.Hosting.HostingEnvironment.InitiateShutdownWithoutDemand() 
   at System.Web.HttpRuntime.ShutdownAppDomain(String stackTrace) 
   at System.Web.Configuration.HttpConfigurationSystem.OnConfigurationChanged(Object sender, InternalConfigEventArgs e) 
   at System.Configuration.Internal.InternalConfigRoot.OnConfigChanged(InternalConfigEventArgs e) 
   at System.Configuration.BaseConfigurationRecord.OnStreamChanged(String streamname) 
   at System.Web.DirectoryMonitor.FireNotifications() 
   at System.Web.Util.WorkItem.CallCallbackWithAssert(WorkItemCallback callback) 
   at System.Web.Util.WorkItem.OnQueueUserWorkItemCompletion(Object state) 
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
   at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem() 
   at System.Threading.ThreadPoolWorkQueue.Dispatch() 

또는, web.config의 numRecompilesBeforeAppRestart 옵션을 3으로 설정한 다음,

<compilation debug="true" targetFramework="4.8" numRecompilesBeforeAppRestart="3" />

aspx 파일을 3번 수정해 발생한 Recycle에서는 이런 메시지가 나옵니다.

_shutDownMessage=Recompilation limit of 3 reached 
HostingEnvironment initiated shutdown 
HostingEnvironment caused shutdown 
 
_shutDownStack=   at System.Environment.GetStackTrace(Exception e, Boolean needFileInfo) 
   at System.Environment.get_StackTrace() 
   at System.Web.Hosting.HostingEnvironment.InitiateShutdownInternal() 
   at System.Web.Hosting.HostingEnvironment.InitiateShutdownWithoutDemand() 
   at System.Web.HttpRuntime.ShutdownAppDomain(String stackTrace) 
   at System.Web.Compilation.DiskBuildResultCache.ShutdownCallBack(Object state) 
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
   at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem() 
   at System.Threading.ThreadPoolWorkQueue.Dispatch() 

물론, 코드를 좀 더 추가해 EventLog.WriteEntry를 호출한다면 얼마든지 EventLog에도 로그를 남길 수 있습니다.




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







[최초 등록일: ]
[최종 수정일: 8/6/2024]

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

비밀번호

댓글 작성자
 




[1]  2  3  4  5  6  7  8  9  10  11  12  13  14  15  ...
NoWriterDateCnt.TitleFile(s)
13917정성태4/30/202544VS.NET IDE: 199. Directory.Build.props에 정의한 속성에 대해 Condition 제약으로 값을 변경하는 방법
13916정성태4/23/2025446디버깅 기술: 221. WinDbg 분석 사례 - ASP.NET HttpCookieCollection을 다중 스레드에서 사용할 경우 무한 루프 현상 - 두 번째 이야기
13915정성태4/13/20251652닷넷: 2331. C# - 실행 시에 메서드 가로채기 (.NET 9)파일 다운로드1
13914정성태4/11/20251965디버깅 기술: 220. windbg 분석 사례 - x86 ASP.NET 웹 응용 프로그램의 CPU 100% 현상 (4)
13913정성태4/10/20251204오류 유형: 950. Process Explorer - 64비트 윈도우에서 32비트 프로세스의 덤프를 뜰 때 "Error writing dump file: Access is denied." 오류
13912정성태4/9/2025863닷넷: 2330. C# - 실행 시에 메서드 가로채기 (.NET 5 ~ .NET 8)파일 다운로드1
13911정성태4/8/20251091오류 유형: 949. WinDbg - .NET Core/5+ 응용 프로그램 디버깅 시 sos 확장을 자동으로 로드하지 못하는 문제
13910정성태4/8/20251257디버깅 기술: 219. WinDbg - 명령어 내에서 환경 변수 사용법
13909정성태4/7/20251725닷넷: 2329. C# - 실행 시에 메서드 가로채기 (.NET Framework 4.8)파일 다운로드1
13908정성태4/2/20252138닷넷: 2328. C# - MailKit: SMTP, POP3, IMAP 지원 라이브러리
13907정성태3/29/20251933VS.NET IDE: 198. (OneDrive, Dropbox 등의 공유 디렉터리에 있는) C# 프로젝트의 출력 경로 변경하기
13906정성태3/27/20252189닷넷: 2327. C# - 초기화되지 않은 메모리에 접근하는 버그?파일 다운로드1
13905정성태3/26/20252217Windows: 281. C++ - Windows / Critical Section의 안정화를 위해 도입된 "Keyed Event"파일 다운로드1
13904정성태3/25/20251901디버깅 기술: 218. Windbg로 살펴보는 Win32 Critical Section파일 다운로드1
13903정성태3/24/20251522VS.NET IDE: 197. (OneDrive, Dropbox 등의 공유 디렉터리에 있는) C++ 프로젝트의 출력 경로 변경하기
13902정성태3/24/20251737개발 환경 구성: 742. Oracle - 테스트용 hr 계정 및 데이터 생성파일 다운로드1
13901정성태3/9/20252112Windows: 280. Hyper-V의 3가지 Thread Scheduler (Classic, Core, Root)
13900정성태3/8/20252346스크립트: 72. 파이썬 - SQLAlchemy + oracledb 연동
13899정성태3/7/20251806스크립트: 71. 파이썬 - asyncio의 ContextVar 전달
13898정성태3/5/20252124오류 유형: 948. Visual Studio - Proxy Authentication Required: dotnetfeed.blob.core.windows.net
13897정성태3/5/20252367닷넷: 2326. C# - PowerShell과 연동하는 방법 (두 번째 이야기)파일 다운로드1
13896정성태3/5/20252178Windows: 279. Hyper-V Manager - VM 목록의 CPU Usage 항목이 항상 0%로 나오는 문제
13895정성태3/4/20252220Linux: 117. eBPF / bpf2go - Map에 추가된 요소의 개수를 확인하는 방법
13894정성태2/28/20252250Linux: 116. eBPF / bpf2go - BTF Style Maps 정의 구문과 데이터 정렬 문제
13893정성태2/27/20252197Linux: 115. eBPF (bpf2go) - ARRAY / HASH map 기본 사용법
[1]  2  3  4  5  6  7  8  9  10  11  12  13  14  15  ...