{"id":105501,"date":"2021-07-30T07:00:00","date_gmt":"2021-07-30T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=105501"},"modified":"2021-07-29T21:00:15","modified_gmt":"2021-07-30T04:00:15","slug":"20210730-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20210730-00\/?p=105501","title":{"rendered":"Why doesn&#8217;t my asynchronous read operation complete when I close the handle?"},"content":{"rendered":"<p>A customer was using asynchronous I\/O with an I\/O completion port. At the time they wanted to shut things down, they still had an outstanding asynchronous read. To get things to clean up, they closed the file handle, expecting it to cause the <code>Read\u00adFile<\/code> to complete and fail with an error like <code>ERROR_<wbr \/>INVALID_<wbr \/>HANDLE<\/code>. But instead, what they found was that the read operation remained outstanding, and nothing completed.<\/p>\n<p>What&#8217;s going on?<\/p>\n<p>What&#8217;s going on is that when you close the file handle, that decrements an internal reference count on the underlying file object, and that internal reference count is not yet zero, so the file is still open. And where did that extra reference count come from?<\/p>\n<p>From the <code>Read\u00adFile<\/code> operation itself!<\/p>\n<p>In the kernel, one of the things that happens when you pass a handle from an application is that the kernel validates the handle and obtains a reference-counted pointer to the underlying kernel object, which temporarily bumps the object reference count by one. If you close the handle, that drops it back down, but there is still the outstanding reference from the I\/O operation, and that outstanding reference won&#8217;t go away until the I\/O operation completes.<\/p>\n<p>As a side note, closing the handle to an object while there is still outstanding work on that object feels really sketchy to me. It&#8217;s getting dangerously close to &#8220;destroying an object while simultaneously using it&#8221;.<\/p>\n<p>What you need to do is cancel the I\/O by calling a function like <code>Cancel\u00adIo<\/code> or <code>Cancel\u00adIo\u00adEx<\/code>. And in order to cancel the I\/O, you need to proide a handle to the file whose I\/O you want to cancel.<\/p>\n<p>Another reason not to close that handle yet.<\/p>\n<p>When you cancel the I\/O, the I\/O will complete with an error code saying that it was cancelled. At that point, you can close the file handle and clean up.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Because the file isn&#8217;t closed yet.<\/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-105501","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Because the file isn&#8217;t closed yet.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/105501","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=105501"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/105501\/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=105501"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=105501"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=105501"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}