Skip to content

Commit eff5122

Browse files
committed
🐛 Correct race condition in chunked uploads
In multi-pod environments with a shared file system, chunked uploads were failing due to a race condition. The file size was being read from a cache, which could be stale if another pod had recently written a chunk. This resulted in an incorrect size check, causing parts of the file to be overwritten. This commit fixes the issue by changing how the file size is retrieved. It now opens the file for reading to get the current size directly from the file handle, bypassing any filesystem attribute caching. This ensures an accurate size check and prevents file corruption during chunked uploads.
1 parent d2db511 commit eff5122

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# frozen_string_literal: true
2+
3+
# OVERRIDE Hyrax v5.0.4 to fix a race condition in chunked uploads for multi-pod environments with a shared file system.
4+
# To prevent file corruption from stale cache reads, the file size is now read directly from the file handle to ensure an accurate size check.
5+
module Hyrax
6+
module UploadsControllerDecorator
7+
private
8+
9+
def handle_chunk(content_range, chunk)
10+
file_path = @upload.file.path
11+
12+
current_size = 0
13+
if file_path && File.exist?(file_path)
14+
File.open(file_path, 'r') { |f| current_size = f.size }
15+
end
16+
17+
begin_of_chunk = content_range[/\ (.*?)-/, 1].to_i
18+
19+
if @upload.file.present? && begin_of_chunk == current_size
20+
File.open(file_path, 'ab') { |f| f.write(chunk.read) }
21+
else
22+
@upload.file = chunk
23+
end
24+
end
25+
end
26+
end
27+
28+
Hyrax::UploadsController.prepend(Hyrax::UploadsControllerDecorator)

0 commit comments

Comments
 (0)