{"id":7973,"date":"2012-03-29T07:00:00","date_gmt":"2012-03-29T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2012\/03\/29\/why-cant-i-delete-a-file-immediately-after-terminating-the-process-that-has-the-file-open\/"},"modified":"2012-03-29T07:00:00","modified_gmt":"2012-03-29T07:00:00","slug":"why-cant-i-delete-a-file-immediately-after-terminating-the-process-that-has-the-file-open","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20120329-00\/?p=7973","title":{"rendered":"Why can&#039;t I delete a file immediately after terminating the process that has the file open?"},"content":{"rendered":"<p>\nA customer discovered a bug where terminating a process did not\nclose the handles that the process had open, resulting in their\nemergency cleanup code not working:\n<\/p>\n<pre>\nTerminateProcess(processHandle, EXITCODE_TERMINATED);\nDeleteFile(someFile);\n<\/pre>\n<p>\nTheir workaround was to insert a call to\n<code>Wait&shy;For&shy;Single&shy;Object(process&shy;Handle, 500)<\/code>\nbefore deleting the file.\nThe customer wanted to know whether they discovered a bug\nin <code>Terminate&shy;Process<\/code>,\nand they were concerned that their workaround could add up to\na half second to their cleanup code,\nduring which the end user is sitting there waiting for everything\nto clean up.\n<\/p>\n<p>\nAs MSDN notes,\n<\/p>\n<blockquote CLASS=\"q\"><p>\n<b>TerminateProcess<\/b> <u>initiates termination and returns immediately<\/u>.\nThis stops execution of all threads within the process\nand requests cancellation of all pending I\/O.\nThe terminated process cannot exit\nuntil all pending I\/O has been completed or canceled.\n<\/p><\/blockquote>\n<p>\n(Emphasis mine.)\n<\/p>\n<p>\nTermination is begun, but the function does not wait\nfor termination to complete.\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2004\/07\/23\/192531.aspx\">\nSometimes a thread gets stuck<\/a>\nbecause a device driver has gotten\nwedged (or the driver doesn&#8217;t support cancellation).\n<\/p>\n<p>\nTo know when the handles are closed,\nwait on the process handle,\nbecause the process handle is not signaled until\nprocess termination is complete.\nIf you are concerned that this can take too long,\nyou can do like the customer suggested and wait with a timeout.\nOf course, if the timeout expires,\nthen you have to decide what to do next.\nYou can&#8217;t delete the file, since it&#8217;s still open,\nbut maybe you can log an error diagnostic and let the user know why\nthings are taking so long to clean up,\nand maybe add the file to a list of files to clean up the next\ntime the program starts up.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A customer discovered a bug where terminating a process did not close the handles that the process had open, resulting in their emergency cleanup code not working: TerminateProcess(processHandle, EXITCODE_TERMINATED); DeleteFile(someFile); Their workaround was to insert a call to Wait&shy;For&shy;Single&shy;Object(process&shy;Handle, 500) before deleting the file. The customer wanted to know whether they discovered a bug in [&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":[25],"class_list":["post-7973","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>A customer discovered a bug where terminating a process did not close the handles that the process had open, resulting in their emergency cleanup code not working: TerminateProcess(processHandle, EXITCODE_TERMINATED); DeleteFile(someFile); Their workaround was to insert a call to Wait&shy;For&shy;Single&shy;Object(process&shy;Handle, 500) before deleting the file. The customer wanted to know whether they discovered a bug in [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/7973","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=7973"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/7973\/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=7973"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=7973"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=7973"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}