Microsoft MVP성태의 닷넷 이야기
닷넷: 2274. IIS - (프로세스 종료 없는) AppDomain Recycle [링크 복사], [링크+제목 복사],
조회: 7141
글쓴 사람
정성태 (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

비밀번호

댓글 작성자
 




... [16]  17  18  19  20  21  22  23  24  25  26  27  28  29  30  ...
NoWriterDateCnt.TitleFile(s)
13541정성태1/29/20249610VS.NET IDE: 188. launchSettings.json의 useSSL 옵션
13540정성태1/29/20249499Linux: 69. 리눅스 - "Docker Desktop for Windows" Container 환경에서 IPv6 Loopback Address 바인딩 오류
13539정성태1/26/20249300개발 환경 구성: 703. Visual Studio - launchSettings.json을 이용한 HTTP/HTTPS 포트 바인딩
13538정성태1/25/20249901닷넷: 2211. C# - NonGC(FOH) 영역에 .NET 개체를 생성파일 다운로드1
13537정성태1/24/202410685닷넷: 2210. C# - Native 메모리에 .NET 개체를 생성파일 다운로드1
13536정성태1/23/202410300닷넷: 2209. .NET 8 - NonGC Heap / FOH (Frozen Object Heap) [1]
13535정성태1/22/202410726닷넷: 2208. C# - GCHandle 구조체의 메모리 분석
13534정성태1/21/202410131닷넷: 2207. C# - SQL Server DB를 bacpac으로 Export/Import파일 다운로드1
13533정성태1/18/202410122닷넷: 2206. C# - TCP KeepAlive의 서버 측 구현파일 다운로드1
13532정성태1/17/202410174닷넷: 2205. C# - SuperSimpleTcp 사용 시 주의할 점파일 다운로드1
13531정성태1/16/202410556닷넷: 2204. C# - TCP KeepAlive에 새로 추가된 Retry 옵션파일 다운로드1
13530정성태1/15/20249832닷넷: 2203. C# - Python과의 AES 암호화 연동파일 다운로드1
13529정성태1/15/202410035닷넷: 2202. C# - PublishAot의 glibc에 대한 정적 링킹하는 방법
13528정성태1/14/202410192Linux: 68. busybox 컨테이너에서 실행 가능한 C++, Go 프로그램 빌드
13527정성태1/14/202410278오류 유형: 892. Visual Studio - Failed to launch debug adapter. Additional information may be available in the output window.
13526정성태1/14/202410612닷넷: 2201. C# - Facebook 연동 / 사용자 탈퇴 처리 방법
13525정성태1/13/20249751오류 유형: 891. Visual Studio - Web Application을 실행하지 못하는 IISExpress
13524정성태1/12/20249780오류 유형: 890. 한국투자증권 KIS Developers OpenAPI - GW라우팅 중 오류가 발생했습니다.
13523정성태1/12/20249767오류 유형: 889. Visual Studio - error : A project with that name is already opened in the solution.
13522정성태1/11/202410596닷넷: 2200. C# - HttpClient.PostAsJsonAsync 호출 시 "Transfer-Encoding: chunked" 대신 "Content-Length" 헤더 처리
13521정성태1/11/202410255닷넷: 2199. C# - 한국투자증권 KIS Developers OpenAPI의 WebSocket Ping, Pong 처리
13520정성태1/10/20249930오류 유형: 888. C# - Unable to resolve service for type 'Microsoft.Extensions.ObjectPool.ObjectPool`....' [1]
13519정성태1/10/20249623닷넷: 2198. C# - Reflection을 이용한 ClientWebSocket의 Ping 호출파일 다운로드1
13518정성태1/9/202410357닷넷: 2197. C# - ClientWebSocket의 Ping, Pong 처리
13517정성태1/8/20249502스크립트: 63. Python - 공개 패키지를 이용한 위성 이미지 생성 (pystac_client, odc.stac)
13516정성태1/7/20249649닷넷: 2196. IIS - AppPool의 "Disable Overlapped Recycle" 옵션의 부작용
... [16]  17  18  19  20  21  22  23  24  25  26  27  28  29  30  ...