{"id":95955,"date":"2017-04-13T07:00:00","date_gmt":"2017-04-13T21:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/?p=95955"},"modified":"2019-03-13T01:09:31","modified_gmt":"2019-03-13T08:09:31","slug":"20170413-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20170413-00\/?p=95955","title":{"rendered":"Can memcpy go into an infinite loop? Why is it being blamed for a busy hang?"},"content":{"rendered":"<p>A customer reported that their program locks up once or twice each day. They took a full dump of the file while it was hung and asked <code>windbg<\/code> to analyze it. Here&#8217;s a redacted version of what it said: <\/p>\n<pre>\nPRIMARY_PROBLEM_CLASS: APPLICATION_HANG_BusyHang\n\nSTACK_TEXT:  \nmsvcr80!_memcpy+0x7d\ncontoso!Buffer::Compact+0x3d\ncontoso!BufferReader::Parse+0x14c\ncontoso!handle_widget_message+0x37\ncontoso!handle_input+0x12f\ncontoso!dispatch_event+0x27\ncontoso!handle_events+0xbe\n<\/pre>\n<p>The <code>Buffer::Compact<\/code> method shifts some memory around inside the buffer: <\/p>\n<pre>\nvoid Buffer::Compact()\n{\n  if (m_bytesRead &gt; 0) {\n    memmove(m_buffer, m_buffer + m_bytesRead, m_capacity - m_bytesRead);\n    m_capacity -= m_bytesRead;\n    m_bytesRead = 0;\n  }\n}\n<\/pre>\n<p>&#8220;Is it possible that <code>memmove<\/code> has a busy wait? What could it be waiting for?&#8221; <\/p>\n<p>The <code>memmove<\/code> function doesn&#8217;t have a busy loop where it waits for something. It just moves the memory from one location to another. <\/p>\n<p>What&#8217;s probably happening is that there is a busy loop higher up the stack. Maybe  <code>Buffer&shy;Reader::Parse<\/code> has gotten into a loop, or (my guess) <code>handle_events<\/code> is stuck in a loop processing a huge number of incoming events. <\/p>\n<p>When you take the memory dump, you are capturing the program at a moment in time. All you know is that the thread is probably in a busy wait, but the source of the busy wait need not be the thing at the top of the stack. <\/p>\n<p>If <code>memcpy<\/code> is consistently at the top of the stack, then it means that the thread is spending most of its time copying memory. But that doesn&#8217;t necessarily mean that <code>memcpy<\/code> is stuck in a loop. The more likely reason is that the thread is busy doing some larger operation, and that larger operation entails a lot of <code>memcpy<\/code> operations. <\/p>\n<p><a HREF=\"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/20040827-00\/?p=38033\">Though in extreme edge cases it might be a busy loop<\/a>. <\/p>\n<p><a HREF=\"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/20150813-00\/?p=91821\">Sort-of related<\/a>. <\/p>\n<p><b>Exercise<\/b>: The customer&#8217;s code calls <code>memmove<\/code>, so why is the <code>memcpy<\/code> function the one at the top of the stack? What happened to <code>memmove<\/code>? <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Look at the bigger picture.<\/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-95955","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Look at the bigger picture.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/95955","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=95955"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/95955\/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=95955"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=95955"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=95955"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}