Skip to content

Fix FileStream leak in ClassPath.IsJmodFile#1424

Open
jonathanpeppers wants to merge 1 commit intodotnet:mainfrom
jonathanpeppers:op-fix-isjmodfile-stream-leak
Open

Fix FileStream leak in ClassPath.IsJmodFile#1424
jonathanpeppers wants to merge 1 commit intodotnet:mainfrom
jonathanpeppers:op-fix-isjmodfile-stream-leak

Conversation

@jonathanpeppers
Copy link
Copy Markdown
Member

Summary

IsJmodFile opened a FileStream via File.OpenRead but never disposed it. Both the early-return path (when fewer than 4 bytes are read) and the normal-return path left the handle open, leaking a file descriptor on every call. Under high-volume classpath scanning this can exhaust OS file descriptors.

Change

In src/Xamarin.Android.Tools.Bytecode/ClassPath.cs, add using to the stream declaration in IsJmodFile:

-var f = File.OpenRead (jmodFile);
+using var f = File.OpenRead (jmodFile);

This matches the pattern already used by the companion method IsJarFile (line 133), which correctly wraps its stream in a using statement.

Verification

  • dotnet build -t:Prepare && dotnet build — ✅ 0 errors
  • dotnet test tests/Xamarin.Android.Tools.Bytecode-Tests/Xamarin.Android.Tools.Bytecode-Tests.csproj — ✅ 75 passed, 2 skipped

Add using to File.OpenRead in IsJmodFile so the stream is disposed on all return paths, matching the pattern used by the companion IsJarFile method.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 8, 2026 19:00
@jonathanpeppers jonathanpeppers added the ready-to-review This PR is ready to review/merge, thanks! label May 8, 2026
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a file-handle leak in Xamarin.Android.Tools.Bytecode by ensuring ClassPath.IsJmodFile disposes the FileStream it opens while probing JMOD headers, preventing file descriptor exhaustion during high-volume classpath scanning.

Changes:

  • Wrap the File.OpenRead (jmodFile) stream in a using var declaration so it is always disposed (including early-return paths).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-to-review This PR is ready to review/merge, thanks!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants