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

Use context manager for locking clvm files #11467

Merged
merged 1 commit into from
May 10, 2022
Merged
Show file tree
Hide file tree
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
11 changes: 5 additions & 6 deletions chia/util/lock.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import contextlib
import os
import time
from typing import Callable, Optional, TextIO, TypeVar
from typing import Iterator, Optional, TextIO, TypeVar

T = TypeVar("T")

Expand All @@ -22,15 +23,13 @@ def create_exclusive_lock(lockfile: str) -> Optional[TextIO]:
return f


def with_lock(lock_filename: str, run: Callable[[], T]) -> T:
@contextlib.contextmanager
def lock_by_path(lock_filename: str) -> Iterator[None]:
"""
Ensure that this process and this thread is the only one operating on the
resource associated with lock_filename systemwide.

Pass through the result of run after exiting the lock.
"""

lock_file = None
while True:
lock_file = create_exclusive_lock(lock_filename)
if lock_file is not None:
Expand All @@ -39,7 +38,7 @@ def with_lock(lock_filename: str, run: Callable[[], T]) -> T:
time.sleep(0.1)

try:
return run()
yield
finally:
lock_file.close()
os.remove(lock_filename)
6 changes: 2 additions & 4 deletions chia/wallet/puzzles/load_clvm.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import pkg_resources
from chia.types.blockchain_format.program import Program, SerializedProgram
from chia.util.lock import with_lock
from chia.util.lock import lock_by_path
from clvm_tools_rs import compile_clvm as compile_clvm_rust


Expand Down Expand Up @@ -67,11 +67,9 @@ def sha256file(f):


def compile_clvm(full_path, output, search_paths=[]):
def do_compile():
with lock_by_path(f"{full_path}.lock"):
compile_clvm_in_lock(full_path, output, search_paths)

with_lock(f"{full_path}.lock", do_compile)


def load_serialized_clvm(clvm_filename, package_or_requirement=__name__) -> SerializedProgram:
"""
Expand Down