Sanic: Streaming Response Chunked Encoding Incomplete Read Error

Created on 4 Apr 2017  ·  4Comments  ·  Source: sanic-org/sanic

The new stream method works with the demo example, but if I try to stream larger chunks of data (e.g. 10 bytes) I get a ChunkedEncodingError / broken connection / Incomplete Read on the client.

I confirmed this on both latest master 62ebcba64 and when the feature was introduced at 19592e8eea.

Here is a slightly modified test demo server to reproduce the problem:

from sanic import Sanic
from sanic.response import stream

app = Sanic(__name__)

@app.route("/")
async def test(request):
    async def sample_streaming_fn(response):
        response.write('foo,bat,baz,')
        response.write('bar')

    return stream(sample_streaming_fn, content_type='text/csv')

app.run(host="0.0.0.0", port=8000, workers=1)

Start this test server with:

$ docker run --rm -it -v `pwd`:/app -p 8000:8000 ubergarm/sanic-alpine /app/test.py
2017-04-04 17:27:49,501: INFO: Goin' Fast @ http://0.0.0.0:8000
2017-04-04 17:27:49,503: INFO: Starting worker [1]

Test with curl:

$ curl -v localhost:8000
* Rebuilt URL to: localhost:8000/
*   Trying ::1...
* Connected to localhost (::1) port 8000 (#0)
> GET / HTTP/1.1
> Host: localhost:8000
> User-Agent: curl/7.47.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Keep-Alive: 60
< Transfer-Encoding: chunked
< Content-Type: text/csv
<
foo,bat,baz,
3
* Malformed encoding found in chunked-encoding
* Closing connection 0
curl: (56) Malformed encoding found in chunked-encoding

Test with httpie:

$ http -v localhost:8000
GET / HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Host: localhost:8000
User-Agent: HTTPie/0.9.4



HTTP/1.1 200 OK
Content-Type: text/csv
Keep-Alive: 60
Transfer-Encoding: chunked


http: error: ChunkedEncodingError: ('Connection broken: IncompleteRead(2 bytes read)', IncompleteRead(2 bytes read))
bug needs investigation

Most helpful comment

The solution is convert bytes size to hex.

All 4 comments

I have the same problem...

The problem is that 10 is a two-digit number and 9 is a single digit

The solution is convert bytes size to hex.

Thanks @qwIvan and all! Confirmed the patch fixes the issue both for the demo code as well as allowing my ffmpeg based webm transcoder to stream "realtime" with reasonable chunk sizes e.g. 1024 bytes.

Cheers!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

davidtgq picture davidtgq  ·  3Comments

litelife picture litelife  ·  3Comments

misakar picture misakar  ·  4Comments

aiurlano picture aiurlano  ·  4Comments

1067511899 picture 1067511899  ·  3Comments