Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataLoss error when content-length doesn't match actual body size #2586

rmax opened this issue Feb 23, 2017 · 5 comments

DataLoss error when content-length doesn't match actual body size #2586

rmax opened this issue Feb 23, 2017 · 5 comments


Copy link

rmax commented Feb 23, 2017

The example website below returns a Content-Length header larger than the actual response size. The webserver itself is broken (or misconfigured) because it returns the Content-Length from the uncompressed body while it sends the compressed body.

$ scrapy shell ""  --set RETRY_ENABLED=0
2017-02-23 00:26:18 [scrapy.utils.log] INFO: Scrapy 1.3.2 started (bot: scrapybot)
2017-02-23 00:26:18 [scrapy.utils.log] INFO: Overridden settings: {'LOGSTATS_INTERVAL': 0, 'RETRY_ENABLED': '0', 'DUPEFILTER_CLASS': 'scrapy.dupefilters.BaseDupeFilter'}
2017-02-23 00:26:18 [scrapy.middleware] INFO: Enabled extensions:
2017-02-23 00:26:19 [scrapy.middleware] INFO: Enabled downloader middlewares:
2017-02-23 00:26:19 [scrapy.middleware] INFO: Enabled spider middlewares:
2017-02-23 00:26:19 [scrapy.middleware] INFO: Enabled item pipelines:
2017-02-23 00:26:19 [scrapy.extensions.telnet] DEBUG: Telnet console listening on
2017-02-23 00:26:19 [scrapy.core.engine] INFO: Spider opened
Traceback (most recent call last):
  File "<snip>/bin/scrapy", line 11, in <module>
    load_entry_point('Scrapy', 'console_scripts', 'scrapy')()
  File "<snip>/scrapy/scrapy/", line 142, in execute
    _run_print_help(parser, _run_command, cmd, args, opts)
  File "<snip>/scrapy/scrapy/", line 88, in _run_print_help
    func(*a, **kw)
  File "<snip>/scrapy/scrapy/", line 149, in _run_command, opts)
  File "<snip>/scrapy/scrapy/commands/", line 73, in run
    shell.start(url=url, redirect=not opts.no_redirect)
  File "<snip>/scrapy/scrapy/", line 48, in start
    self.fetch(url, spider, redirect=redirect)
  File "<snip>/scrapy/scrapy/", line 115, in fetch
    reactor, self._schedule, request, spider)
  File "<snip>/twisted/src/twisted/internet/", line 122, in blockingCallFromThread
  File "<snip>/twisted/src/twisted/python/", line 372, in raiseException
    raise self.value.with_traceback(self.tb)
twisted.web._newclient.ResponseFailed: [<twisted.python.failure.Failure twisted.internet.error.ConnectionDone: Connection was closed cleanly.>, <twisted.python.failure.Failure twisted.web.http._DataLoss: >]
Copy link
Contributor Author

rmax commented Feb 23, 2017

The PR #2585 fixes this issue by adding a new setting DOWNLOADER_ALLOW_DATA_LOSS and customizing the transfer decoders to allow transfer data loss, that is, not raising _DataLoss nor PotentialDataLoss exceptions.

$ scrapy shell ""  --set RETRY_ENABLED=0 --loglevel WARN --set DOWNLOADER_ALLOW_DATA_LOSS=1 -c "len(response.body)"
2017-02-23 00:32:50 [scrapy.core.downloader.handlers.http11] WARNING: Data loss, not all data received

However, the fix requires to apply two patches to twisted ( twisted/twisted#711 and twisted/twisted#712 ) to allow to a clean solution.

Copy link

I had a look at a similar issue with HTTP 3xx in #2493 (comment)
There, I suggested a patch catching _DataLoss and flagging it only, as for PotentialDataLoss.
(So maybe twisted/twisted#711 is not strictly needed.)

twisted/twisted#711 and twisted/twisted#712 look useful nonetheless.

Copy link
Contributor Author

rmax commented Feb 23, 2017

@redapple nice approach without requiring twisted patches. I made a PR #2590 based on your suggestion and it works well, although the tests were a bit tricky.

Copy link
Contributor Author

rmax commented Mar 1, 2017

Fixed by #2590

@rmax rmax closed this as completed Mar 1, 2017
Copy link

lzyADA commented Nov 5, 2018

Hello, scrapy [<twisted.python.failure.Failure twisted.internet.error.ConnectionDone: Connection was closed cleanly.><twisted.python.failure.Failure twisted.web.http._DataLoss: Chunked decoder in 'CHUNK_LENGTH' state , still expecting more data to get to 'FINISHED' state.>
Pull the request

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

No branches or pull requests

3 participants