dotnet / dotnet/fsharp

FsiEvaluationSession.Create does not unload FSI-ASSEMBLY when `collectible = true`

Open
#15,669 11 comments 1 reaction 0 assignees View on GitHub
Area-FSI Bug Impact-Low
Dominant language
F#
Stars
4.3k
Forks
876
Avg merge
4d 22h
Merged PRs (30d)
144

Description

**Succinct description**

When `collectible = true` is passed as an argument to `FsiEvaluationSession.Create`, the FSI-ASSEMBLY is never unloaded, contrary to expectations.

**Repro steps**

The problem can be reproduced by following these steps:

1. Run the provided collectionTest function in the Collectible code generation section of the FCS API documentation: https://fsharp.github.io/fsharp-compiler-docs/fcs/interactive.html#Collectible-code-generation
2. Observe that the FSI-ASSEMBLY is never unloaded.

**Expected behavior**

When `collectible = true` is set, the FSI-ASSEMBLY should be unloaded when it's no longer needed.

**Actual behavior**

The FSI-ASSEMBLY is never unloaded, even when `collectible = true`.

**Known workarounds**

There are currently no known workarounds for this issue.

**Potential solution**

A potential fix might involve modifying the `defineDynamicAssemblyAndLog` function in `ilreflect.fs` to ensure that the assembly is created within a `CollectibleAssemblyLoadContext`. Here's the current implementation of the function:

```csharp
let defineDynamicAssemblyAndLog (asmName, flags, asmDir: string) =
let asmB = AssemblyBuilder.DefineDynamicAssembly(asmName, flags)

if logRefEmitCalls then
printfn "open System"
printfn "open System.Reflection"
printfn "open System.Reflection.Emit"

printfn
"let assemblyBuilder%d = System.AppDomain.CurrentDomain.DefineDynamicAssembly(AssemblyName(Name=\"%s\"), enum %d, %A)"
(abs <| hash asmB)
asmName.Name
(LanguagePrimitives.EnumToValue flags)
asmDir

asmB
```

**Related information**

Operating system: Windows 10 Pro 21H2 (19044.3086)
.NET Runtime kind: .NET 7.0.306
Editing Tools: Visual Studio Code

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.