Commit e84bbfc
[Java.Interop.Tools.Cecil] Fix DirectoryAssemblyResolver.Dispose() (#88)
Context: #87 (comment)
Context: https://bugzilla.xamarin.com/show_bug.cgi?id=44529
(Private) Bug #44529 is an `IOException` on `xamarin-android/master`
due to file sharing:
Error executing task StripEmbeddedLibraries: System.IO.IOException: Sharing violation on path .../obj/Release/linksrc/Xamarin.Android.NUnitLite.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 <253a3790b2c44512bbca8669ecfc1822>:0
at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share) [0x00000] in <253a3790b2c44512bbca8669ecfc1822>:0
at (wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare)
at Mono.Cecil.ModuleDefinition.GetFileStream (System.String fileName, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share) [0x00007] in :0
at Mono.Cecil.ModuleDefinition.Write (System.String fileName, Mono.Cecil.WriterParameters parameters) [0x00007] in :0
at Mono.Cecil.AssemblyDefinition.Write (System.String fileName, Mono.Cecil.WriterParameters parameters) [0x00001] in :0
at Xamarin.Android.Tasks.StripEmbeddedLibraries.Execute () [0x0034a] in <3d5202a5d4874a76a99388021bf1ab1a>:0
The underlying cause of this change is the migration to
Cecil 0.10.0-beta1-v2 (dfed286), which -- along with API changes --
has some *semantic* changes [^0].
In particular, within Cecil <= 0.9.x, `AssemblyDefinition` was
entirely in-memory. Starting with Cecil 0.10.x, `AssemblyDefinition`
isn't; it is backed by a `System.IO.Stream`, which can be in-memory
(if `ReaderParameters.InMemory` is `true`), or a `FileStream`
(the default).
This normally might not be bad, except we also have
`Java.Interop.Tools.Cecil.DirectoryAssemblyResolver`, which *caches*
all created `AssemblyDefinition` instances.
Thus, "normal" *correct* Cecil use would be fine, so long as you know
all assemblies which have been loaded, load them with the correct
`ReaderParameters`, and promptly `Dispose()` of the
`AssemblyDefinition` when done.
`DirectoryAssemblyResolver` throws a wrench in that, because
(1) commit dfed286 incorrectly implemented
`DirectoryAssemblyResolver.Dispose()`, leaving all of the cached
`AssemblyDefinition` instances still "live", which means
(2) The lifetime of the `Stream` underlying the `AssemblyDefinition`
is controlled by the GC, which can mean nearly anything.
This is all a *huge* recipe for confusion.
Fix `DirectoryAssemblyResolver.Dispose()` so that the cached
`AssemblyDefinition` instances are `Dispose()`d of, and review all use
of `DirectoryAssemblyResolver` within Java.Interop to ensure that any
created instances are appropriate `Dispose()`d of.
Additionally, add a new `DirectoryAssemblyResolver` constructor to
allow controlling the `ReaderParameters` that
`DirectoryAssemblyResolver.Load()` uses when loading an assembly:
partial class DirectoryAssemblyResolver {
public DirectoryAssemblyResolver (
Action<string, object[]> logWarnings,
bool loadDebugSymbols,
ReaderParameters loadReaderParameters = null);
}
The new `loadReaderParameters` allows specifying the default
`ReaderParameters` values to use when
`DirectoryAssemblyResolver.Load()` is invoked. This ensures that all
assemblies loaded by `DirectoryAssemblyResolver` are loaded in a
consistent fashion (e.g. readonly, read+write, in-memory), which will
hopefully allow code to be sanely reasoned about.
[^0]: The best kind of changes!1 parent b259a81 commit e84bbfc
File tree
4 files changed
+55
-9
lines changed- src
- Java.Interop.Tools.Cecil/Java.Interop.Tools.Cecil
- Java.Interop.Tools.JavaCallableWrappers/Java.Interop.Tools.JavaCallableWrappers
- tools
- generator
- jcw-gen
4 files changed
+55
-9
lines changedLines changed: 38 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
64 | 64 | | |
65 | 65 | | |
66 | 66 | | |
67 | | - | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
68 | 73 | | |
69 | 74 | | |
70 | 75 | | |
71 | 76 | | |
72 | 77 | | |
73 | 78 | | |
74 | 79 | | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
75 | 97 | | |
76 | 98 | | |
| 99 | + | |
77 | 100 | | |
78 | 101 | | |
79 | 102 | | |
| |||
104 | 127 | | |
105 | 128 | | |
106 | 129 | | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
107 | 133 | | |
108 | | - | |
109 | | - | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
110 | 145 | | |
111 | 146 | | |
112 | 147 | | |
| |||
Lines changed: 7 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
68 | 68 | | |
69 | 69 | | |
70 | 70 | | |
71 | | - | |
| 71 | + | |
72 | 72 | | |
73 | | - | |
74 | | - | |
75 | | - | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
76 | 77 | | |
77 | | - | |
| 78 | + | |
| 79 | + | |
78 | 80 | | |
79 | 81 | | |
80 | 82 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
201 | 201 | | |
202 | 202 | | |
203 | 203 | | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
204 | 211 | | |
205 | 212 | | |
206 | 213 | | |
| |||
229 | 236 | | |
230 | 237 | | |
231 | 238 | | |
232 | | - | |
233 | 239 | | |
234 | 240 | | |
235 | 241 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
71 | 71 | | |
72 | 72 | | |
73 | 73 | | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
74 | 77 | | |
75 | 78 | | |
76 | 79 | | |
| |||
0 commit comments