Skip to content
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

fix: running parallel bundle submits no longer clobbers config file #444

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 8 additions & 17 deletions src/deadline/client/config/config_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
import getpass
import os
import platform
import stat
import subprocess
from configparser import ConfigParser
from pathlib import Path
from typing import Any, Dict, List, Optional
import tempfile

import boto3
from deadline.job_attachments.models import FileConflictResolution
Expand Down Expand Up @@ -268,10 +268,6 @@ def write_config(config: ConfigParser) -> None:
"""
config_file_path = get_config_file_path()
config_file_path.parent.mkdir(parents=True, exist_ok=True)
try:
config_file_path.unlink()
except FileNotFoundError:
pass

if platform.system() == "Windows":
username = getpass.getuser()
Expand All @@ -280,21 +276,16 @@ def write_config(config: ConfigParser) -> None:
# CI - Sub-directories will inherit
# F - Full control
_reset_directory_permissions_windows(config_file_parent_path, username, "(OI)(CI)(F)")
with open(config_file_path, "w", encoding="utf8") as configfile:
config.write(configfile)

else:
flags = os.O_WRONLY | os.O_CREAT | os.O_EXCL
mode = stat.S_IRUSR | stat.S_IWUSR
# Using the config file path as the prefix ensures that the tmpfile and real file are
# on the same filesystem. This is a requirement for os.replace to be atomic.
file_descriptor, tmp_file_name = tempfile.mkstemp(prefix=str(config_file_path), text=True)

original_umask = os.umask(0)
try:
file_descriptor = os.open(config_file_path, flags, mode)
finally:
os.umask(original_umask)
# Note: The file descriptor is closed when exiting the context manager.
with os.fdopen(file_descriptor, "w", encoding="utf8") as configfile:
config.write(configfile)

with os.fdopen(file_descriptor, "w", encoding="utf8") as configfile:
config.write(configfile)
os.replace(tmp_file_name, config_file_path)


def _get_setting_config(setting_name: str) -> dict:
Expand Down
Loading