성태의 닷넷 이야기
홈 주인
모아 놓은 자료
프로그래밍
질문/답변
사용자 관리
사용자
메뉴
아티클
외부 아티클
유용한 코드
온라인 기능
MathJax 입력기
최근 덧글
[정성태] Reordering on an Alpha processor ;...
[정성태] 공유 감사합니다. ^^ 참고로, WPF에서 WindowsF...
[Tom Lee] 답변 감사합니다. 나름의 해결책 연구해보고 여기에도 공유해봅니다...
[정성태] 아래의 글을 보면, MoveWindow 하면 될 듯한데요. ^^...
[Tom Lee] 안녕하세요 올려주신 글 참고하여 WPF 어플리케이션 안에 Uni...
[정성태] A graphical depiction of the steps ...
[정성태] 질문을 주셔서 출판사 측에 문의를 했습니다. 약 한 달 정도 후...
[Thorondor
] @정성태 개인 블로그인데도 거의 커뮤니티 급 인 것 같아요. 요...
[정성태] Roll A Lisp In C - Reading ; https...
[정성태] Java - How to use the Foreign Funct...
글쓰기
제목
이름
암호
전자우편
HTML
홈페이지
유형
제니퍼 .NET
닷넷
COM 개체 관련
스크립트
VC++
VS.NET IDE
Windows
Team Foundation Server
디버깅 기술
오류 유형
개발 환경 구성
웹
기타
Linux
Java
DDK
Math
Phone
Graphics
사물인터넷
부모글 보이기/감추기
내용
<div style='display: inline'> <h1 style='font-family: Malgun Gothic, Consolas; font-size: 20pt; color: #006699; text-align: center; font-weight: bold'>C# - WPF의 Dispatcher Queue로 알아보는 await 호출의 hang 현상</h1> <p> 예전 글을 하나 다시 다뤄볼까요? ^^<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > .NET Framework: 394. async/await 사용 시 hang 문제가 발생하는 경우 ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/1541'>https://www.sysnet.pe.kr/2/0/1541</a> </pre> <br /> 위의 글에서 다룬 코드를 다시 정리해 보면 아래의 상황에서,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > private void Window_Loaded(object sender, RoutedEventArgs e) { Task<string> task = GetMyText(); this.textBox1.Text = <span style='color: blue; font-weight: bold'>task.Result;</span> // 무한 대기!!! } async Task<string> GetMyText() { <span style='color: blue; font-weight: bold'>await Task.Delay(1);</span> return "Hello World"; } </pre> <br /> task.Result를 호출하는 시점에 WPF 응용 프로그램의 UI 스레드가 중지하는 현상이 발생합니다. 원인은, 위의 글에서 이미 밝혔듯이 task.Result로 대기한 UI 스레드가 GetMyText에서 Task.Delay(1) 이후에 실행되는 callback을 실행하지 못해 영원히 대기 상태로 빠지기 때문입니다.<br /> <br /> 이것을 다시 풀이해 보면, GetMyText에서 1ms 이후에 timer 알람을 받은 스레드풀의 스레드가 Dispatch Queue에 (await 이후의 코드를 담은) callback 메서드를 추가합니다. 그런데, 그 Queue에 쌓인 작업을 UI 스레드가 실행을 해야 비로소 Task의 상태가 Completed로 빠지게 되는데요, 문제는 그렇게 실행해야 할 UI 스레드가 Window_Loaded 코드를 실행하느라, 정확히는 task.Result를 대기하느라 Dispatch Queue의 작업을 수행하지 못하고 있다는 점입니다.<br /> <br /> 그로 인해 서로가 서로를 기다리게 되는 무한 대기 상태로 빠진 것입니다.<br /> <a name='SetOnInvokeMres'></a> <br /> 참고로, Task.Result는 내부적으로 SetOnInvokeMres 호출로 처리가 되는데요, 이에 대해서는 전에 분석한 적이 있습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > async/await 사용 시 hang 문제가 발생하는 경우 - 두 번째 이야기 ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/10801'>https://www.sysnet.pe.kr/2/0/10801</a> </pre> <br /> Task.Result + SetOnInvokeMres 호출은 결국 다음과 같은 식으로 처리하는 것에 대한 도우미 함수라고 봐도 무방합니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > private async void Window_Loaded(object sender, RoutedEventArgs e) { Task<string> task = GetMyText(); <span style='color: blue; font-weight: bold'>EventWaitHandle ewh = new EventWaitHandle(false, EventResetMode.ManualReset); task.ContinueWith((t) => { ewh.Set(); }); ewh.WaitOne();</span> this.textBox1.Text = await task; } </pre> <br /> <hr style='width: 50%' /><br /> <br /> 위와 같은 상태에서, <a target='tab' href='https://www.sysnet.pe.kr/2/0/13563'>(도움이 안 되는) windbg</a>를 연결해 스레드 상태를 보면 이렇게 나옵니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > 0:021> <span style='color: blue; font-weight: bold'>!threads</span> ThreadCount: 4 UnstartedThread: 0 BackgroundThread: 3 PendingThread: 0 DeadThread: 0 Hosted Runtime: no Lock ID OSID ThreadOBJ State GC Mode GC Alloc Context Domain Count Apt Exception 0 1 2054 000001ad39df0ef0 2026020 Preemptive 000001AD3BC22CC0:000001AD3BC23FD0 000001ad39dc7810 0 STA 5 2 591c 000001ad39e1dac0 2b220 Preemptive 0000000000000000:0000000000000000 000001ad39dc7810 0 MTA (Finalizer) 17 4 8f1c 000001ad39e1cb20 102a220 Preemptive 0000000000000000:0000000000000000 000001ad39dc7810 0 MTA (Threadpool Worker) 18 5 a904 000001ad39e1abe0 1029220 Preemptive 000001AD3BC20AD8:000001AD3BC21FD0 000001ad39dc7810 0 MTA (Threadpool Worker) 0:021> <span style='color: blue; font-weight: bold'>~0s</span> 0:000> <span style='color: blue; font-weight: bold'>!clrstack</span> OS Thread Id: 0x2054 (0) Child SP IP Call Site 000000087ff9c6b8 00007ff94000a034 [HelperMethodFrame_1OBJ: 000000087ff9c6b8] System.Threading.SynchronizationContext.WaitHelper(IntPtr[], Boolean, Int32) 000000087ff9ca00 00007ff8dd2c0e6a System.Windows.Threading.DispatcherSynchronizationContext.Wait(IntPtr[], Boolean, Int32) 000000087ff9cd78 00007ff9245612c3 [GCFrame: 000000087ff9cd78] 000000087ff9cf70 00007ff9245612c3 [GCFrame: 000000087ff9cf70] 000000087ff9d0b8 00007ff9245612c3 [HelperMethodFrame_1OBJ: 000000087ff9d0b8] System.Threading.Monitor.ObjWait(Boolean, Int32, System.Object) 000000087ff9d1d0 00007ff9197914d4 System.Threading.ManualResetEventSlim.Wait(Int32, System.Threading.CancellationToken) 000000087ff9d260 00007ff91975921b System.Threading.Tasks.Task.SpinThenBlockingWait(Int32, System.Threading.CancellationToken) [f:\dd\ndp\clr\src\BCL\system\threading\Tasks\Task.cs @ 3320] 000000087ff9d2d0 00007ff91a019c91 System.Threading.Tasks.Task.InternalWait(Int32, System.Threading.CancellationToken) [f:\dd\ndp\clr\src\BCL\system\threading\Tasks\Task.cs @ 3259] 000000087ff9d3a0 00007ff91a0c9957 <span style='color: blue; font-weight: bold'>System.Threading.Tasks.Task`1[[System.__Canon, mscorlib]].GetResultCore(Boolean)</span> [f:\dd\ndp\clr\src\BCL\system\threading\Tasks\Future.cs @ 562] 000000087ff9d3e0 00007ff8c4da618a <span style='color: blue; font-weight: bold'>WpfApp1.MainWindow.Window_Loaded</span>(System.Object, System.Windows.RoutedEventArgs) ...[생략]... 000000087ff9ee60 00007ff8db02b541 System.Windows.Application.RunDispatcher(System.Object) 000000087ff9eea0 00007ff8db02af7c System.Windows.Application.RunInternal(System.Windows.Window) 000000087ff9ef00 00007ff8c4da08f2 WpfApp1.App.Main() 000000087ff9f148 00007ff9245612c3 [GCFrame: 000000087ff9f148] 0:000> <span style='color: blue; font-weight: bold'>~17s</span> ntdll!NtDelayExecution+0x14: 00007ff9`42bef9f4 c3 ret 0:017> <span style='color: blue; font-weight: bold'>!clrstack</span> OS Thread Id: 0x8f1c (17) Child SP IP Call Site GetFrameContext failed: 1 0000000000000000 0000000000000000 0:017> <span style='color: blue; font-weight: bold'>~18s</span> ntdll!NtWaitForSingleObject+0x14: 00007ff9`42bef3f4 c3 ret 0:018> <span style='color: blue; font-weight: bold'>!clrstack</span> OS Thread Id: 0xa904 (18) Child SP IP Call Site GetFrameContext failed: 1 0000000000000000 0000000000000000 </pre> <br /> 호출 스택으로 봐서 Window_Loaded의 task.Result에서 blocking이 걸린 것은 나오지만, lock의 유형이 <a target='tab' href='https://www.sysnet.pe.kr/2/0/13552'>System.Threading.Monitor.ObjWait이기 때문에 critical section에 해당하는 것은 아니어서 그 이상 어떠한 lock 정보도 구할 수 없습니다.</a><br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > 0:000> <span style='color: blue; font-weight: bold'>!dumpheap -thinlock</span> Address MT Size Found 0 objects. 0:000> <span style='color: blue; font-weight: bold'>!syncblk</span> Index SyncBlock MonitorHeld Recursion Owning Thread Info SyncBlock Owner ----------------------------- Total 93 CCW 12 RCW 35 ComClassFactory 0 Free 0 </pre> <br /> 하지만, 지난 글을 통해서,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > C# - WPF의 Dispatcher Queue 동작 확인 ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/13570'>https://www.sysnet.pe.kr/2/0/13570</a> C# - await 호출과 WPF의 Dispatcher Queue 동작 확인 ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/13571'>https://www.sysnet.pe.kr/2/0/13571</a> </pre> <br /> 우리는 저 hang을 풀어줄 작업이 Dispatcher Queue에 쌓여 있음을 짐작할 수 있습니다. (그러니까, 바로 이 설명을 하기 위해서 ^^ 지난 2개의 글을 쓴 것입니다.)<br /> <br /> 실제로 위의 예제에서 hang을 풀어주는 테스트를 간단하게 해볼까요? 현재 문제는, Task.Delay(1)의 시간이 지난 후 스레드풀의 스레드가 Dispatcher Queue에 작업을 추가했지만 이후 그 작업이 실행되지 않고 있는 것인데요, 이것을 Hooks_OperationPosted를 이용해 직접 호출해 주는 식으로 바꿔보면,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > private void Hooks_OperationPosted(object sender, System.Windows.Threading.DispatcherHookEventArgs e) { string name = GetName(e.Operation); string target = "System.Threading.Tasks.SynchronizationContextAwaitTaskContinuation+<>c.<.cctor>b__8_0"; if (name == target) System.Delegate method = GetMethod(e.Operation); object objArg = GetArg(e.Operation); if (method is Action action) { action(); } else if (method is SendOrPostCallback callback) { <span style='color: blue; font-weight: bold'>callback(objArg);</span> } } } </pre> <br /> blocking이 해제되면서 텍스트 박스에 "Hello World"가 출력되는 것을 확인할 수 있습니다. 하지만, Hooks_OperationPosted에서 한 번 실행했던 callback으로 인해 UI 스레드의 blocking이 해제되면서 (이후 Dispatcher Queue에 쌓인 작업을 실행하는 과정에서) <a target='tab' href='https://www.sysnet.pe.kr/2/0/13570#run_twice'>다시 한번 더 callback을 실행</a>하기 때문에 GetMyText 메서드를 벗어나는 "}" 블록에 "System.InvalidOperationException: 'An attempt was made to transition a task to a final state when it had already completed.'" 예외가 발생하게 됩니다.<br /> <br /> 왜냐하면, Task와 달리 결과를 반환하는 Task<T>는 SetResult 메서드를 호출하게 되는데, <br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > // AsyncTaskMethodBuilder.cs [__DynamicallyInvokable] public void SetResult(TResult result) { Task<TResult> task = m_task; if (task == null) { m_task = GetTaskForResult(result); return; } if (AsyncCausalityTracer.LoggingOn) { AsyncCausalityTracer.TraceOperationCompletion(CausalityTraceLevel.Required, task.Id, AsyncCausalityStatus.Completed); } if (System.Threading.Tasks.Task.s_asyncDebuggingEnabled) { System.Threading.Tasks.Task.RemoveFromActiveTasks(task.Id); } if (<span style='color: blue; font-weight: bold'>task.TrySetResult(result)</span>) { return; } <span style='color: blue; font-weight: bold'>throw new InvalidOperationException(Environment.GetResourceString("TaskT_TransitionToFinal_AlreadyCompleted"));</span> } </pre> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > // Task.cs internal bool <span style='color: blue; font-weight: bold'>TrySetResult</span>(TResult result) { <span style='color: blue; font-weight: bold'>if (base.IsCompleted) { return false; }</span> if (AtomicStateUpdate(67108864, 90177536)) { m_result = result; Interlocked.Exchange(ref m_stateFlags, m_stateFlags | 0x1000000); m_contingentProperties?.SetCompleted(); FinishStageThree(); return true; } return false; } </pre> <br /> 하필 저게 2번 불리면 예외가 발생하도록 코딩이 돼 있기 때문입니다.<br /> <br /> <hr style='width: 50%' /><br /> <br /> 자, 그럼 Hooks_OperationPosted에서 (두 번 실행하지 않도록) Queue를 비우고 callback을 수행하면 더 좋을 듯합니다. 그래서 다음과 같은 코드를 넣어두면,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > // Dispatcher가 포함한 Queue 필드 // private PriorityQueue<DispatcherOperation> _queue; static object GetQueue(object objValue) { Type type = objValue.GetType(); FieldInfo fi = type.GetField("_queue", BindingFlags.Instance | BindingFlags.NonPublic); return fi.GetValue(objValue); } static int GetCount(object objValue) { Type type = objValue.GetType(); FieldInfo fi = type.GetField("_count", BindingFlags.Instance | BindingFlags.NonPublic); return (int)fi.GetValue(objValue); } static void Dequeue(object objValue) { Type type = objValue.GetType(); MethodInfo mi = type.GetMethod("Dequeue", BindingFlags.Instance | BindingFlags.Public); mi.Invoke(objValue, null); } <span style='color: blue; font-weight: bold'> void ClearQueue(System.Windows.Threading.DispatcherHookEventArgs e) { object queue = GetQueue(e.Operation.Dispatcher); while (true) { int count = GetCount(queue); if (count <= 0) { break; } Dequeue(queue); } } </span> private void Hooks_OperationPosted(object sender, System.Windows.Threading.DispatcherHookEventArgs e) { string name = GetName(e.Operation); string target = "System.Threading.Tasks.SynchronizationContextAwaitTaskContinuation+<>c.<.cctor>b__8_0"; if (name == target) { <span style='color: blue; font-weight: bold'>ClearQueue(e);</span> System.Delegate method = GetMethod(e.Operation); object objArg = GetArg(e.Operation); if (method is Action action) { action(); } else if (method is SendOrPostCallback callback) { <span style='color: blue; font-weight: bold'>callback(objArg);</span> } } } </pre> <br /> callback을 Hooks_OperationPosted에서 한 번만 수행하기 때문에 hang 현상 없이, 예외도 없이 잘 실행됩니다. (물론, callback에서 수행해야 할 코드가, 즉 await 이후에 실행하는 코드가 UI 접근을 포함한다면 예외가 발생합니다.)<br /> <br /> (참고로, 저 코드는 Dispatcher를 이해하는 차원에서 작성된 것일 뿐 현업에서 사용할 만한 코드는 아닙니다.)<br /> <br /> <hr style='width: 50%' /><br /> <br /> 조금 더 응용하면, Dispatcher Queue에 있는 작업 수를 출력하는 부가 스레드를 제작해,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > namespace WpfApp1 { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); <span style='color: blue; font-weight: bold'>#if DEBUG Dispatcher.Hooks.OperationPosted += Hooks_OperationPosted; Thread t = new Thread(() => { object queue = GetQueue(Application.Current.Dispatcher); while (true) { int count = GetCount(queue); Console.WriteLine($"# of items in Dispatcher Queue: {count}, last called at: {_lastStarted}"); Thread.Sleep(5000); } }); t.IsBackground = true; t.Start(); #endif</span> } #if DEBUG DateTime _lastStarted; private void Hooks_OperationStarted(object sender, DispatcherHookEventArgs e) { _lastStarted = DateTime.Now; } #endif } } </pre> <br /> 실행했을 때, hang 상태에 빠진 것에 대한 대략적인 상황을 인식하는 정보를 얻는 디버깅 용도로는 쓸만합니다. ^^<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > ...[생략]... # of items in Dispatcher Queue: 12, last called at: 2024-03-04 오전 1:03:39 # of items in Dispatcher Queue: 12, last called at: 2024-03-04 오전 1:03:39 # of items in Dispatcher Queue: 12, last called at: 2024-03-04 오전 1:03:39 # of items in Dispatcher Queue: 12, last called at: 2024-03-04 오전 1:03:39 ...[생략]... </pre> <br /> 위의 경우 1:03:39분에 실행된 작업 이래로, hang 상태에 빠졌음을 짐작할 수 있습니다. 물론, 특정 작업이, 예를 들어 ListBox에 10_000_000개의 항목을 집어넣느라 UI 스레드가 의도적으로 바쁘게 일하고 있는 경우도 있을 것입니다. ^^<br /> <br /> (<a target='tab' href='https://www.sysnet.pe.kr/bbs/DownloadAttachment.aspx?fid=2144&boardid=331301885'>첨부 파일은 이 글의 예제 코드를 포함</a>합니다.)<br /> </p><br /> <br /><hr /><span style='color: Maroon'>[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]</span> </div>
첨부파일
스팸 방지용 인증 번호
6759
(왼쪽의 숫자를 입력해야 합니다.)