Skip to content

Opening a tar.gz file directly from the network makes SharpZipLib to an attempt to access an already disposed Stream #379

Closed

Description

@koide

I'm downloading a file from an FTP server and unzipping, untarring it on the fly, via the underlying streams. The code looks like this:

var request = WebRequest.Create("ftp://host/file.tgz");
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential("user","password");
var response = (FtpWebResponse) await request.GetResponseAsync();
using (var gzStr = new GZipInputStream(response.GetResponseStream()))
{
    gzStr.IsStreamOwner = false;
    var tar = TarArchive.CreateInputTarArchive(gzStr);
    Directory.CreateDirectory(_targetPath);
    tar.IsStreamOwner = false;
    tar.ExtractContents(_targetPath);
}

With the current 1.2.0 release (and 1.1.0 and 1.0.0) I get an ObjectDisposedException on the network socket stream, but it happens after the whole file has been extracted successfully, so it seems to be yet another exceptional case happening after finishing the file.

In my total ignorance of this code, I've tracked the issue to this line:

catch (Exception ex) when (completedLastBlock && (ex is GZipException || ex is EndOfStreamException))

 catch (Exception ex) when (completedLastBlock && 
    (ex is GZipException || ex is EndOfStreamException)) 

and, changing it to

catch (Exception ex) when (completedLastBlock && 
    (ex is GZipException || ex is EndOfStreamException ||
     ex is ObjectDisposedException))

makes my specific case work successfully. I think that the server ends the connection after the file is downloaded so the stream is not there to be read from anymore.

Activity

  1. added
    tar
    on Aug 23, 2019
  2. Numpsy commented on Aug 24, 2019

    @Numpsy
    Contributor

    The discussion @ https://stackoverflow.com/questions/48829032/streamreader-readline-throwing-disposed-exception-rather-than-returning-null suggests that the FTP response stream closes itself when all the data is read, which could cause issues with stacked streams?

    Maybe the multiple-block handling loop in GZipInputStream.Read could check stream.CanRead before trying to read a second block?

  3. koide commented on Aug 26, 2019

    @koide
    Author

    Yup, looks like it's not the server but FtpWebRequest's FtpDataStream.

    I can confirm that doing

    while (toRead > 0 && inputStream.CanRead)

    instead of

    makes it work in my case as well, and looks like a saner approach.

  4. added a commit that references this issue on Sep 15, 2019
  5. added a commit that references this issue on Feb 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugcore streamgziptar

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions