Skip to content
Closed
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
30 changes: 15 additions & 15 deletions distributed/locket.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

from __future__ import annotations

import os
import threading
import warnings
import weakref
from time import sleep

Expand Down Expand Up @@ -154,26 +156,24 @@ def release(self):
class _LockFile:
def __init__(self, path):
self._path = path
self._file = None
self._thread_lock = threading.Lock()

def acquire(self, timeout=None, retry_period=None):
if self._file is None:
self._file = open(self._path, "wb")
if timeout is None and _lock_file_blocking_available:
_lock_file_blocking(self._file)
else:
_acquire_non_blocking(
acquire=lambda: _lock_file_non_blocking(self._file),
timeout=timeout,
retry_period=retry_period,
path=self._path,
)
with open(self._path, "wb") as fd:
if timeout is None and _lock_file_blocking_available:
_lock_file_blocking(fd)
else:
_acquire_non_blocking(
acquire=lambda: _lock_file_non_blocking(fd),
timeout=timeout,
retry_period=retry_period,
path=self._path,
)

def release(self):
_unlock_file(self._file)
self._file.close()
self._file = None
if os.path.exists(self._path):
with open(self._path, "wb") as fd:
_unlock_file(fd)
Comment on lines +174 to +176

@zanieb zanieb Apr 13, 2022 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems less than ideal, but if someone has already purged the worker directory this lock file is gone and we'll throw an exception here without checking if the path exists.

I originally included a warning but it is reraised as an error during Dask's test run and we'd have to ignore it or avoid calling release after the work space has been deleted.

Suggested change
if os.path.exists(self._path):
with open(self._path, "wb") as fd:
_unlock_file(fd)
if os.path.exists(self._path):
with open(self._path, "wb") as fd:
_unlock_file(fd)
else:
warnings.warn(f"Lock at {self._path!r} no longer exists and cannot be released.", stacklevel=2)



class _Locker:
Expand Down