Deleting Ain't Easy
Join the DZone community and get the full member experience.
Join For FreeWe 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.
Published at DZone with permission of Oren Eini, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
Testing Applications With JPA Buddy and Testcontainers
-
MLOps: Definition, Importance, and Implementation
-
Fun Is the Glue That Makes Everything Stick, Also the OCP
-
Hibernate Get vs. Load
Comments