Skip to content

System.IO.IOException: The process cannot access the file because it is being used by another process #824

@adiessl

Description

@adiessl

Describe the bug

We have a solution containing multiple projects that use Microsoft.Web.LibraryManager.Build for restoring JavaScipt libraries. Some of these projects reference the same library from the same provider. We have multiple build servers, and on our more recent, faster build server we noticed builds are regularly failing with error messages of the following kind:

C:\nuget\microsoft.web.librarymanager.build\3.0.71\build\Microsoft.Web.LibraryManager.Build.targets(35,9): error : Error during copying file System.IO.IOException: The process cannot access the file 'C:\Users\[username]\AppData\Local\.librarymanager\cache\jsdelivr\jquery\3.7.1\dist\jquery.min.js' because it is being used by another process.
C:\nuget\microsoft.web.librarymanager.build\3.0.71\build\Microsoft.Web.LibraryManager.Build.targets(35,9): error :    at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
C:\nuget\microsoft.web.librarymanager.build\3.0.71\build\Microsoft.Web.LibraryManager.Build.targets(35,9): error :    at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
C:\nuget\microsoft.web.librarymanager.build\3.0.71\build\Microsoft.Web.LibraryManager.Build.targets(35,9): error :    at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
C:\nuget\microsoft.web.librarymanager.build\3.0.71\build\Microsoft.Web.LibraryManager.Build.targets(35,9): error :    at Microsoft.Web.LibraryManager.Contracts.FileHelpers.<CopyFileAsync>d__4.MoveNext()

This is the method this exception occurs in:

public static async Task<bool> CopyFileAsync(string sourceFile, string destinationFile, ILogger logger, CancellationToken cancellationToken)
{
//Sometimes it flaky. Let's retry
for (int i=0; i<2;i++)
{
try
{
using FileStream sourceStream = File.Open(sourceFile, FileMode.Open, FileAccess.Read);
await WriteToFileAsync(destinationFile, sourceStream, cancellationToken);
return true;
}
catch (Exception exception)
{
logger.Log($"Error during copying file {exception}", LogLevel.Error);
}
}
return false;

The problematic line ist probably the following:

using FileStream sourceStream = File.Open(sourceFile, FileMode.Open, FileAccess.Read);

I would suggest changing it as follows:

using FileStream sourceStream = File.Open(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read);

This should allow multiple processes to access the same file at the same time.

While at it, a Task.Delay between the first and second iteration might also help to reduce the flakiness.

Additional context

Microsoft.Web.LibraryManager.Build, Version 3.0.71

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions