kolibri icon indicating copy to clipboard operation
kolibri copied to clipboard

FileDownload reports an existing, correct file as corrupted and deletes it

Open Rishika-dev opened this issue 2 months ago • 2 comments

❌ This issue is not open for contribution. Visit Contributing guidelines to learn about the contributing process and how to find suitable issues.

Target branch: develop (also present in release-v0.19.x)

Observed behavior

When FileDownload writes to a plain TransferFile (the non-chunked path used by content import) and the destination file already exists when run() is called, the transfer:

  1. treats the file as complete, because TransferFile.is_complete() returns True whenever the destination exists (kolibri/utils/file_transfer.py:513), so nothing is downloaded,
  2. then verifies the checksum with TransferFile.md5_checksum() (:572), which only hashes the bytes written during this transfer. No bytes were written, so it returns d41d8cd98f00b204e9800998ecf8427e (the MD5 of empty input),
  3. so _verify_checksum() (:713) logs "File … is corrupted", calls TransferFile.delete(), which removes the existing destination file (:568), and raises TransferFailed.

A complete, correct file ends up deleted, and the import fails.

I noticed this while importing the Kolibri QA Channel (95a52b386f2c485cb97dd60901674a98) from Studio on a fresh dev setup. 4 of 7 importcontent runs stopped with "is corrupted", each time on a different file (.perseus, .mp4, .pdf, .vtt). Downloading those same files with curl gave the correct MD5 every time (9/9). With extra logging on one failing file, the transfer had written 0 bytes and its computed checksum was the empty-input MD5 above.

I couldn't work out what put the destination file in place between _start_file_transfer's existence check (resource_import.py:169) and the transfer starting. The last 4 runs from a clean content directory all succeeded. So I'm reporting the part I could reproduce reliably, which is the behaviour when the destination already exists.

Errors and logs

ERROR An error occurred during content import: File https://studio.learningequality.org/content/storage/9/c/9cef3071d36d5b0e4a1c317d56fc8043.perseus is corrupted.
ERROR An error occurred during content import: Transferred file checksums did not match for https://studio.learningequality.org/content/storage/9/c/9cef3071d36d5b0e4a1c317d56fc8043.perseus
CommandError: Transferred file checksums did not match for https://studio.learningequality.org/content/storage/9/c/9cef3071d36d5b0e4a1c317d56fc8043.perseus

Logging added around _verify_checksum for one failure:

TransferFile written=0 file_size=1042050 got_md5=d41d8cd98f00b204e9800998ecf8427e expected=493870c33aa02bdbf466a6926189fb06

Expected behavior

If the destination already holds the file, the transfer either verifies the existing file's real checksum and succeeds, or downloads it again. It should never delete a destination file that this transfer didn't write.

User-facing consequences

Admins see a content import fail with a "corrupted file" error even though both the source and the local copy are fine, and a good resource file is deleted from disk. Resources that were available may then become unavailable until the content is imported again.

Steps to reproduce

Minimal reproduction on develop (no network needed):

import hashlib, os, tempfile
from unittest import mock
from kolibri.utils.file_transfer import FileDownload, TransferFailed

data = b"hello kolibri" * 1000
md5 = hashlib.md5(data).hexdigest()
dest = os.path.join(tempfile.mkdtemp(), md5 + ".txt")
with open(dest, "wb") as f:
    f.write(data)  # a correct, complete file is already in place

session = mock.MagicMock()
try:
    with FileDownload("http://example.invalid/" + md5 + ".txt", dest, checksum=md5, session=session) as t:
        t.run()
    print("OK")
except TransferFailed as e:
    print("TransferFailed:", e)
print("dest still exists:", os.path.exists(dest))

Output:

TransferFailed: Transferred file checksums did not match for http://example.invalid/a53ed77aee91fe5b04bab85508abee0d.txt
dest still exists: False

Context

  • Kolibri develop at 6eda02702d. TransferFile was added in 511d46f121 ("Reverts complete transition to ChunkedFile usage for FileDownload class…"), so this has been present since v0.19.0. I checked that it is also on release-v0.19.x.
  • macOS 26.6.2, Python 3.11.14
  • Import from Kolibri Studio with kolibri manage importcontent network 95a52b386f2c485cb97dd60901674a98

A possible direction, if this is confirmed: when TransferFile treats an existing destination as complete, compute the checksum from that file on disk instead of the empty hasher, and make delete() remove only the temporary .transfer file it created. I'm happy to work on a fix if this is triaged.

AI usage

I used Claude Code to help investigate the failing imports. It added temporary logging around the transfer code, compared the results with curl downloads, traced the code path, and helped draft this issue. I ran the reproduction script and the imports myself, checked the code references and output above against develop, and edited the text.

Rishika-dev avatar Oct 01 '26 08:10 Rishika-dev

Thanks, confirmed on develop. A likely trigger: _start_file_transfer only skips existing files whose size matches file_size from the channel DB. On a size mismatch it falls through to the download, which then finds the destination already there. That would explain why it only fails on re-runs over an existing content dir. Assigning to you. Please include a test with your repro script.

rtibbles avatar Oct 03 '26 02:10 rtibbles

Opened a fix in https://github.com/learningequality/kolibri/pull/15487, with tests based on the repro script.

Rishika-dev avatar Oct 03 '26 14:10 Rishika-dev