stowage icon indicating copy to clipboard operation
stowage copied to clipboard

System.IO.IOException: The process cannot access the file when calling Ren on the local disk storage

Open intelligide opened this issue 7 months ago • 0 comments

When the Ren method is called from local disk storage, an exception is thrown:

System.IO.IOException: The process cannot access the file '<filename>' because it is being used by another process.
   at System.IO.FileSystem.DeleteFile(String fullPath)
   at Stowage.Impl.LocalDiskFileStorage.Rm(IOPath path, CancellationToken cancellationToken)
   at Stowage.PolyfilledFileStorage.RenFile(IOPath oldPath, IOPath newPath, CancellationToken cancellationToken)
   at Stowage.PolyfilledFileStorage.Ren(IOPath oldPath, IOPath newPath, CancellationToken cancellationToken)

It seems to come from the PolyfilledFileStorage.RenFile function, because it tries to delete the file while it's still open for reading.

Modifying the function as follows removes the error:

        private async Task RenFile(IOPath oldPath, IOPath newPath, CancellationToken cancellationToken = default) {
            using(Stream? src = await OpenRead(oldPath, cancellationToken)) {
                if(src != null) {
                    using(Stream dest = await OpenWrite(newPath, cancellationToken)) {
                        await src.CopyToAsync(dest);
                    }
                }
            }

            await Rm(oldPath); // <-- Move the file deletion outside the using scope
        }

intelligide avatar May 21 '25 18:05 intelligide