question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Chunked responses lack data

See original GitHub issue

Using urllib3 1.10.3 in python-etcd client.

We were hitting a really weird bug when occasonally we’d get

Traceback (most recent call last):
  File "etcd/lib_etcd.py", line 116, in _result_from_response
    res = json.loads(response.data.decode('utf-8'))
  File "~/soft/infra-virtenv/local/lib/python2.7/site-packages/urllib3/response.py", line 166, in data
    return self.read(cache_content=True)
  File "~/infra-virtenv/local/lib/python2.7/site-packages/urllib3/response.py", line 239, in read
    data = self._fp.read()
  File "/usr/lib/python2.7/httplib.py", line 549, in read
    return self._read_chunked(amt)
  File "/usr/lib/python2.7/httplib.py", line 622, in _read_chunked
    self._safe_read(2)      # toss the CRLF at the end of the chunk
  File "/usr/lib/python2.7/httplib.py", line 664, in _safe_read
    chunk = self.fp.read(min(amt, MAXAMOUNT))
AttributeError: 'NoneType' object has no attribute 'read'

on a chunked response from Etcd.

It seems to be related to the fact that HttpConectionPool#urlopen by default release_conn=True, which is dictated by preload_content

~/soft/infra-virtenv/lib/python2.7/site-packages/urllib3/connectionpool.py:510
        if release_conn is None:
            release_conn = response_kw.get('preload_content', True)

which means that if it is a chunked response, it will be closed immediately without the requesting library being able to read the content, since

~/soft/infra-virtenv/lib/python2.7/site-packages/urllib3/response.py:135
        # We certainly don't want to preload content when the response is chunked.
        if not self.chunked and preload_content and not self._body:
            self._body = self.read(decode_content=decode_content)

This is fine, as long as the connection doesn’t get reused, but under heavy load it causes things to break really badly.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:17 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
Lukasacommented, May 18, 2016

RFC 2616 is not the specification for HTTP/1.1, RFC 7230 is. RFC 7230 says you cannot send both:

A sender MUST NOT send a Content-Length header field in any message that contains a Transfer-Encoding header field.

It’s good to know that removing it causes the same problem though. Let me see if I can adequately reproduce it locally.

0reactions
Lukasacommented, May 18, 2016

The dummy server should handle multiple concurrent connections, being written in Tornado. As far as I know it doesn’t require any kind of synchronous execution.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What Is HTTP Chunked Encoding? How Is It Used? - Bunny.net
HTTP Chunked Encoding involves dividing data into smaller blocks. Breaking up data into smaller pieces allows for near-instant load times.
Read more >
how to handle a chunked response from a server?
1 Answer 1 ... You can't use strlen to get the size of your buffer, strlen is only for getting the size of...
Read more >
How HTTP Chunked Encoding was killing a request
Transfer -Encoding: chunked - there is no content length specified, the server tells us it will send a bunch of chunks whenever it...
Read more >
HTTP Chunking - Oracle
Chunking is most often used by the server for responses, but clients can also chunk large requests. HTTP 1.1 supports persistent connections by...
Read more >
Chunked transfer encoding - Wikipedia
Chunked transfer encoding is a streaming data transfer mechanism available in Hypertext Transfer Protocol (HTTP) version 1.1, defined in RFC 9112 §7.1.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found