Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Deleting Ain't Easy

DZone's Guide to

Deleting Ain't Easy

Free Resource

Maintain Application Performance with real-time monitoring and instrumentation for any application. Learn More!

We started getting test failures in Voron regarding being unable to clean up the data directory. Which was a major cause for concern. We thought that we had a handle leak, or something like that.

As it turned out, we could reproduce this issue with the following code:


 class Program
 {
     static void Main(string[] args)
     {
         var name = Guid.NewGuid().ToString();
         for (int i = 0; i < 1000; i++)
         {
             Console.WriteLine(i);
             Directory.CreateDirectory(name);
  
             for (int j = 0; j < 10; j++)
             {
                 File.WriteAllText(Path.Combine(name, "test-" +j), "test");
             }
  
             if (Directory.Exists(name))
                 Directory.Delete(name, true);
         }
         
     }
 }

If you run this code, in 6 out of 10 runs, it would fail with something like this:

I am pretty sure that the underlying cause is some low lever “thang” somewhere on my system. FS Indexing, AV, etc. But it basically means that directory delete is not atomic, and that I should basically just suck it up and write a robust delete routine that can handle failure and retry until it is successful.


Collect, analyze, and visualize performance data from mobile to mainframe with AutoPilot APM. Learn More!

Topics:

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}