{"id":15783,"date":"2009-12-04T07:00:00","date_gmt":"2009-12-04T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2009\/12\/04\/when-there-is-a-long-line-of-people-waiting-for-a-shared-resource-you-want-to-investigate-the-person-who-is-hogging-the-resource-not-the-people-waiting-in-line-for-it\/"},"modified":"2009-12-04T07:00:00","modified_gmt":"2009-12-04T07:00:00","slug":"when-there-is-a-long-line-of-people-waiting-for-a-shared-resource-you-want-to-investigate-the-person-who-is-hogging-the-resource-not-the-people-waiting-in-line-for-it","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20091204-00\/?p=15783","title":{"rendered":"When there is a long line of people waiting for a shared resource, you want to investigate the person who is hogging the resource, not the people waiting in line for it"},"content":{"rendered":"<p>\nIf you see a long line of people waiting for a phone booth\n(note: this analogy assumes you remember how phone booths work),\nand you want to understand the reason for the long line,\ndo you\n<\/p>\n<ul>\n<li>Go to a person waiting in line and begin your investigation there?\n<li>Go to the phone booth (and the person inside)\n    and begin your investigation there?\n<\/ul>\n<p>\nIf there is a long line of people waiting for a single resource,\na resource that there is not normally a long line for,\nyou would probably look at the person who is using the resource\nto see if, for example, they are a chatterbox who will be on\nthe phone for an hour, or if the phone is being repaired\nor is otherwise not working properly.\n<\/p>\n<p>\nSimilarly, if you find that in your 20-thread program, 17 of\nthem are waiting for a single critical section,\nthen you probably want to investigate the thread that owns the\ncritical section to see whether (and why) it isn&#8217;t releasing it.\n<\/p>\n<blockquote CLASS=\"q\">\n<p>\nWhen testing a program, I encountered a hang that occurred\nafter doing&nbsp;X.\nThere are a few threads stuck in <code>LoadLibrary<\/code>,\nand about 40 threads stuck here:\n<\/p>\n<pre>\nntdll!KiFastSystemCallRet\nntdll!ZwWaitForSingleObject+0xc\nntdll!RtlpWaitForCriticalSection+0x132\nntdll!RtlEnterCriticalSection+0x46\nntdll!_LdrpInitialize+0xf0\nntdll!KiUserApcDispatcher+0x7\n<\/pre>\n<p>\nHere is one of the threads that is stuck in\n<code>LoadLibrary<\/code> [stack trace deleted].\nYou seem to be one of the people who work on the\ncomponent that is trying to load the library.\nCan you investigate why the program is stuck?\n<\/p>\n<\/blockquote>\n<p>\nThis person picked one of the people waiting in line\nand decided that they were the ones responsible for\nthe problem.\nBut if course, that person waiting in line is just\nanother victim of the person at the head of the line\nwho is hogging the critical section.\nIn this case, the critical section is the infamous\n<i>loader lock<\/i>.\nThat it&#8217;s the loader lock is obvious from the symptoms:\nWhat critical section\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2007\/09\/04\/4731478.aspx\">\ndoes every thread require when it starts up<\/a>?\nWhat critical section does <code>LoadLibrary<\/code> require?\n<\/p>\n<p>\nYou can use the <code>!critsec<\/code> debugger command to\nidentify the current owner of the loader lock,\nand then start studying that thread to see what the hold-up is.\n<\/p>\n<p>\nNote that I&#8217;m not saying that the thread that owns the resource\nis necessarily the culprit.\nThe problem could be in the resource itself,\nor it could be in\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2006\/12\/12\/1266392.aspx\">\nthe pattern of usage associated with that resource<\/a>.\nBut starting your investigation\nwith the owner of the resource is a good bet,\nbecause most of the time, the reason for the long wait queue\nis that the current owner of the resource isn&#8217;t releasing it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you see a long line of people waiting for a phone booth (note: this analogy assumes you remember how phone booths work), and you want to understand the reason for the long line, do you Go to a person waiting in line and begin your investigation there? Go to the phone booth (and the [&hellip;]<\/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":[26],"class_list":["post-15783","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-other"],"acf":[],"blog_post_summary":"<p>If you see a long line of people waiting for a phone booth (note: this analogy assumes you remember how phone booths work), and you want to understand the reason for the long line, do you Go to a person waiting in line and begin your investigation there? Go to the phone booth (and the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/15783","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=15783"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/15783\/revisions"}],"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=15783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=15783"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=15783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}