Skip to content

Sharing violation on assembly after calling AssemblyDefinition.ReadAssembly #291

@lewurm

Description

@lewurm

With bumping Cecil to a more recent version (from a version from 2013 or something), we run into a problem on xamarin-android, where we get

Sharing violation on path /Users/bernhardu/work/benchmarker/tools/AndroidAgent/obj/Release/android/assets/shrunk/Mono.Android.dll

The problem starts here: https://github.com/xamarin/xamarin-android/blob/59ec488b4005a09fc0e5f330f217e78c3fa14724/src/Xamarin.Android.Build.Tasks/Tasks/RemoveRegisterAttribute.cs#L27 (we call AssemblyDefinition.ReadAssembly ()). Afterwards we do some other file operations on that same assembly. I see that the process still has a filedescriptor on the assembly in question open and thus fails with the error above. I looked a bit into the code, and I think https://github.com/mono/cecil/blob/505b07d6974d8405a63124139733c6fdc0e67bc7/Mono.Cecil.PE/ImageReader.cs#L29 needs to call Disposable () for its Stream member eventually. Here is a test case that highlights the problem:

using System;
using System.IO;
using Mono.Cecil;

namespace Cecil.Samples {
        public class MainClass {
                private void RunSample () {
                        var dataPath = Path.Combine (Path.GetDirectoryName (GetType ().Assembly.Location) ,"Mono.Cecil.dll");
                        Console.WriteLine ("datapath: {0}" ,dataPath);
                        if (!File.Exists (dataPath)) {
                                Console.Error.WriteLine ("need a valid file");
                                return;
                        }
                        var r = ModuleDefinition.ReadModule (dataPath);
                        FileStream f = File.Open (dataPath ,FileMode.Open);
                        Console.WriteLine ("do something with r: {0}" ,r);
                        Console.WriteLine ("do something with f: {0}" ,f);
                }

                public static void Main (string[] args) {
                        new MainClass ().RunSample ();
                }
        }
}
$ mono sharedviolation/bin/Debug/sharedviolation.exe
datapath: /Users/bernhardu/work/cecil-repro/cecil.samples/sharedviolation/bin/Debug/Mono.Cecil.dll

Unhandled Exception:
System.IO.IOException: Sharing violation on path /Users/bernhardu/work/cecil-repro/cecil.samples/sharedviolation/bin/Debug/Mono.Cecil.dll
  at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions options) [0x0025f] in <269653efc2ae4ad59c04a426f96119ad>:0
  at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share) [0x00000] in <269653efc2ae4ad59c04a426f96119ad>:0
  at (wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare)
  at System.IO.File.Open (System.String path, System.IO.FileMode mode) [0x00000] in <269653efc2ae4ad59c04a426f96119ad>:0
  at Cecil.Samples.MainClass.RunSample () [0x00053] in <fbfd0a81d94a4268a2289d89cc81ba19>:0
  at Cecil.Samples.MainClass.Main (System.String[] args) [0x00006] in <fbfd0a81d94a4268a2289d89cc81ba19>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.IO.IOException: Sharing violation on path /Users/bernhardu/work/cecil-repro/cecil.samples/sharedviolation/bin/Debug/Mono.Cecil.dll
  at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions options) [0x0025f] in <269653efc2ae4ad59c04a426f96119ad>:0
  at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share) [0x00000] in <269653efc2ae4ad59c04a426f96119ad>:0
  at (wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare)
  at System.IO.File.Open (System.String path, System.IO.FileMode mode) [0x00000] in <269653efc2ae4ad59c04a426f96119ad>:0
  at Cecil.Samples.MainClass.RunSample () [0x00053] in <fbfd0a81d94a4268a2289d89cc81ba19>:0
  at Cecil.Samples.MainClass.Main (System.String[] args) [0x00006] in <fbfd0a81d94a4268a2289d89cc81ba19>:0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions