Artem
Artem

Reputation: 11

C# .Net Core problem with create FileStream (docker on mapped volume from Mac OS host)

My application executing in docker container.

Host is MacOs

Container is Linux

Running in debug

In docker-compose I mapped volume

volumes:
      - /Users/blabla/Temp:/temp

In this case time to time when I try to create FileStream I got exception System.IO.FileNotFoundException

StackTrace:

at Interop.ThrowExceptionForIoErrno(ErrorInfo errorInfo, String path, Boolean isDirectory, Func`2 errorRewriter)\n at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String path, OpenFlags flags, Int32 mode)\n at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize)\n at System.IO.Strategies.OSFileStreamStrategy..ctor(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize)\n at System.IO.Strategies.FileStreamHelpers.ChooseStrategy(FileStream fileStream, String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, Int64 preallocationSize)\n at System.IO.FileStream..ctor(String path, FileMode mode)

var mappedFolderPath = "/temp/testFile.png";
var exist = System.IO.File.Exists(mappedFolderPath);

for (int i = 0; i < 1000; i++)
  {
    try
    {
      using var streamForTest = System.IO.File.OpenRead(mappedFolderPath);
      var justForTest = streamForTest.Length;
    }
    catch (Exception ex)
    {
        **// Happen after 3-4 times.**
        var exception = ex;
    }
 }

But in case when I copy file to output directory I dont have this exception

.csproj fragment

...
 <ItemGroup>
    <None Update="StaticContent\testFile.png">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
  </ItemGroup>
...
var root = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
var path = Path.Combine(root, "StaticContent", "testFile.png");
var exist = System.IO.File.Exists(path);

for (int i = 0; i < 1000; i++)
  {
    try
    {
      using var streamForTest = System.IO.File.OpenRead(mappedFolderPath);
      var justForTest = streamForTest.Length;
    }
    catch (Exception ex)
    {
        **// Never to happen.**
        var exception = ex;
    }
 }

In two cases System.IO.File.Exists(path) = true

What is wrong here?

Upvotes: 1

Views: 438

Answers (1)

Artem
Artem

Reputation: 11

Answer: After restarting docker the problem is solved.

Upvotes: 0

Related Questions