runtime
382 / 500
question 382 Async Python · easy

Fix the download that leaks temp files when cancelled

A downloader registers a temporary file before streaming and removes it at the end. When users cancel a download (the task is cancelled mid-stream), the temp file is never removed, and the disk fills up. Fix download so the temp file is removed however the download ends, while a cancelled download still ends cancelled.

Coroutineasync def download(url, temp_files, fetch_chunk, chunks)
Effectadds url + ".part" to the temp_files list, awaits fetch_chunk(url, i) for each chunk, removes the temp entry, and returns the number of chunks
Rulethe temp entry is removed after success, error and cancellation; cancellation is not swallowed

Loading your progress…

Fix the download that leaks temp files when cancelled — Python practice · Runtime