{"id":112235,"date":"2026-04-15T07:00:00","date_gmt":"2026-04-15T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=112235"},"modified":"2026-04-15T11:14:30","modified_gmt":"2026-04-15T18:14:30","slug":"20260415-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20260415-00\/?p=112235","title":{"rendered":"Why is there a long delay between a thread exiting and the <CODE>Wait&shy;For&shy;Single&shy;Object<\/CODE> returning?"},"content":{"rendered":"<p>A customer reported that they were using the <code>Wait\u00adFor\u00adSingle\u00adObject<\/code> function to wait for a thread to exit, but they found that even though the thread had exited, the <code>Wait\u00adFor\u00adSingle\u00adObject<\/code> call did not return for over a minute. What could explain this delay in reporting the end of a thread? Can we do something to speed it up?<\/p>\n<p>My psychic powers tell me that the thread didn&#8217;t actually exit.<\/p>\n<p>What the customer is observing is probably that their thread procedure has returned, signaling the end of the thread. But a lot of stuff happens after the thread procedure exits. The system needs to send <code>DLL_<wbr \/>THREAD_<wbr \/>DETACH<\/code> notifications to all of the DLLs (unless the DLL has opted out via <code>Disable\u00adThread\u00adLibrary\u00adCalls<\/code>), and doing so requires the loader lock.<\/p>\n<p>I would use the debugger to look for the thread you <i>thought<\/i> had exited and see what it&#8217;s doing. It might be blocked waiting for the loader lock because some other thread is hogging it. Or it could be running a DLL&#8217;s detach code, and that detach code has gotten stuck on a long-running operation.<\/p>\n<p>I suspect it&#8217;s the latter: One of the DLLs is waiting for something in its detach code, and that something takes about a minute.<\/p>\n<p>We didn&#8217;t hear back from the customer, which could mean that this was indeed the problem. Or it could mean that this didn&#8217;t help, but they decided that we weren&#8217;t being helpful and didn&#8217;t pursue the matter further. Unfortunately, in a lot of these customer debugging engagements, we never hear back whether our theory worked. (Another possibility is that the customer wrote back with a &#8220;thank you&#8221;, but the customer liaison didn&#8217;t forward it to the engineering team because they didn&#8217;t want to bother them any further.)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Maybe it didn&#8217;t really exit.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-112235","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Maybe it didn&#8217;t really exit.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/112235","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=112235"}],"version-history":[{"count":1,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/112235\/revisions"}],"predecessor-version":[{"id":112236,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/112235\/revisions\/112236"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=112235"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=112235"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=112235"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}