Skip to content

Conversation

@mrnugget
Copy link
Contributor

@mrnugget mrnugget commented Nov 18, 2020

This fixes https://github.com/sourcegraph/sourcegraph/issues/15865

This fixes a bug where src-cli would leave partially-downloaded ZIP
files behind when the user hit Ctrl-C while a download was ongoing.

The fetchRepositoryArchive would then run into a context-cancelation
error when doing the request or the io.Copy and not clean up the file.

The code here checks whether the cause of the error is a cancelation
and, if so, cleans up the zip file.

This fixes a bug where src-cli would leave partially-downloaded ZIP
files behind when the user hit Ctrl-C while a download was ongoing.

The `fetchRepositoryArchive` would then run into a context-cancelation
error when doing the request or the `io.Copy` and not clean up the file.

The code here checks whether the cause of the error is a cancelation
and, if so, cleans up the zip file.
@mrnugget mrnugget requested review from a team, efritz and keegancsmith November 18, 2020 15:23
Copy link
Member

@eseliger eseliger left a comment

Choose a reason for hiding this comment

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

Nice test 🤓

Copy link
Member

@keegancsmith keegancsmith left a comment

Choose a reason for hiding this comment

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

LGTM. However, this is relying on your program not being sigkilled so cleanup can run. To make downloading more robust I'd recommend downloading to filepath.Join(dst, ".tmp") (or something) and renaming once download is complete. This technically still won't be "safe", since you haven't fflushed, but that is probably fine. You can use something like the renameio pkg to handle doing this safely.

@LawnGnome
Copy link
Contributor

To make downloading more robust I'd recommend downloading to filepath.Join(dst, ".tmp") (or something) and renaming once download is complete.

I think I agree with this too, with the caveat that I'd still keep the logic to clean up wherever possible, except applied to that temp file.

You can use something like the renameio pkg to handle doing this safely.

Unfortunately, renameio doesn't work on Windows. shakes fist

@mrnugget
Copy link
Contributor Author

Regarding download-to-temp-file and rename: I don't think this is necessary, since I just changed the code to delete the file in case of any error and nothing else will access the file while that method is running. It doesn't need to be an atomic operation. I'm going to go ahead and merge this, but if someone can tell me what I'm missing and why this needs to be an atomic download-and-swap I'll try to change it.

@mrnugget mrnugget merged commit 24badc8 into main Nov 19, 2020
@mrnugget mrnugget deleted the mrn/clean-up-partial-zips branch November 19, 2020 08:31
@LawnGnome
Copy link
Contributor

I'm going to go ahead and merge this, but if someone can tell me what I'm missing and why this needs to be an atomic download-and-swap I'll try to change it.

I think the concern here is the one Keegan called out: if src receives a SIGKILL, then the cleanup code never gets called, since you can't handle that signal.

@mrnugget
Copy link
Contributor Author

Ah, thanks! Now I get it. I'll think about that a bit more, because we also don't want to leave tmp files lying around and probably want to clean those up when starting. Implementing that could go hand-in-hand with my thoughts about making the archive-caching a bit more efficient (right now, if turned on, we'd cache all archives, which means you can accumulate a large number if your default branch updates often)

scjohns pushed a commit that referenced this pull request Apr 24, 2023
* Clean up partially-downloaded ZIP files if aborted

This fixes a bug where src-cli would leave partially-downloaded ZIP
files behind when the user hit Ctrl-C while a download was ongoing.

The `fetchRepositoryArchive` would then run into a context-cancelation
error when doing the request or the `io.Copy` and not clean up the file.

The code here checks whether the cause of the error is a cancelation
and, if so, cleans up the zip file.

* Add changelog entry

* Clean up partial files on any error
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.

src-cli: Clean up partially downloaded ZIP files if user aborts with Ctrl-C

6 participants