forked from rubygems/bundler
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Safely store concurrent compact index downloads
When bundler is run concurrently using the same bundle dir in $HOME, the versions file can be updated from two processes at once. The download has been changed to a temporary file, which is securely moved into place over the original. If retrying the update operation, the original file is no longer immediately deleted and instead a full download is performed, later overwriting the original file if successful. If two processes are updating in parallel, this should ensure the original file isn't corrupted and that both processes succeed. - Fixes rubygems#4519
- Loading branch information
Showing
3 changed files
with
81 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
spec/support/artifice/compact_index_concurrent_download.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# frozen_string_literal: true | ||
require File.expand_path("../compact_index", __FILE__) | ||
|
||
Artifice.deactivate | ||
|
||
class CompactIndexConcurrentDownload < CompactIndexAPI | ||
get "/versions" do | ||
versions = File.join(Bundler.rubygems.user_home, ".bundle", "cache", "compact_index", | ||
"localgemserver.test.80.dd34752a738ee965a2a4298dc16db6c5", "versions") | ||
|
||
# Verify the original (empty) content hasn't been deleted, e.g. on a retry | ||
File.read(versions) == '' or fail('Original file should be present and empty') | ||
|
||
# Verify this is only requested once for a partial download | ||
env['HTTP_RANGE'] or fail('Missing Range header for expected partial download') | ||
|
||
# Overwrite the file in parallel, which should be then overwritten | ||
# after a successful download to prevent corruption | ||
File.open(versions, "w") { |f| f.puts "another process" } | ||
|
||
etag_response do | ||
file = tmp("versions.list") | ||
file.delete if file.file? | ||
file = CompactIndex::VersionsFile.new(file.to_s) | ||
file.update_with(gems) | ||
CompactIndex.versions(file, nil, {}) | ||
end | ||
end | ||
end | ||
|
||
Artifice.activate_with(CompactIndexConcurrentDownload) |