Skip to content

Commit

Permalink
Remove LOCK_NB to let process wait unlock
Browse files Browse the repository at this point in the history
  • Loading branch information
natthan-pigoux committed Nov 4, 2024
1 parent 7c2b8b8 commit 276db27
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 19 deletions.
6 changes: 3 additions & 3 deletions diracx-core/src/diracx/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ def read_credentials(location: Path) -> TokenResponse:
try:
with open(credentials_path, "r") as f:
# Lock the file to prevent other processes from writing to it at the same time
fcntl.flock(f, fcntl.LOCK_SH | fcntl.LOCK_NB)
fcntl.flock(f, fcntl.LOCK_SH)
# Read the credentials from the file
try:
credentials = json.load(f)
finally:
# Release the lock
fcntl.flock(f, fcntl.LOCK_UN)
except (BlockingIOError, FileNotFoundError, json.JSONDecodeError) as e:
except (FileNotFoundError, json.JSONDecodeError) as e:
raise RuntimeError(f"Error reading credentials: {e}") from e

return TokenResponse(
Expand All @@ -74,7 +74,7 @@ def write_credentials(token_response: TokenResponse, *, location: Path | None =

with open(credentials_path, "w") as f:
# Lock the file to prevent other processes from writing to it at the same time
fcntl.flock(f, fcntl.LOCK_EX | fcntl.LOCK_NB)
fcntl.flock(f, fcntl.LOCK_EX)
try:
# Write the credentials to the file
f.write(serialize_credentials(token_response))
Expand Down
20 changes: 4 additions & 16 deletions diracx-core/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def test_dotenv_files_from_environment(monkeypatch):
def lock_and_read_file(file_path):
"""Lock and read file."""
with open(file_path, "r") as f:
fcntl.flock(f, fcntl.LOCK_SH | fcntl.LOCK_NB)
fcntl.flock(f, fcntl.LOCK_SH)
f.read()
time.sleep(2)
fcntl.flock(f, fcntl.LOCK_UN)
Expand All @@ -62,7 +62,7 @@ def lock_and_read_file(file_path):
def lock_and_write_file(file_path: Path):
"""Lock and write file."""
with open(file_path, "a") as f:
fcntl.flock(f, fcntl.LOCK_EX | fcntl.LOCK_NB)
fcntl.flock(f, fcntl.LOCK_EX)
f.write(CREDENTIALS_CONTENT)
time.sleep(2)
fcntl.flock(f, fcntl.LOCK_UN)
Expand Down Expand Up @@ -134,13 +134,7 @@ def test_read_credentials_reading_locked_file(
_, token_location = token_setup
process_to_test = (read_credentials, {"location": token_location})
error_dict = concurrent_access_to_lock_file(process_to_test, read=False)
process_name = process_to_test[0].__name__
if process_name in error_dict.keys():
assert isinstance(error_dict[process_name], RuntimeError)
else:
raise AssertionError(
"Expected a RuntimeError while reading locked credentials."
)
assert not error_dict


def test_write_credentials_writing_locked_file(
Expand All @@ -153,13 +147,7 @@ def test_write_credentials_writing_locked_file(
{"token_response": token_response, "location": token_location},
)
error_dict = concurrent_access_to_lock_file(process_to_test)
process_name = process_to_test[0].__name__
if process_name in error_dict.keys():
assert isinstance(error_dict[process_name], BlockingIOError)
else:
raise AssertionError(
"Expected a BlockingIOError while writing locked credentials."
)
assert not error_dict


def create_temp_file(content=None) -> Path:
Expand Down

0 comments on commit 276db27

Please sign in to comment.