Skip to content

fix: sweep fragment partials immediately on cancel - #18

Open
Mr-Neutr0n wants to merge 1 commit into
pablostanley:mainfrom
Mr-Neutr0n:fix/cancel-cleanup-fragments
Open

Mr-Neutr0n wants to merge 1 commit into
pablostanley:mainfrom
Mr-Neutr0n:fix/cancel-cleanup-fragments

Conversation

@Mr-Neutr0n

Copy link
Copy Markdown

Problem

Cancelling a download leaked yt-dlp's partial files in the output directory. Two stacked causes:

  1. The artifact list was incomplete. Cleanup removed dest, dest.part, and dest.ytdl — but fragmented downloads write dest.part-Frag0, dest.part-Frag1, … siblings that a plain rm of the .part never touches.
  2. Cleanup waited for the child's close event. spawn(bin, args, {signal}) signals only the direct child; anything it spawned (an ffmpeg merge step, a shell's sleep) inherits the stdio pipes and keeps them open after the parent dies, delaying close by the grandchild's entire runtime. Until it fires, no cleanup happens at all.

Reproduced with a scripted fake yt-dlp: cancel mid-download → .part, .part-Frag0, .part-Frag1 all still on disk a minute later.

Change

  • On abort, sweep partials immediately from what the live stdout parser already collected — destinations are parsed as lines stream, so by cancel time they are known. The close handler re-sweeps for any line that streamed past the abort.
  • The sweeper now enumerates the destination's sibling family: exact name, dot-suffixed (.ytdl, .part*) and dash-suffixed (-Frag<n>), while explicitly not touching similarly-named user files like <dest>-notes.txt.
  • Successful downloads and genuine failures behave exactly as before.

Verification

New src/lib/ytdlp.test.ts drives the real download() against scripted fake binaries writing actual fragment files:

  • cancel sweeps .part + -FragN siblings (fails on old code, passes now)
  • late-announced destinations still get swept
  • full suite: 8 passed (6 existing + 2 new); tsup build clean

This change was prepared with AI assistance under human direction and review.

Cancelled downloads leaked yt-dlp's range fragments (.part-Frag<n>):
the artifact list only knew dest, .part and .ytdl, and cleanup ran
inside the close handler, which a surviving grandchild (ffmpeg mid-
merge, an orphaned sleep) can hold off for as long as it runs.

Sweep the destination's sibling family (exact name, dot-suffixed,
dash-suffixed) the moment abort fires instead of waiting for close,
and re-sweep in close for lines that streamed past the abort. The
filter never touches similarly-named user files.

Signed-off-by: Mr-Neutr0n <64578610+Mr-Neutr0n@users.noreply.github.com>
@Mr-Neutr0n

Copy link
Copy Markdown
Author

Hi @pablostanley - a gentle nudge on sweeping fragment partials on cancel; still mergeable and applies cleanly. Happy to adjust anything, and fine to close if not wanted. Thank you.

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