runtime icon indicating copy to clipboard operation
runtime copied to clipboard

MemoryMappedFile.CreateFromFile not able to open file for read

Open schellap opened this issue 10 years ago • 17 comments

@stephentoub, my changes for ILC integration with CLI https://github.com/dotnet/cli/pull/1363 failed in the CI. Is the below by design?

I narrowed it down to this scenario: XUnit runner process and the ILC's input project both refer to the same version of the NETStandard.Library. XUnit runner loads System.Console.dll from the shared packages cache location. ILC wants to compile an user's app using the same System.Console.dll specified in the project.json file and opens a MemoryMappedFile on it which errors out with:

The process cannot access the file 'C:\Users\schellap\.nuget\packages\runtime.win7.System.Console\4.0.0-rc2
-23811\runtimes\win7\lib\dotnet5.4\System.Console.dll' because it is being used by another process.

To repro,

  1. Download CLI and unzip into c:\cli
  2. set DOTNET=c:\cli
  3. Unzip this zip file temp2.zip or use below code.
  4. Run repro.bat from the zip file.
  5. Unexpected result.
        public static int Main(string[] args)
        {
            MemoryMappedFile mappedFile = null;
            while (true) try
            {
                mappedFile = MemoryMappedFile.CreateFromFile(args[0], FileMode.Open, null, 0, MemoryMappedFileAccess.Read);
                Console.WriteLine("Expected");
                return 100;
            }
            catch (IOException e)
            {
                Console.WriteLine("Unexpected " + e.Message);
            }
            finally
            {
                if (mappedFile != null)
                    mappedFile.Dispose();
            }
            return -1;
        }
:: populate the shared location
%DOTNET%\bin\dotnet.exe restore

:: compile the app
%DOTNET%\bin\dotnet.exe publish

:: delete system.console so shared system.console will be used
if exist "bin\Debug\dnxcore50\win7-x64\System.Console.dll" (del  "bin\Debug\dnxcore50\win7-x64\System.Console.dll")

:: run the process
bin\Debug\dnxcore50\win7-x64\temp2.exe %USERPROFILE%\.nuget\packages\runtime.win7.System.Console\4.0.0-rc2-23811\runtimes\win7\lib\dotnet5.4\System.Console.dll

schellap avatar Feb 13 '16 03:02 schellap

Cc @weshaggard @gkhanna79 @davidfowl @brthor @piotrpMSFT Cc @tijoytom

schellap avatar Feb 13 '16 03:02 schellap

Is the below by design?

MemoryMappedFile.CreateFromFile(string, ...) creates the FileStream for the specified path with FileShare.None.

Have you tried creating the FileStream yourself with FileShare.Read and then calling CreateFromFile with that?

stephentoub avatar Feb 13 '16 03:02 stephentoub

MemoryMappedFileAccess.Read

This doesn't suffice?

schellap avatar Feb 13 '16 03:02 schellap

No. That's about the access you have, not about sharing.

stephentoub avatar Feb 13 '16 04:02 stephentoub

Oh, thanks.

schellap avatar Feb 13 '16 04:02 schellap

@schellap, can you confirm if that fixes your issue?

stephentoub avatar Feb 13 '16 14:02 stephentoub

It is pretty unfortunate that the convenience overloads do not work well for the most common use case of memory mapped files: read-only access to data files. Can we change the share mode default for read only access?

jkotas avatar Feb 14 '16 00:02 jkotas

I agree it's a strange default, especially since the default when constructing a FileStream and not specifying a sharing mode is FileShare.Read (http://referencesource.microsoft.com/#mscorlib/system/io/filestream.cs,440). I don't know why FileShare.None was chosen as the default in this MemoryMappedFile case. However, my concern with changing it at this point is that it's a fairly observable difference, and if someone were to write code like what @schellap has, find that it works fine (once the change was made), and then later tries to run it on the full framework, it'll fail on the full framework because of the more restrictive default (http://referencesource.microsoft.com/#System.Core/System/IO/MemoryMappedFiles/MemoryMappedFile.cs,148). If that's not a concern, it would of course be trivial to change in corefx (https://github.com/dotnet/corefx/blob/master/src/System.IO.MemoryMappedFiles/src/System/IO/MemoryMappedFiles/MemoryMappedFile.cs#L150). cc: @ericstj, @weshaggard, @terrajobst

stephentoub avatar Feb 14 '16 03:02 stephentoub

I don't have any of the history for why the defaults are the way they are but I would be very hesitant to change the behavior of this from what we have on the full .NET Framework. I share @stephentoub's concern about the code working completely different when run on the two frameworks and it is something I think we should try to maintain consistency on.

weshaggard avatar Feb 15 '16 19:02 weshaggard

find that it works fine (once the change was made), and then later tries to run it on the full framework, it'll fail on the full framework because of the more restrictive default

You are really saying that we need to maintain bug-for-bug compatibility between full .NET Framework and corefx, so that core that works fine on corefx is guaranteed to work on full .NET Framework too. I does not make sense. .NET Core was not developed against this bar so far.

Changing default sharing mode for read-only access to be more permissive would fall into a category of non-breaking changes: the thing threw exceptions under some circumstances before, but it works without exceptions now. .NET Core has number of changes in this category.

jkotas avatar Feb 16 '16 07:02 jkotas

You are really saying that we need to maintain bug-for-bug compatibility between full .NET Framework and corefx, so that core that works fine on corefx is guaranteed to work on full .NET Framework too

I don't believe I'm saying that. There is a spectrum of possible differences and the impact they can have, and I'm saying I'm worried this one is a bit too far along that line... not every difference is created equal. And specifically, changing a default such that just using an API with all of the defaults works in one place but fails in another seems particularly onerous to me.

That said, as I stated, I don't feel strongly about it. If the consensus is that it should be changed, it's certainly an easy fix to make and I'd go along with it. Might be worth a design meeting discussion (cc: @terrajobst).

stephentoub avatar Feb 16 '16 12:02 stephentoub

Why not change both and update MSDN with a note or just update MSDN with a note? It should be useful to have two instances to open a file for read in both. This is only a relaxing change and if it is trivial I don't quite understand @weshaggard's hesitation.

schellap avatar Feb 16 '16 19:02 schellap

I've not dug into all the details of this issue but based on @stephentoub comments I would expect that making this change in the full .NET Framework would be a breaking change and couldn't be made there without some some of quirk at a minimum. I'm also not opposed to entertaining the idea of doing it but it isn't free and would require some work.

weshaggard avatar Feb 16 '16 20:02 weshaggard

I've not dug into all the details of this issue but based on @stephentoub comments I would expect that making this change in the full .NET Framework would be a breaking change

I'm actually not sure if it would be considered a breaking change, in that it would stop throwing exceptions in cases that were previously throwing exceptions.

The issue is this. You use MemoryMappedFile.CreateFromFile(string path, ...) to create an MMF. Under the covers, it creates a FileStream, using FileShare.None. That means if anyone else has that file open, CreateFromFile will throw an exception. The suggestion was to change that from FileShare.None to FileShare.Read, which means an exception won't be thrown if the file is opened for read.

stephentoub avatar Feb 16 '16 20:02 stephentoub

If that is the case we should definitely consider making this change even for .Net Framework

weshaggard avatar Feb 16 '16 20:02 weshaggard

@weshaggard, does that mean you think we should change it for corefx, regardless of whether it's changed in the full framework?

stephentoub avatar Feb 16 '16 20:02 stephentoub

Having a little closer look I think it is probably OK to have this change only in corefx but it would still be nice to have it in full framework as people are hitting it there as well (see http://stackoverflow.com/questions/24435260/how-to-have-shared-read-only-access-to-a-memory-mapped-file). I still don't know enough about how FileShare ends up working at the OS level to say whether or not switching from FileShare.None to FileShare.Read will have any other repercussions, but from what I can gather it seems like it should be OK.

weshaggard avatar Feb 16 '16 20:02 weshaggard