Commit 78f8863
authored
[Xamarin.Android.Build.Tasks] close
Fixes: #9133
Context: https://discord.com/channels/732297728826277939/732297916680765551/1308554103206580244
Context: 86260ed
Various customers have been reporting `UnauthorizedAccessExceptions`
in incremental builds, which seems to be a new problem in .NET 9.
We were not able to reproduce the issue locally, but with the number
of reports it seems to be a real issue.
One customer shared a [`MSBuild.dmp`][0] file (while the file was
locked), where I could observe the objects in memory:
MemoryMappedViewStream 132
Mono.Cecil.PE.Image 100
Mono.Cecil.ModuleDefinition 100
Mono.Cecil.TypeDefinition 100
Mono.Cecil.TypeDefinition[] 100
List<Mono.Cecil.TypeDefinition> 1
Xamarin.Android.Tasks.NativeCodeGenState [Static variable Xamarin.Android.Tasks.NativeCodeGenState.<Template>k__BackingField] 1
Then realized the problem was:
* We opened some `.dll` files with `Mono.Cecil`.
* They were never closed.
* Future incremental build attempts would fail on various
operations of the same `.dll` files.
We were also storing some static state (`NativeCodeGenState`) to be
shared across multiple MSBuild tasks:
partial class NativeCodeGenState {
public static NativeCodeGenState? Template { get; set; }
}
`NativeCodeGenState` also holds a `XAAssemblyResolver` in a `Resolver`
property. This means this `XAAssemblyResolver` instance would *also*
be kept alive.
It appears we only use the static `Template` property for a `bool`
flag, so I changed the property to a `bool` instead:
partial class NativeCodeGenState {
public static bool TemplateJniAddNativeMethodRegistrationAttributePresent { get; set; }
}
After this change, we can safely dispose `Resolver` instances.
I looped over the `NativeCodeGenState` instances disposing of each
`Resolver` at the end of the `<GenerateJavaStubs/>` and
`<GeneratePackageManagerJava/>` MSBuild tasks.
[0]: https://drive.google.com/file/d/12dOkO6ZdbK67sNeY_PVS4d0mgnLyOwUA/view?pli=1XAAssemblyResolvers (#9531)1 parent 22f23dd commit 78f8863
File tree
3 files changed
+25
-5
lines changed- src/Xamarin.Android.Build.Tasks
- Tasks
- Utilities
3 files changed
+25
-5
lines changedLines changed: 15 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
222 | 222 | | |
223 | 223 | | |
224 | 224 | | |
225 | | - | |
226 | | - | |
227 | | - | |
228 | 225 | | |
229 | 226 | | |
230 | 227 | | |
| |||
262 | 259 | | |
263 | 260 | | |
264 | 261 | | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
265 | 265 | | |
266 | 266 | | |
267 | 267 | | |
| |||
270 | 270 | | |
271 | 271 | | |
272 | 272 | | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
273 | 285 | | |
274 | 286 | | |
275 | 287 | | |
| |||
Lines changed: 9 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
331 | 331 | | |
332 | 332 | | |
333 | 333 | | |
334 | | - | |
| 334 | + | |
335 | 335 | | |
336 | 336 | | |
337 | 337 | | |
| |||
395 | 395 | | |
396 | 396 | | |
397 | 397 | | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
398 | 406 | | |
399 | 407 | | |
400 | 408 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
15 | | - | |
| 15 | + | |
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| |||
0 commit comments