Skip to content

Fix: cache not cleaned up if download fails #7663

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 17, 2024

Conversation

pwallrich
Copy link
Contributor

@pwallrich pwallrich commented Jun 13, 2024

If a server returns an error when trying to download a binaryTarget an invalid file remains in the artifacts cache.

Motivation:

If the download of a binaryTarget fails a cache file still remains in the artifact cache.

The file only contains the server's response which is usually the status code and the error message.
Example:

$ ls ~/Library/Caches/org.swift.swiftpm/artifacts
.
..
https___test_example_local_foo_zip

$ cat ~/Library/Caches/org.swift.swiftpm/artifacts/https___test_example_local_foo_zip
404 page not found

Worse, all following resolve calls therefore fail because the cached file is used but is an invalid archive.

Modifications:

If the download fails the cache file in the artifacts cache is deleted.

Result:

As expected, the artifacts folder is empty.

Example:

$ ls ~/Library/Caches/org.swift.swiftpm/artifacts
.
..

If the download of a binaryTarget fails a cache file still remains in
the artifact cache.

The file only contains the server's response which is usually the status
code and the error message.

All following resolve calls therefore fail because the cached file is
used but is an invalid archive
@pwallrich pwallrich changed the title Fix cache not cleaned up if download fails Fix: cache not cleaned up if download fails Jun 13, 2024
@@ -7471,6 +7471,7 @@ final class WorkspaceTests: XCTestCase {
// make sure artifact downloaded is deleted
XCTAssertTrue(fs.isDirectory(AbsolutePath("/tmp/ws/.build/artifacts/root")))
XCTAssertFalse(fs.exists(AbsolutePath("/tmp/ws/.build/artifacts/root/a.zip")))
XCTAssertFalse(fs.exists(AbsolutePath("/home/user/caches/org.swift.swiftpm/artifacts/https___a_com_a_zip")))
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this the actual path where caches were previously stored? Can we compute it somehow from previously defined paths instead of hardcoding here?

Copy link
Contributor

Choose a reason for hiding this comment

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

My concern here is that if cache storage somehow changes, this test won't detect that and will continue passing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've updated the test to get the url from the location of the workspace which is also used when initialising the BinaryArtifactsManager in the production code.
The cacheKey is now created the same way as in the BinaryArtifactsManager.

The test could still lead to false positives, if the underlying logic is changed. But I guess to prevent this the BinaryArtifactsManager would need to be refactored which I don't feel comfortable doing at the moment ^^.

Copy link
Contributor

@MaxDesiatov MaxDesiatov left a comment

Choose a reason for hiding this comment

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

Thank you 👍

@MaxDesiatov
Copy link
Contributor

@swift-ci test

@MaxDesiatov MaxDesiatov merged commit 4b9f96f into swiftlang:main Jun 17, 2024
5 checks passed
@MaxDesiatov
Copy link
Contributor

Definitely worth cherry-picking this one for 6.0 👍

@MaxDesiatov
Copy link
Contributor

Cherry-picked as #7671

MaxDesiatov added a commit that referenced this pull request Jun 19, 2024
Cherry-pick of #7663.

**Explanation**: If a server returns an error when trying to download a
binaryTarget an invalid file remains in the artifacts cache.
**Scope**: Isolated to binary targets cache.
**Risk**: Low, scope is isolated, and the change has a corresponding
test.
**Testing**: Automated with an existing test case refined.
**Issue**: rdar://123897276
**Reviewer**: @MaxDesiatov

---------

Co-authored-by: Philipp Wallrich <p.wallrich@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants