{"id":96215,"date":"2017-05-24T07:00:00","date_gmt":"2017-05-24T21:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/?p=96215"},"modified":"2019-03-13T01:11:53","modified_gmt":"2019-03-13T08:11:53","slug":"20170524-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20170524-00\/?p=96215","title":{"rendered":"Some questions about unflushed data and calling FlushFileBuffers on a new handle to a file"},"content":{"rendered":"<p>Consider the following sequence of events: <\/p>\n<ol>\n<li>Process A opens a file with <code>Create&shy;File<\/code>     with attributes that include <i>neither<\/i>     <code>FILE_FLAG_NO_BUFFERING<\/code>     nor     <code>FILE_FLAG_WRITE_THROUGH<\/code>. <\/li>\n<li>Process A writes to the file with <code>Write&shy;File<\/code>.     These writes are internally buffered since we didn&#8217;t disable buffering. <\/li>\n<li>Process A crashes without calling <code>Close&shy;Handle<\/code>,     and before the operating system&#8217;s internal buffers     are flushed to disk. <\/li>\n<\/ol>\n<p>First question: Under these conditions, will the data written in step&nbsp;2 be lazy-written to disk? Or is it at risk of being lost forever because the handle wasn&#8217;t closed? <\/p>\n<p>Let&#8217;s look at the last part first. Whether the process closed the handle before crashing doesn&#8217;t affect the story, because the kernel will close all the handles as part of process cleanup. The handle does get closed eventually. Whether the handle closure was done explicitly by the app or implicitly by the kernel doesn&#8217;t affect the answer. <\/p>\n<p>Okay, now let&#8217;s look at the first part: Yes, the data written in step&nbsp;2 will eventually be lazy-written to disk, assuming your system doesn&#8217;t crash before then. <\/p>\n<p>And that&#8217;s the middle part of the question: The data is at risk of being lost forever, but not because the handle wasn&#8217;t closed. It&#8217;s at risk of being lost forever because the system might crash before it gets flushed out. <\/p>\n<p>Okay, let&#8217;s extend the scenario: <\/p>\n<ol>\n<li VALUE=\"4\">Before the operating system flushes its internal     buffers naturally,     Process B opens the same file, with the same attributes. <\/li>\n<li>Process B calls <code>Flush&shy;File&shy;Buffers<\/code>. <\/li>\n<\/ol>\n<p>Does this call to <code>Flush&shy;File&shy;Buffers<\/code> cause the data written by Process A to be flushed to disk? <\/p>\n<p>Yes. A call to <code>Flush&shy;File&shy;Buffers<\/code> will flush data for that file, even if the data was written by a different handle. <\/p>\n<p>If <code>Flush&shy;File&shy;Buffers<\/code> is never called, then the operating system will flush the buffer at its convenience. <\/p>\n<p><b>Note<\/b>: In step 2, the relevant call is <code>Write&shy;File<\/code>. If you write the data to the file using a runtime-provided function like <code>fwrite<\/code>, then that data might be sitting in the runtime&#8217;s buffer without ever triggering a <code>Write&shy;File<\/code>. Only when the data is written with <code>Write&shy;File<\/code> does the data actually reach the operating system&#8217;s buffers. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Will it get flushed out eventually?<\/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-96215","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Will it get flushed out eventually?<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/96215","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=96215"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/96215\/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=96215"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=96215"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=96215"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}