A retried download appends to the partial .transfer file
🙂 Looking for an issue? Welcome! This issue is open for contribution. If this is the first time you’re requesting an issue, please:
- Read Contributing guidelines carefully. Pay extra attention to Using generative AI. Pull requests and comments that don’t follow the guidelines won’t be answered.
- Confirm that you’ve read the guidelines in your comment.
Target branch: develop
Observed behavior
- A
FileDownloadto aTransferFilethat hits a retryable error mid-stream (e.g.ConnectionError) is re-run from byte 0 by_catch_exception_and_retry. The.transferfile handle and MD5 hasher from the first attempt are still open, soTransferFile.writeappends the new stream to the partial data. - With a checksum (content file import), verification fails and the file errors with
TransferFailed. - Without a checksum (channel database download), the corrupt file is moved into place and accepted.
Errors and logs
From the test in Steps to reproduce:
ERROR kolibri.utils.file_transfer:file_transfer.py:866 Error reading download stream:
ERROR kolibri.utils.file_transfer:file_transfer.py:720 An error occurred during content import: File http://example.com/test_file is corrupted.
kolibri.utils.file_transfer.TransferFailed: Transferred file checksums did not match for http://example.com/test_file
Without a checksum, the file on disk is 14336 bytes: the 4096-byte partial stream followed by the full 10240-byte retry.
Expected behavior
- A retried download resumes from the bytes already written, with a range request when the server supports one.
- If the server doesn't support range requests, the retry starts from an empty
.transferfile and a fresh hasher.
User-facing consequences
- On an unstable connection, a content import that retries a file then fails it with
Transferred file checksums did not match, even though the file on the server is intact. The admin has to run the import again. - After a retry, a channel database download leaves a corrupt database in place and reports no error at download time.
Steps to reproduce
Add this class to kolibri/utils/tests/test_file_transfer.py, which already has the imports it needs, then run pytest kolibri/utils/tests/test_file_transfer.py -k RetryAfterPartialStream.
class TestTransferFileRetryAfterPartialStream(unittest.TestCase):
def setUp(self):
self.destdir = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, self.destdir, ignore_errors=True)
self.dest = os.path.join(self.destdir, "test_file")
self.content = os.urandom(1024 * 10)
def _response(self, chunks):
response = MagicMock()
response.headers = {"content-length": str(len(self.content))}
response.iter_content.return_value = chunks
return response
def _partial_stream(self):
yield self.content[:4096]
raise ConnectionError
def _download(self, checksum):
session = MagicMock()
session.get.side_effect = [
self._response(self._partial_stream()),
self._response(iter([self.content])),
]
with FileDownload(
"http://example.com/test_file",
self.dest,
checksum,
session=session,
retry_wait=0,
) as fd:
fd.run()
def test_retry_with_checksum(self):
self._download(hashlib.md5(self.content).hexdigest())
with open(self.dest, "rb") as f:
self.assertEqual(f.read(), self.content)
def test_retry_without_checksum(self):
self._download(None)
with open(self.dest, "rb") as f:
self.assertEqual(f.read(), self.content)
Result on develop: test_retry_with_checksum raises TransferFailed, and test_retry_without_checksum fails on the file contents. The mocked server ignores Range headers, so these tests pass whether the fix resumes or restarts. The existing test_file_download_retry_resume raises ConnectionError from raise_for_status before any bytes are written, so it doesn't cover this case.
Context
Affects v0.18.3 and later. 511d46f121 introduced TransferFile.
Acceptance Criteria
- [ ]
TestTransferFileRetryAfterPartialStreamfrom Steps to reproduce passes. - [ ] For a download with a checksum against a server that honours
Range, a retry after a drop at byte 4096 requestsbytes=4096-, and the file on disk matches the source.
AI usage
Claude Code found this while I was reviewing #15487. It reproduced the bug and drafted this issue. I chose the expected behaviour (resume with a range request, otherwise restart) and the scope.
Hi! 👋
This issue is available for contribution and supports self-assignment. Here's how to get started:
- Comment
/assignto assign yourself to this issue - You can have up to 2 issues assigned at a time across all community repos
- Dropping an issue has a 7-day cooldown before the slot opens up
- Link your pull request to this issue when you submit it
📖 Read the Contributing guidelines before starting.
Are you preparing for Google Summer of Code? See our GSoC guidelines.
/assign
Hi! 👋
You've been assigned to this issue. Here's what to do next:
- Read the issue description carefully and make sure you understand the requirements
- Link your pull request to this issue when you submit it
- If you can no longer work on this, unassign yourself so others can pick it up
Good luck! 😊
Are you preparing for Google Summer of Code? See our GSoC guidelines.