Skip to content

System.IO.IOException: The directory is not empty from TemporaryExeFile.Dispose() #61

Closed
@mikhail-tin

Description

@mikhail-tin

Hi there!
We catch a lot of exceptions on a machine with slow HDD(Azure).
It's very simple to reproduce on the machine like we have in Azure, but impossible on local machine.
I assume the reason is OS can not free resources so fast.

System.IO.IOException: The directory is not empty.

   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.Directory.DeleteHelper(String fullPath, String userPath, Boolean recursive, Boolean throwOnTopLevelDirectoryNotFound, WIN32_FIND_DATA& data)
   at System.IO.Directory.Delete(String fullPath, String userPath, Boolean recursive, Boolean checkHost)
   at Medallion.Shell.Signals.WindowsProcessSignaler.**TemporaryExeFile.Dispose()**
   at Medallion.Shell.Signals.WindowsProcessSignaler.<TrySignalAsync>d__1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Medallion.Shell.Command.<>c__DisplayClass16_0.<<TrySignalAsync>g__TrySignalHelperAsync|0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

I suggest using "try catch" and two attempts

            public void Dispose()
            {
                var toDelete = Interlocked.Exchange(ref this._path, null);
                if (toDelete != null)
                {
                   this.Cleanup(toDelete);
                }
            }

            private void Cleanup(string toDelete, bool firstAttempt = true)
            {
                try
                {
                    File.Delete(toDelete);
                    Directory.Delete(System.IO.Path.GetDirectoryName(toDelete));
                }
                catch (IOException)
                {
                    if (firstAttempt)
                    {
                        Task.Delay(0).GetAwaiter().GetResult();
                        this.Cleanup(toDelete, false);
                    }
                }
            }

Thx!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions