Skip to content

fix: clean up cancelled URL downloads - #5

Merged
programmersd21 merged 3 commits into
mainfrom
fix/cleanup-cancelled-url-download
Nov 28, 2026
Merged

programmersd21 merged 3 commits into
mainfrom
fix/cleanup-cancelled-url-download

Conversation

@programmersd21

@programmersd21 programmersd21 commented Nov 28, 2026 •

Copy link
Copy Markdown
Owner

summary

  • remove the direct-download partial file when cancellation interrupts the download
  • pass the fallback output path as an OS string instead of replacing non-UTF-8 paths with a relative filename

validation

  • reviewed cancellation and fallback path handling
  • CI will run formatting, Clippy, tests, MSRV, and release builds

Summary by Sourcery

Ensure cancelled URL downloads remove temporary artifacts and reliably support arbitrary filesystem paths.

Bug Fixes:

  • Clean up partial direct-download files when URL downloads are cancelled.
  • Preserve non-UTF-8 fallback output paths when invoking the downloader.

Enhancements:

  • Improve temporary download filename uniqueness across closely timed or concurrent runs.

@sourcery-ai

sourcery-ai Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

Implementation

Make generated download filenames unique across concurrent runs by combining a timestamp with an atomic sequence.

  • src/fetch.rs L227-234 (+3 small edits)

Remove partial download files when cancellation interrupts either the direct download or fallback process.

  • src/fetch.rs L153-158, L193

Pass the fallback output path as an OS-native argument so non-UTF-8 paths are preserved.

  • src/fetch.rs L181-182

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@coderabbitai

coderabbitai Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

Important

  • Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 36eebe77-03f4-4327-8ef6-05f7005221b2
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 3 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="src/fetch.rs" line_range="154" />
<code_context>
         result = downloader.download_audio_stream(&video, &name) => result,
-        _ = cancelled(cancel) => return Err("cancelled".into()),
+        _ = cancelled(cancel) => {
+            let _ = std::fs::remove_file(&direct);
+            return Err("cancelled".into());
+        }
</code_context>
<issue_to_address>
**Active downloads lose their output**

When two fetches in the same process generate the same direct filename in the same millisecond and one is cancelled, `remove_file(&direct)` unlinks the shared path while the other downloader is writing it, so the active fetch fails or returns a missing or damaged audio file.

Give each fetch a unique direct output path and ensure cancellation only removes the path owned by that fetch.
</issue_to_address>

### Comment 2
<location path="src/fetch.rs" line_range="154" />
<code_context>
         result = downloader.download_audio_stream(&video, &name) => result,
-        _ = cancelled(cancel) => return Err("cancelled".into()),
+        _ = cancelled(cancel) => {
+            let _ = std::fs::remove_file(&direct);
+            return Err("cancelled".into());
+        }
</code_context>
<issue_to_address>
**Cancelled partials remain on Windows**

When cancellation wins while the direct download has the output file open on Windows, `remove_file` runs inside the cancellation branch while the losing download future can still hold the output file open. Windows rejects removing an open file, and the ignored error leaves the partial download behind after the future is dropped.

Have the select return a cancellation outcome, then remove `direct` after the select has completed and dropped the download future.
</issue_to_address>

### Comment 3
<location path="src/fetch.rs" line_range="154" />
<code_context>
         result = downloader.download_audio_stream(&video, &name) => result,
-        _ = cancelled(cancel) => return Err("cancelled".into()),
+        _ = cancelled(cancel) => {
+            let _ = std::fs::remove_file(&direct);
+            return Err("cancelled".into());
+        }
</code_context>
<issue_to_address>
**Fallback cancellation leaves partial audio**

When the direct downloader leaves a partial file before returning an error and cancellation occurs while yt-dlp is running, the direct download can return an error after leaving a partial file, then the fallback cancellation branch removes only `fallback` and returns. The abandoned direct partial remains in the audio directory.

Remove `direct` in the fallback cancellation branch before returning.
</issue_to_address>

Sourcery assessment

Needs a human reviewer. 3 findings to address first, and if cancellation wins while the downloader is finishing, the new cleanup can remove the completed file at the expected output path, and reverting would not restore it. The loss is limited to that download and can usually be repaired by downloading it again.

Blocking findings: src/fetch.rs:154, src/fetch.rs:154, src/fetch.rs:154


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread src/fetch.rs Resolved
Comment thread src/fetch.rs Resolved
Comment thread src/fetch.rs Resolved

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sourcery assessment

Approved.

@programmersd21
programmersd21 merged commit f1147eb into main Nov 28, 2026
5 checks passed
@programmersd21
programmersd21 deleted the fix/cleanup-cancelled-url-download branch November 28, 2026 03:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant