GeezerVison is showing github.com — chunked encoding + GZIP prevents keep-alive · Issue #367 · googleapis/google-http-java-client
chunked encoding + GZIP prevents keep-alive #367Labels 🚨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. DescriptionGZIPInputStream.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. Activityeddavisson commented on Sep 7, 2017 wsh commented on Nov 15, 2017 We've worked around this in our code for now, but it's definitely worth fixing in the underlying library. 😄 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, 2018added 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, 2018elharo commented on Jul 29, 2019 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? eddavisson commented on Aug 12, 2019 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). eddavisson commented on Oct 7, 2019 added 2 commits that reference this issue on Oct 14, 2019This issue will close once commit d5a0de6 is merged into the 'master' branch.d5a0de6 This issue will close once commit 756987a is merged into the 'master' branch.756987a added a commit that references this issue on Oct 18, 2019This issue will close once commit 087a428 is merged into the 'main' branch.087a428 hiranya911 commented on Mar 5, 2020 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);
added a commit that references this issue on Mar 11, 2020This issue will close once commit 1ba2197 is merged into the 'main' branch.1ba2197
MetadataMetadata🚨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. DevelopmentNo branches or pull requestsIssue actions
|