Bug 38335 - ZipFile.Open leaks its file handle if the load fails
Summary: ZipFile.Open leaks its file handle if the load fails
Alias: None
Product: Class Libraries
Classification: Mono
Component: General ()
Version: master
Hardware: PC Windows
: --- normal
Target Milestone: Untriaged
Assignee: João Matos
Depends on:
Reported: 2016-02-02 20:00 UTC by Matthew Orlando
Modified: 2016-02-03 16:35 UTC (History)
2 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Matthew Orlando 2016-02-02 20:00:45 UTC
The way ZipArchives are constructed is leaky in the face of exceptions. See http://stackoverflow.com/questions/8526732/dealing-with-exceptions-in-constructor-when-implementing-idisposable

Code to reproduce:

// Create an invalid zip to force loading failure
var filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "foo.zip");
using (var created = File.CreateText(filePath))
    created.WriteLine("This is not a valid zip file.");

    using (var zipFile = ZipFile.Open(filePath, ZipArchiveMode.Read))
        Console.WriteLine("This would hit if zip file is valid");
catch (Exception)
    // Failed to load zip. Prove file is still open
        using (var busted = File.Open(filePath, FileMode.Open, FileAccess.ReadWrite))
            Console.WriteLine("Successfully re-opened; this should happen but does not");
        Console.WriteLine("Failed to re-open; this should not happen but does");
Comment 1 Matthew Orlando 2016-02-02 21:08:55 UTC
Pull request submitted: https://github.com/mono/mono/pull/2563
Comment 2 João Matos 2016-02-03 16:35:23 UTC
Fixed by https://github.com/mono/mono/pull/2563.