Skip to content

Commit

Permalink
Remove lock from CancelContext
Browse files Browse the repository at this point in the history
  • Loading branch information
zanieb committed Feb 24, 2023
1 parent dd46a28 commit 4be1325
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions src/prefect/_internal/concurrency/timeouts.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,17 @@ class CancelContext:
def __init__(self, timeout: Optional[float]) -> None:
self._timeout = timeout
self._cancelled: bool = False
self._lock = threading.Lock()

@property
def timeout(self) -> Optional[float]:
return self._timeout

@property
def cancelled(self):
with self._lock:
return self._cancelled
return self._cancelled

@cancelled.setter
def cancelled(self, value: bool):
with self._lock:
self._cancelled = value
def mark_cancelled(self):
self._mark_cancelled()


@contextlib.contextmanager
Expand Down Expand Up @@ -194,7 +190,7 @@ def _alarm_based_timeout(timeout: float):
ctx = CancelContext(timeout=timeout)

def raise_alarm_as_timeout(signum, frame):
ctx.cancelled = True
ctx.mark_cancelled()
logger.debug(
"Cancel fired for alarm based timeout of thread %r", current_thread.name
)
Expand Down Expand Up @@ -229,7 +225,7 @@ def timeout_enforcer():
"Cancel fired for watcher based timeout of thread %r",
supervised_thread.name,
)
ctx.cancelled = True
ctx.mark_cancelled()
_send_exception_to_thread(supervised_thread, TimeoutError)

enforcer = threading.Thread(target=timeout_enforcer, daemon=True)
Expand Down

0 comments on commit 4be1325

Please sign in to comment.