Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 

(시리즈 글이 5개 있습니다.)
.NET Framework: 2064. C# - Mutex와 Semaphore/SemaphoreSlim 차이점
; https://www.sysnet.pe.kr/2/0/13156

.NET Framework: 2065. C# - Mutex의 비동기 버전
; https://www.sysnet.pe.kr/2/0/13157

닷넷: 2216. C# - SemaphoreSlim 사용 시 주의점
; https://www.sysnet.pe.kr/2/0/13555

닷넷: 2217. C# - 최댓값이 1인 SemaphoreSlim 보다 Mutex 또는 lock(obj)를 선택하는 것이 나은 이유
; https://www.sysnet.pe.kr/2/0/13558

디버깅 기술: 195. windbg 분석 사례 - Semaphore 잠금으로 인한 Hang 현상 (닷넷)
; https://www.sysnet.pe.kr/2/0/13560




windbg 분석 사례 - Semaphore 잠금으로 인한 Hang 현상 (닷넷)

지난 글에서,

C# - 최댓값이 1인 SemaphoreSlim 보다 Mutex 또는 lock(obj)를 선택하는 것이 나은 이유
; https://www.sysnet.pe.kr/2/0/13558

Mutex를 사용해 hang 현상이 발생한 경우 어떻게 잠금을 추적할 수 있는지 설명했는데요, 동일한 상황에서 (최댓값이 1인) SemaphoreSlim을 사용했을 때 디버깅이 어떻게 바뀌는지 살펴보겠습니다.




문제의 상황에서 덤프 파일을 뜨면, 가장 먼저 해야 할 것이 바로 "Debug Diagnostics"로 대강의 상황을 파악하는 것입니다.

실제 사례를 통해 Debug Diagnostics 도구가 생성한 닷넷 웹 응용 프로그램의 성능 장애 보고서 설명
; https://www.sysnet.pe.kr/2/0/12067

위의 방법에 따라 닷넷 프로세스를 분석했더니 6개의 스레드가 다음과 같은 호출 스택으로 나왔습니다.

[[GCFrame]]
[[HelperMethodFrame_1OBJ] (System.Threading.Monitor.ObjWait)] System.Threading.Monitor.ObjWait(Boolean, Int32, System.Object)
mscorlib_ni!System.Threading.SemaphoreSlim.WaitUntilCountOrTimeout(Int32, UInt32, System.Threading.CancellationToken)+8b
mscorlib_ni!System.Threading.SemaphoreSlim.Wait(Int32, System.Threading.CancellationToken)+151
SuperSimpleTcp.SimpleTcpServer.SendInternal(System.String, Int64, System.IO.Stream)+9e
SuperSimpleTcp.SimpleTcpServer.Send(System.String, Byte[])+a2
...[생략]...

문제는, ^^; 저 hang 상태를 유발한 Semaphore.Wait을 다른 스레드에서 찾을 수 없었다는 점입니다. 이후의 분석은 덤프 파일을 windbg로 열어 추적을 해야 하는데요, 위의 hang 상태에 빠진 스레드 6개 중 1개의 스레드로 문맥을 변경한 다음 "clrstack -a"로 SemaphoreSlim 인스턴스를 얻는 것으로 시작할 수 있습니다.

// DebugDiag로 분석한 스레드 중 1개를 선택, 이 글에서는 "59번 스레드"

0:000> ~59s
ntdll!NtWaitForMultipleObjects+0x14:
00007ffc`f53b59a4 c3              ret


// call stack과 그 스택으로부터 얻을 수 있는 SemaphoreSlim 인스턴스를 알아냄

0:059> !clrstack -a
OS Thread Id: 0x320c (59)
        Child SP               IP Call Site
000000d03d13e530 00007ffcf53b59a4 [GCFrame: 000000d03d13e530] 
000000d03d13e658 00007ffcf53b59a4 [HelperMethodFrame_1OBJ: 000000d03d13e658] System.Threading.Monitor.ObjWait(Boolean, Int32, System.Object)
000000d03d13e770 00007ffce43dac4b System.Threading.SemaphoreSlim.WaitUntilCountOrTimeout(Int32, UInt32, System.Threading.CancellationToken) [f:\dd\ndp\clr\src\BCL\system\threading\SemaphoreSlim.cs @ 469]
    PARAMETERS:
        this (<CLR reg>) = 0x0000021f6acc0ff8
        millisecondsTimeout (<CLR reg>) = 0x00000000ffffffff
        startTime (<CLR reg>) = 0x0000000000000000
        cancellationToken = <no data>

...[생략]...

그럼 해당 세마포어 인스턴스의 상태를 볼까요? ^^

0:059> !DumpObj /d 0000021f6acc0ff8
Name:        System.Threading.SemaphoreSlim
MethodTable: 00007ffce3506740
EEClass:     00007ffce374bc90
Size:        64(0x40) bytes
File:        C:\Windows\Microsoft.Net\assembly\GAC_64\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll
Fields:
              MT    Field   Offset                 Type VT     Attr            Value Name
00007ffce34885a0  4001a88       28         System.Int32  1 instance                0 m_currentCount
00007ffce34885a0  4001a89       2c         System.Int32  1 instance                1 m_maxCount
00007ffce34885a0  4001a8a       30         System.Int32  1 instance                6 m_waitCount
00007ffce3485dd8  4001a8b        8        System.Object  0 instance 0000000000000000 m_lockObj
00007ffce3504398  4001a8c       10 ....ManualResetEvent  0 instance 0000000000000000 m_waitHandle
00007ffce34d4f38  4001a8d       18 ...horeSlim+TaskNode  0 instance 0000000000000000 m_asyncHead
00007ffce34d4f38  4001a8e       20 ...horeSlim+TaskNode  0 instance 0000000000000000 m_asyncTail
00007ffce34a4ac0  4001a8f     1008 ...olean, mscorlib]]  0   shared           static s_trueTask
                                 >> Domain:Value  0000021d6a3b80e0:NotInit  0000021d6a4d3350:0000021d6a9a0058 <<
00007ffce34fa680  4001a91     1010 ...bject, mscorlib]]  0   shared           static s_cancellationTokenCanceledEventHandler
                                 >> Domain:Value  0000021d6a3b80e0:NotInit  0000021d6a4d3350:0000021d6a9a00a8 <<

m_maxCount를 통해 Semaphore가 "new SemaphoreSlim(1, 1)" 조건으로 생성된 것임을 짐작게 합니다. 또한, m_currentCount가 0이므로 이미 다른 곳에서 SemaphoreSlim.Wait을 호출해 한 번의 lock 조건을 소진한 상태이고, m_waitCount가 6인 것으로 봐서 해당 lock을 대기하고 있는 곳이 총 6개, 결국 DebugDiag가 분석한 6개의 스레드와 일치하는 결과를 보입니다.

따라서, 6개의 스레드가 동일한 SemaphoreSlim 1개에 걸려 hang 상태가 발생한 것입니다.

여기서 결정적인 문제점 하나가 나오는데요, m_lockObj가 null로 나오는 것에서 이미 저 SemaphoreSlim은 Dispose를 호출한 상태라는 점입니다. 바로 이 문제를 지난 글에서 다뤘던 것입니다.




문제의 덤프 파일을 발생시켰던 응용 프로그램은 call stack에도 나오듯이 SuperSimpleTcp를 사용한 경우였습니다.

C# - SuperSimpleTcp 사용 시 주의할 점
; https://www.sysnet.pe.kr/2/0/13532

SimpleTcpServer.cs 파일의 SendInternal에 보면 이런 코드가 나옵니다.

private void SendInternal(string ipPort, long contentLength, Stream stream)
{
    if (!_clients.TryGetValue(ipPort, out ClientMetadata client)) return;
    if (client == null) return;

    long bytesRemaining = contentLength;
    int bytesRead = 0;
    byte[] buffer = new byte[_settings.StreamBufferSize];

    try
    {
        client.SendLock.Wait();

        while (bytesRemaining > 0)
        {
            bytesRead = stream.Read(buffer, 0, buffer.Length);
            if (bytesRead > 0)
            {
                if (!_ssl) client.NetworkStream.Write(buffer, 0, bytesRead); 
                else client.SslStream.Write(buffer, 0, bytesRead); 

                bytesRemaining -= bytesRead;
                _statistics.SentBytes += bytesRead;
            }
        }

        if (!_ssl) client.NetworkStream.Flush();
        else client.SslStream.Flush();
        _events.HandleDataSent(this, new DataSentEventArgs(ipPort, contentLength));
    }
    finally
    {
        if (client != null) client.SendLock.Release();
    }
}

try/finally를 했기 때문에 쌍이 잘 맞습니다. 문제는, DisconnectClient 코드에서 나옵니다.

public void DisconnectClient(string ipPort)
{
    if (string.IsNullOrEmpty(ipPort)) throw new ArgumentNullException(nameof(ipPort));

    if (!_clients.TryGetValue(ipPort, out ClientMetadata client))
    {
        Logger?.Invoke($"{_header}unable to find client: {ipPort}");
    }
    else
    {
        if (!_clientsTimedout.ContainsKey(ipPort))
        {
            Logger?.Invoke($"{_header}kicking: {ipPort}");
            _clientsKicked.TryAdd(ipPort, DateTime.Now);
        }
    }

    if (client != null)
    {
        if (!client.TokenSource.IsCancellationRequested)
        {
            client.TokenSource.Cancel();
            Logger?.Invoke($"{_header}requesting disposal of: {ipPort}");
        }

        client.Dispose();
    }
}

ClientMetaData.cs에 정의된 client 인스턴스의 Dispose 메서드를 보면,

public void Dispose()
{ 
    if (TokenSource != null)
    {
        if (!TokenSource.IsCancellationRequested)
        {
            TokenSource.Cancel();
            TokenSource.Dispose();
        }
    }

    if (_sslStream != null)
    {
        _sslStream.Close(); 
    }

    if (_networkStream != null)
    {
        _networkStream.Close(); 
    }

    if (_tcpClient != null)
    {
        _tcpClient.Close();
        _tcpClient.Dispose(); 
    }

    SendLock.Dispose();
    ReceiveLock.Dispose();
}

단순히 SendLock.Dispose() 호출만을 하고 있습니다. 즉, (이전에 설명한 대로) 다른 SendInternal에서 Wait하고 있는 스레드들은 영원히 hang 상태에 빠져든 체로, 일종의 메모리 leak을 유발하게 되는 것입니다.




SuperSimpleTcp가 생성한 SemaphoreSlim은,

internal SemaphoreSlim SendLock = new SemaphoreSlim(1, 1);
internal SemaphoreSlim ReceiveLock = new SemaphoreSlim(1, 1);

lock(obj), Mutex 역할과 동일합니다. 하지만 그래도 SemaphoreSlim을 사용할 수밖에 없는 이유는 SendInternalAsync와 같은 비동기 함수를 제공하고 있기 때문인데요, 예전 글에 설명했던 대로,

C# - Mutex의 비동기 버전
; https://www.sysnet.pe.kr/2/0/13157

Mutex의 경우 비동기 구현을 하려면 결국 동기 방식의 스레드를 쓰거나 아니면 SemaphoreSlim으로 우회해야 합니다. (또한, lock, 즉 Monitor.Enter/Exit은 비동기 구문을 지원하지 않습니다.)

결국 비동기로 인해 SemaphoreSlim를 쓸 수밖에 없었던 것입니다. 만약, 비동기를 포기한다면 SendInternalAsync를 사용하지 않고 오직 SendInternal만 사용한다면 SemaphoreSlim 대신 lock을 사용하는 것이 문제를 그나마 쉽게 해결할 수 있습니다.

private void SendInternal(string ipPort, long contentLength, Stream stream)
{
    if (!_clients.TryGetValue(ipPort, out ClientMetadata client)) return;
    if (client == null) return;

    long bytesRemaining = contentLength;
    int bytesRead = 0;
    byte[] buffer = new byte[_settings.StreamBufferSize];

    bool lockTaken = false;
 
    try
    {
        // client.SendLock.Wait();
        Monitor.Enter(client.SendLock, ref lockTaken);

        while (bytesRemaining > 0)
        {
            bytesRead = stream.Read(buffer, 0, buffer.Length);
            if (bytesRead > 0)
            {
                if (!_ssl) client.NetworkStream.Write(buffer, 0, bytesRead); 
                else client.SslStream.Write(buffer, 0, bytesRead); 

                bytesRemaining -= bytesRead;
                _statistics.SentBytes += bytesRead;
            }
        }

        if (!_ssl) client.NetworkStream.Flush();
        else client.SslStream.Flush();
        _events.HandleDataSent(this, new DataSentEventArgs(ipPort, contentLength));
    }
    finally
    {
        if (lockTaken)
        {
            Monitor.Exit(client.SendLock);
        }

        // if (client != null) client.SendLock.Release();
    }
}

이와 함께, 혹시라도 SendInternalAsync를 사용할 수도 있으므로 비동기 관련 메서드들은 모두 private 처리하는 것이 좋습니다.




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







[최초 등록일: ]
[최종 수정일: 2/19/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)
13560정성태2/19/20242074디버깅 기술: 195. windbg 분석 사례 - Semaphore 잠금으로 인한 Hang 현상 (닷넷)
13559정성태2/19/20242939오류 유형: 895. ASP.NET - System.Security.SecurityException: 'Requested registry access is not allowed.'
13558정성태2/18/20242164닷넷: 2217. C# - 최댓값이 1인 SemaphoreSlim 보다 Mutex 또는 lock(obj)를 선택하는 것이 나은 이유
13557정성태2/18/20241914Windows: 258. Task Scheduler의 Author 속성 값을 변경하는 방법
13556정성태2/17/20241958Windows: 257. Windows - Symbolic (hard/soft) Link 및 Junction 차이점
13555정성태2/15/20242113닷넷: 2216. C# - SemaphoreSlim 사용 시 주의점
13554정성태2/15/20241857VS.NET IDE: 189. Visual Studio - 닷넷 소스코드 디컴파일 찾기가 안 될 때
13553정성태2/14/20241939닷넷: 2215. windbg - thin/fat lock 없이 동작하는 Monitor.Wait + Pulse
13552정성태2/13/20241888닷넷: 2214. windbg - Monitor.Enter의 thin lock과 fat lock
13551정성태2/12/20242085닷넷: 2213. ASP.NET/Core 웹 응용 프로그램 - 2차 스레드의 예외로 인한 비정상 종료
13550정성태2/11/20242199Windows: 256. C# - Server socket이 닫히면 Accept 시켰던 자식 소켓이 닫힐까요?
13549정성태2/3/20242501개발 환경 구성: 706. C# - 컨테이너에서 실행하기 위한 (소켓) 콘솔 프로젝트 구성
13548정성태2/1/20242332개발 환경 구성: 705. "Docker Desktop for Windows" - ASP.NET Core 응용 프로그램의 소켓 주소 바인딩(IPv4/IPv6 loopback, Any)
13547정성태1/31/20242085개발 환경 구성: 704. Visual Studio - .NET 8 프로젝트부터 dockerfile에 추가된 "USER app" 설정
13546정성태1/30/20241942Windows: 255. (디버거의 영향 등으로) 대상 프로세스가 멈추면 Socket KeepAlive로 연결이 끊길까요?
13545정성태1/30/20241859닷넷: 2212. ASP.NET Core - 우선순위에 따른 HTTP/HTTPS 호스트:포트 바인딩 방법
13544정성태1/30/20241883오류 유형: 894. Microsoft.Data.SqlClient - Could not load file or assembly 'System.Security.Permissions, ...'
13543정성태1/30/20241881Windows: 254. Windows - 기본 사용 중인 5357 포트 비활성화는 방법
13542정성태1/30/20241913오류 유형: 893. Visual Studio - Web Application을 실행하지 못하는 IISExpress - 두 번째 이야기
13541정성태1/29/20241955VS.NET IDE: 188. launchSettings.json의 useSSL 옵션
13540정성태1/29/20242076Linux: 69. 리눅스 - "Docker Desktop for Windows" Container 환경에서 IPv6 Loopback Address 바인딩 오류
13539정성태1/26/20242365개발 환경 구성: 703. Visual Studio - launchSettings.json을 이용한 HTTP/HTTPS 포트 바인딩
13538정성태1/25/20242417닷넷: 2211. C# - NonGC(FOH) 영역에 .NET 개체를 생성파일 다운로드1
13537정성태1/24/20242501닷넷: 2210. C# - Native 메모리에 .NET 개체를 생성파일 다운로드1
13536정성태1/23/20242588닷넷: 2209. .NET 8 - NonGC Heap / FOH (Frozen Object Heap) [1]
13535정성태1/22/20242432닷넷: 2208. C# - GCHandle 구조체의 메모리 분석
1  2  [3]  4  5  6  7  8  9  10  11  12  13  14  15  ...