Repository navigation
File download broken for chunked responses from github with Python 3.x - #140
kartikmohta wants to merge 1 commit into
Conversation
…te to file Instead of using `resp.fp`, directly use the response object to write data to file. Using `resp.fp` adds some extra data to the written file in Python 3 which is obviously undesired.
|
Thanks for debugging so far. It would be good to know what happens exactly and why our tests do not catch it. Function urlopen_netrc() first tries urlopen, and if that fails with 401, it tries a netrc call. Is it possible that in your case it's an authenticated call that's being performed? |
|
It's definitely using the import urllib.request
import tempfile
import os
import shutil
resp = urllib.request.urlopen('https://github.com/ros-gbp/qt_gui_core-release/archive/release/melodic/qt_gui/0.3.11-0.tar.gz')
fdesc, fname = tempfile.mkstemp()
print(fname)
fhand = os.fdopen(fdesc, "wb")
shutil.copyfileobj(resp.fp, fhand)This saves a file which is not a proper gzip compressed file. |
|
Can you turn that into a unit test that fails for the current version and passes for your fix? |
|
I looked into this a bit more and it looks like the failure happens when receiving a http response with Using the following script: import urllib.request
import tempfile
import os
import shutil
import tarfile
resp = urllib.request.urlopen('https://github.com/ros-gbp/qt_gui_core-release/archive/release/melodic/qt_dotgraph/0.3.11-0.tar.gz')
print(resp.headers)
fdesc, fname = tempfile.mkstemp()
print(fname)
fhand = os.fdopen(fdesc, "wb")
shutil.copyfileobj(resp.fp, fhand)
fhand.close()
temp_tarfile = tarfile.open(fname, 'r:*')with different URLs resulted in different results (note the difference in the first line of the response headers): $ python test.py
Content-Length: 54942
Access-Control-Allow-Origin: https://render.githubusercontent.com
Content-Security-Policy: default-src 'none'; style-src 'unsafe-inline'; sandbox
Strict-Transport-Security: max-age=31536000
Vary: Authorization,Accept-Encoding
X-Content-Type-Options: nosniff
X-Frame-Options: deny
X-XSS-Protection: 1; mode=block
ETag: "a0f9f8d7fe2bbfacb18f0c6eadd43b36349339e2"
Content-Type: application/x-gzip
Content-Disposition: attachment; filename=qt_gui_core-release-release-melodic-qt_gui-0.3.11-0.tar.gz
X-Geo-Block-List:
Date: Sun, 09 Sep 2018 21:09:32 GMT
X-GitHub-Request-Id: DE02:1705:3342BE:58B50F:5B958C0B
Connection: close
/tmp/tmpuoo52hwj
$ python test.py
Transfer-Encoding: chunked
Access-Control-Allow-Origin: https://render.githubusercontent.com
Content-Security-Policy: default-src 'none'; style-src 'unsafe-inline'; sandbox
Strict-Transport-Security: max-age=31536000
Vary: Authorization,Accept-Encoding
X-Content-Type-Options: nosniff
X-Frame-Options: deny
X-XSS-Protection: 1; mode=block
ETag: "4016706d2826c5cf8594d02b9009b75b916e4d28"
Content-Type: application/x-gzip
Content-Disposition: attachment; filename=qt_gui_core-release-release-melodic-qt_dotgraph-0.3.11-0.tar.gz
X-Geo-Block-List:
Date: Sun, 09 Sep 2018 21:12:37 GMT
X-GitHub-Request-Id: DE28:1706:604C24:A40073:5B958CC5
Connection: close
/tmp/tmp9g8l0f_7
Traceback (most recent call last):
File "test.py", line 18, in <module>
temp_tarfile = tarfile.open(fname, 'r:*')
File "/usr/lib/python3.7/tarfile.py", line 1576, in open
raise ReadError("file could not be opened successfully")
tarfile.ReadError: file could not be opened successfullyThe error went away after changing As for a new unittest, I changed the URL in the Note that, officially, using the |
|
Thanks for finding that cause. I still don't know or remember why Ideally we would write our own stubbed http endpoint for a unit test (like https://realpython.com/testing-third-party-apis-with-mock-servers/), but that might be too much effort to ask of you. Also if this is a reproducable bug in urllib, we should report it anyway to the python community. |
|
As far as I see it, the I'm fairly confident that the patch won't break anything, unless someone was working around this bug in some way, since the old behavior is used in the case when the Transfer-Encoding is not chunked, and the chunked case is handled properly now. |
|
I don't get how you can be sure that this is not a bug in urllib. If response.fp behaved in one way in previous versions and in a different way in new versions (even if undocumented), this might be an unintended regression hurting other users as well, no? In any case, @tfoote or @wjwwood should judge whether to merge this. I believe this does need fixing, but I believe there is a small risk of the current patch hurting a few users if our assumptions are wrong about different download situations, in which case an urgent second patch might be required. |
|
Maybe you can edit your initial post (at the top) giving the details you found out since, it will help others get the problem more quickly. |
I believe it never supported chunked transfers, so it was already broken. Maybe github changed something on their end which triggers this problem now. Also, you can't expect stable behavior when using undocumented (internal) fields of a class right?
Yes, that's a good idea. I added a line pointing to the comment with more details. |
|
I confirm that the given script also fails with Python 3.2 to 3.5, so this is not a python regression, but more likely something that never worked. Since users depend on downloading from github, this looks pretty urgent to me. @kartikmohta : I created a branch that reproduces the problem (and solution) reliably using a mock-server: https://github.com/tkruse/vcstools/tree/mock_server_tar_test
@wjwwood : Does OSRF share the opinion that this needs urgent fixing? |
|
Actually, the old variant using |
|
create #141 with enough tests to merge with confidence |
|
Closing this in favor of #141 |
Environment: Python 3.7.0 on ArchLinux.
When using wstool to update my workspace, I received the error:
I checked the temporary tar file downloaded and it was not a proper tar archive, there were some extra bytes at the start and end of the file compared to the same file manually downloaded. It is odd that this started happening recently, it used to work fine a few weeks ago.
The documentation for both Python 2 and 3 mentions that the response object from
urlopenis a file-like object, so I believe we can directly use it in theshutil.copyfileobjfunction.Edit: See the following comment #140 (comment) for more details about the issue.