-
Notifications
You must be signed in to change notification settings - Fork 530
Fix: Prevent race condition in cubin loader when file is being consumed #1852
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -80,7 +80,9 @@ def download_file( | |||||||||||||||||||||||||||||||||
# Handle local file copy | ||||||||||||||||||||||||||||||||||
if os.path.exists(source): | ||||||||||||||||||||||||||||||||||
try: | ||||||||||||||||||||||||||||||||||
shutil.copy(source, local_path) | ||||||||||||||||||||||||||||||||||
temp_path = f"{local_path}.tmp" | ||||||||||||||||||||||||||||||||||
shutil.copy(source, temp_path) | ||||||||||||||||||||||||||||||||||
os.replace(temp_path, local_path) # Atomic rename | ||||||||||||||||||||||||||||||||||
logger.info(f"File copied successfully: {local_path}") | ||||||||||||||||||||||||||||||||||
return True | ||||||||||||||||||||||||||||||||||
except Exception as e: | ||||||||||||||||||||||||||||||||||
|
@@ -93,9 +95,13 @@ def download_file( | |||||||||||||||||||||||||||||||||
response = session.get(source, timeout=timeout) | ||||||||||||||||||||||||||||||||||
response.raise_for_status() | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
with open(local_path, "wb") as file: | ||||||||||||||||||||||||||||||||||
temp_path = f"{local_path}.tmp" | ||||||||||||||||||||||||||||||||||
with open(temp_path, "wb") as file: | ||||||||||||||||||||||||||||||||||
file.write(response.content) | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
# Atomic rename to prevent readers from seeing partial writes | ||||||||||||||||||||||||||||||||||
os.replace(temp_path, local_path) | ||||||||||||||||||||||||||||||||||
Comment on lines
+98
to
+103
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to the local file copy, the URL download logic can leave temporary
Suggested change
|
||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
logger.info( | ||||||||||||||||||||||||||||||||||
f"File downloaded successfully: {source} -> {local_path}" | ||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While using a temporary file and atomic rename is a good solution for the race condition, this implementation can leave behind
.tmp
files if an error occurs during the copy or rename operation. This could lead to disk space exhaustion over time. To make this more robust, I suggest wrapping the file operations in atry...finally
block to ensure the temporary file is always cleaned up.