Commit 3df8826c authored by Thanassis Tsiodras's avatar Thanassis Tsiodras
Browse files

Introduced context manager for proper (auto-released, including Ctrl-C) locking.

parent b09d8c26
Pipeline #2877 failed with stage
in 35 seconds
......@@ -68,6 +68,8 @@ from .asnAST import (
AsnSet, AsnSetOf, AsnMetaMember, AsnMetaType, AsnInt, AsnReal, AsnNode,
AsnComplexNode, AsnBool, AsnOctetString, AsnAsciiString
)
from .lockResource import lock_filename
g_asnFilename = ""
......@@ -401,15 +403,9 @@ def ParseAsnFileList(listOfFilenames: List[str]) -> None: # pylint: disable=inv
"The configured cache folder:\n\n\t" + projectCache + "\n\n...is not there!\n")
# To avoid race conditions from multiple processes spawning ASN1SCC at the same time,
# enforce mutual exclusion via locking - using an atomic syscall:
while True:
try:
os.mkdir("/tmp/onlyOneAsn1Scc")
break
except:
time.sleep(1)
# enforce mutual exclusion via locking.
with lock_filename('/tmp/onlyOneASN1SCC'):
try:
xmlAST = None
someFilesHaveChanged = False
if projectCache is not None:
......@@ -462,9 +458,6 @@ def ParseAsnFileList(listOfFilenames: List[str]) -> None: # pylint: disable=inv
if projectCache is None:
os.unlink(xmlAST)
finally:
os.rmdir("/tmp/onlyOneAsn1Scc")
def Dump() -> None:
for nodeTypename in sorted(g_names.keys()):
......
import os
import time
import fcntl
from contextlib import contextmanager
@contextmanager
def lock_filename(filename: str, verbose: bool):
# If file is not there, make it, and close it.
# This should only happen once in the universe - lockfiles are not supposed to be removed.
# https://unix.stackexchange.com/questions/368159/why-flock-doesnt-clean-the-lock-file
try:
open(filename, 'w').write("LOCK")
except:
# In case multiple instances try to create the file, only one will succeed.
# No worries, we're good with that
pass
# Now keep trying to lock it with a read-only exclusive lock.
fd = os.open(filename, os.O_RDONLY)
if verbose:
print("[-] Trying to lock:", filename)
while True:
try:
fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
break
except:
if verbose:
print("[x] Already locked. Waiting 1 second...")
time.sleep(1)
if verbose:
print("[-] Got lock!")
yield
fcntl.flock(fd, fcntl.LOCK_UN)
if verbose:
print("[-] Unlocked:", filename)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment