Skip to content

Commit

Permalink
Address Toshio's commments.
Browse files Browse the repository at this point in the history
  • Loading branch information
jochapma committed Mar 6, 2024
1 parent d01a15b commit 431844e
Showing 1 changed file with 16 additions and 19 deletions.
35 changes: 16 additions & 19 deletions convert2rhel/applock.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ class ApplicationLock:
def __init__(self, name):
# Our application name
self._name = name
# Our process ID
# Our process ID. We save this when the lock is created so it will be
# consistent even if we check from inside a fork.
self._pid = os.getpid()
# Path to the file that contains the process id
self._pidfile = os.path.join(_DEFAULT_LOCK_DIR, self._name + ".pid")
Expand Down Expand Up @@ -95,7 +96,7 @@ def _try_create(self):
except OSError as exc:
if exc.errno == errno.EEXIST:
return False
raise exc
raise

Check warning on line 99 in convert2rhel/applock.py

View check run for this annotation

Codecov / codecov/patch

convert2rhel/applock.py#L99

Added line #L99 was not covered by tests
loggerinst.debug("%s." % self)
return True

Expand All @@ -114,23 +115,19 @@ def _read_pidfile(self):
:returns: the file contents as an integer, or None if it doesn't exist
:raises: ApplicationLockedError if the contents are corrupt
"""
if os.path.exists(self._pidfile):
try:
with open(self._pidfile, "r") as f:
file_contents = f.read()
pid = int(file_contents.rstrip())
except OSError as exc:
# This addresses a race condition in which another process
# deletes the lock file between our check that it exists
# and our attempt to read the contents.
# In Python 3 this could be changed to FileNotFoundError.
if exc.errno == errno.ENOENT:
return None
raise exc
except ValueError:
raise ApplicationLockedError("Lock file %s is corrupt" % self._pidfile)
try:
with open(self._pidfile, "r") as f:
file_contents = f.read()
pid = int(file_contents.rstrip())
return pid
return None
except IOError as exc:
# The open() failed because the file exists.
# In Python 3 this could be changed to FileNotFoundError.
if exc.errno == errno.ENOENT:
return None
raise
except ValueError:
raise ApplicationLockedError("Lock file %s is corrupt" % self._pidfile)

@staticmethod
def _pid_exists(pid):
Expand All @@ -157,7 +154,7 @@ def _safe_unlink(self):
# In Python 3 this could be changed to FileNotFoundError.
if exc.errno == errno.ENOENT:
return
raise exc
raise

Check warning on line 157 in convert2rhel/applock.py

View check run for this annotation

Codecov / codecov/patch

convert2rhel/applock.py#L156-L157

Added lines #L156 - L157 were not covered by tests

def try_to_lock(self):
"""Try to get a lock on this application. If this method does
Expand Down

0 comments on commit 431844e

Please sign in to comment.