IL: map the short-lived metadata-only PE reader - #20489
Open
auduchinok wants to merge 3 commits into
Open
Conversation
This view exists only to parse the PE headers and copy the resources out, and is disposed immediately after, so reading the whole assembly into a managed byte array materialises megabytes to touch a few pages. Mapping it is safe because neither pectxtEager nor pevEager may be captured by the results. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Contributor
|
The cleanup lived in an explicit IDisposable implementation, which Stream.Dispose() never reaches, so getBinaryFile's disposer left the file mapped until the safe handles were finalized. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
T-Gro
reviewed
Sep 9, 2026
| // For metadata-only, always use a temporary, short-lived PE file reader, preferably over a memory mapped file. | ||
| // Then use the metadata blob as the long-lived memory resource. | ||
| let disposer, pefileEager = getBinaryFile fullPath false | ||
| let disposer, pefileEager = getBinaryFile fullPath true |
Member
There was a problem hiding this comment.
🤖🕵️ [P2] Empty references leave a read handle open until GC, blocking an exclusive reopen after FCS reports the invalid assembly. CreateFromFile throws before disposer exists; the mapping-construction failure path needs to dispose its opened FileStream.
open System
open System.IO
open FSharp.Compiler.CodeAnalysis
open FSharp.Compiler.Text
let dll = Path.GetFullPath "empty.dll"
let script = Path.GetFullPath "check.fsx"
File.WriteAllBytes(dll, [||])
let source = SourceText.ofString (sprintf "#r @\"%s\"\nlet value = 1" dll)
let checker = FSharpChecker.Create()
let options, _ =
checker.GetProjectOptionsFromScript(script, source, useSdkRefs = true)
|> Async.RunSynchronously
// Keep GC from hiding the leaked handle during this repro.
if GC.TryStartNoGCRegion(256L * 1024L * 1024L) then
try
checker.ParseAndCheckFileInProject(script, 0, source, options)
|> Async.RunSynchronously |> ignore
use retry = new FileStream(dll, FileMode.Open, FileAccess.ReadWrite, FileShare.None)
() // Base succeeds; mapped path throws IOException on Windows.
finally
GC.EndNoGCRegion()
T-Gro
self-requested a review
September 9, 2026 08:58
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
OpenILModuleReader's metadata-only branch — the FCS/IDE path,reduceMemoryUsage = Yes,metadataOnly = Yes— opens a temporary PE reader to parse the headers, locate the metadata directory andcopy the resources out, then throws it away:
Allocation
Time
Warm whole-solution, 10 in-process passes