GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — chunked encoding + GZIP prevents keep-alive · Issue #367 · googleapis/google-http-java-client

chunked encoding + GZIP prevents keep-alive #367

New issue
Copy link
New issue
Copy link
Closed
#990
#840
Closed
chunked encoding + GZIP prevents keep-alive#367
#990
#840
Copy link
Assignees
chingor13
Labels
🚨This issue needs some love.priority: p2Moderately-important priority. Fix may not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Description

@eddavisson
eddavisson
opened on Sep 1, 2017
Issue body actions

GZIPInputStream.read() starts returning -1 once it finishes reading the gzip trailer. Depending on the underlying InputStream, this may occur before it has finished consuming the underlying InputStream.

In particular, if the underlying InputStream is a ChunkedInputStream, the ChunkedInputStream never reads the final chunk header (which indicates a chunk of length 0).

When ChunkedInputStream.close() is called, it is in a non-STATE_DONE state, and does not call HttpClient.finished(), which is required for the HttpClient object to get into the keep alive cache.

One possible fix is here:
https://github.com/google/google-http-java-client/blob/acdaeacc3c43402d795266d2ebcb584abe918961/google-http-client/src/main/java/com/google/api/client/http/HttpResponse.java#L363

Rather than returning a GZIPInputStream, return a proxy input stream that retains references to both the gzip stream and the underlying stream. The proxy's close() method first consumes the underlying stream by calling read() and then calls close() on the gzip stream.

Activity

  1. eddavisson commented on Sep 7, 2017

    @eddavisson
    eddavisson
    on Sep 7, 2017
    Author
    More actions

    It's possible this only applies for some HttpTransport types. For example, Apache's ChunkedInputStream.close() method indicates that it will consume the rest the stream automatically:
    https://hc.apache.org/httpclient-3.x/apidocs/org/apache/commons/httpclient/ChunkedInputStream.html#close()

  2. eddavisson
    closed this as completedin google-cloud-datastore#181on Oct 10, 2017
  3. eddavisson
    reopened this on Nov 15, 2017
  4. wsh commented on Nov 15, 2017

    @wsh
    wsh
    on Nov 15, 2017
    More actions

    We've worked around this in our code for now, but it's definitely worth fixing in the underlying library. 😄

  5. saicheems
    assigned
    mattwhisenhunt
    on Nov 15, 2017
  6. mattwhisenhunt
    added
    type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.
    priority: p1Important issue which blocks shipping the next release. Will be fixed prior to next release.
    on Jan 8, 2018
  7. JustinBeckwith
    unassigned
    mattwhisenhunt
    on Jun 6, 2018
  8. JustinBeckwith
    added
    priority: p2Moderately-important priority. Fix may not be included in next release.
    🚨This issue needs some love.
    and removed
    priority: p1Important issue which blocks shipping the next release. Will be fixed prior to next release.
    on Jun 6, 2018
  9. ajaaym
    assigned
    pmakani
    on Feb 11, 2019
  10. pmakani
    removed their assignment
    on Mar 19, 2019
  11. chingor13
    self-assigned this
    on Jul 15, 2019
  12. elharo commented on Jul 29, 2019

    @elharo
    elharo
    on Jul 29, 2019
    Contributor
    More actions

    This is really weird. Is the issue that there's a GZIP file/blob in a much larger stream that contains extra data after the end of the gzipped part?

  13. eddavisson commented on Aug 12, 2019

    @eddavisson
    eddavisson
    on Aug 12, 2019
    Author
    More actions

    IIRC, the issue occurs when the GZIPed content is chunked, so there is a GZIPInputStream wrapping a ChunkedInputStream.

    The GZIPInputStream reports that is has been exhausted as soon as it observes the GZIP trailer. However, the ChunkedInputStream is not exhausted until it observes the final chunk header (with length 0).

  14. eddavisson commented on Oct 7, 2019

    @eddavisson
    eddavisson
    on Oct 7, 2019
    Author
    More actions

    Note that the hack we put in place for google-cloud-datastore is going to become increasingly inconvenient in future JDK releases.

  15. codyoss
    added 2 commits that reference this issue on Oct 14, 2019

    feat: wrap GZIPInputStream for connection reuse

    This issue will close once commit d5a0de6 is merged into the 'master' branch.
    d5a0de6

    feat: wrap GZIPInputStream for connection reuse

    This issue will close once commit 756987a is merged into the 'master' branch.
    756987a
  16. codyoss
    closed this as completedin #840on Oct 18, 2019
  17. codyoss
    added a commit that references this issue on Oct 18, 2019

    feat: wrap GZIPInputStream for connection reuse (#840)

    This issue will close once commit 087a428 is merged into the 'main' branch.
    087a428
  18. hiranya911 commented on Mar 5, 2020

    @hiranya911
    hiranya911
    on Mar 5, 2020
    Contributor
    More actions

    I believe the solution implemented in #840 is inadequate. Consider the following test case, which is very similar to the code used in the NetHttpTransport:

      @Test
      public void testConnectionReuse() throws Exception {
        for (int i = 0; i < 3; i++) {
          URL url = new URL("https://jsonplaceholder.typicode.com/todos/1");
          HttpURLConnection conn = (HttpURLConnection) url.openConnection();
          conn.setRequestMethod("GET");
          conn.addRequestProperty("accept-encoding", "gzip");
    
          InputStream original = conn.getInputStream();
          InputStream is = new GZIPInputStream(original);
          byte[] data = ByteStreams.toByteArray(is);
          System.out.println(new String(data));
    
          is.close();
        }
      }
    

    I expect the above code to open only 1 connection due to keep-alive, but it actually opens 3:

    $ sudo tcpdump -i eth0 'tcp[tcpflags] & (tcp-syn) != 0' and dst host typicode.com
    14:34:01.425773 IP my.host.49644 > 104.28.28.194.https: Flags [S], seq 2833089104, win 64240, options [mss 1460,sackOK,TS val 2275669545 ecr 0,nop,wscale 7], length 0
    14:34:01.548967 IP my.host.49646 > 104.28.28.194.https: Flags [S], seq 538857976, win 64240, options [mss 1460,sackOK,TS val 2275669668 ecr 0,nop,wscale 7], length 0
    14:34:01.573442 IP my.host.49648 > 104.28.28.194.https: Flags [S], seq 3217697064, win 64240, options [mss 1460,sackOK,TS val 2275669693 ecr 0,nop,wscale 7], length 0
    

    The solution in #840 simply wraps the GZIPInputStream in a ConsumingInputStream:

    InputStream is = new ConsumingInputStream(new GZIPInputStream(original));
    

    But this won't change the behavior, because ConsumingInputStream only does this:

    @Override
      public void close() throws IOException {
        if (!closed && in != null) {
          try {
            ByteStreams.exhaust(this);
            super.in.close();
          } finally {
            this.closed = true;
          }
        }
      }
    

    Since ByteStreams.exhaust() is simply trying to read any remaining bytes from the GZIPInputStream, it doesn't really help clean up the underlying the ChunkedInputStream.

    To get the desired keep-alive behavior we need to do something like the following in the test case:

    ByteStreams.exhaust(original);
    
    Reacted by Chanseok Oh
  19. codyoss
    added a commit that references this issue on Mar 11, 2020

    fix: Correctly handling chunked response streams with gzip (#990)

    This issue will close once commit 1ba2197 is merged into the 'main' branch.
    1ba2197
  20. gcf-merge-on-green
    added a commit that references this issue on Apr 27, 2020

    chore: release 1.35.0 (#1038)

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

Metadata

Metadata

Assignees

  • @chingor13
    chingor13

Labels

🚨This issue needs some love.priority: p2Moderately-important priority. Fix may not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.