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 flock for the feed lock file instead of just looking for existence. #257

Merged
merged 7 commits into from
May 15, 2020
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Don't send host details and log messages to the client when Boreas is enabled. [#252](https://github.com/greenbone/ospd-openvas/pull/252)
- Progress bar calculation do not takes in accound dead hosts. [#252](https://github.com/greenbone/ospd-openvas/pull/252)
- Host progress is stored as integer. [#256](https://github.com/greenbone/ospd-openvas/pull/256)
- Use flock for the feed lock file. [#257](https://github.com/greenbone/ospd-openvas/pull/257)

### Fixed
- Check vt_aux for None before trying to access it. [#177](https://github.com/greenbone/ospd-openvas/pull/177)
Expand Down
56 changes: 38 additions & 18 deletions ospd_openvas/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import logging
import time
import fcntl

from pathlib import Path

Expand All @@ -29,56 +30,75 @@ class LockFile:
def __init__(self, path: Path):
self._lock_file_path = path
self._has_lock = False

def is_locked(self) -> bool:
return self._lock_file_path.exists()
self._fd = None

def has_lock(self) -> bool:
return self._has_lock

def acquire_lock(self) -> "LockFile":
def _acquire_lock(self) -> "LockFile":
""" Acquite a lock by creating a lock file.
"""
if self.has_lock() or self.is_locked():
if self.has_lock():
return self

try:
# create parent directories recursively
parent_dir = self._lock_file_path.parent
parent_dir.mkdir(parents=True, exist_ok=True)

self._lock_file_path.touch(exist_ok=False)
self._has_lock = True

logger.debug("Created lock file %s.", str(self._lock_file_path))

except FileExistsError as e:
self._fd = self._lock_file_path.open('w')
except Exception as e: # pylint: disable=broad-except
logger.error(
"Failed to create lock file %s. %s",
str(self._lock_file_path),
e,
)
try:
self._fd.close()
self._fd = None
except Exception: # pylint: disable=broad-except
pass
return self

# Try to adquire the lock.
try:
fcntl.flock(self._fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
self._has_lock = True
logger.debug("Created lock file %s.", str(self._lock_file_path))
except BlockingIOError as e:
logger.error(
"Failed to lock the file %s. %s", str(self._lock_file_path), e,
)
try:
self._fd.close()
self._fd = None
except Exception: # pylint: disable=broad-except
pass

return self

def wait_for_lock(self):
while not self.has_lock():
self.acquire_lock()
self._acquire_lock()
time.sleep(10)

return self

def release_lock(self) -> None:
def _release_lock(self) -> None:
""" Release the lock by deleting the lock file
"""
if self.has_lock() and self.is_locked():
self._lock_file_path.unlink()
if self.has_lock() and self._fd:
fcntl.flock(self._fd, fcntl.LOCK_UN)
self._fd.close()
self._fd = None
self._has_lock = False
logger.debug("Removed lock file %s.", str(self._lock_file_path))
logger.debug(
"Removed lock from file %s.", str(self._lock_file_path)
)

def __enter__(self):
self.acquire_lock()
self._acquire_lock()
return self

def __exit__(self, exc_type, exc_value, exc_tb):
self.release_lock()
self._release_lock()
Loading