Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
TASTE
dmt
Commits
3df8826c
Commit
3df8826c
authored
Dec 10, 2020
by
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
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
dmt/commonPy/asnParser.py
View file @
3df8826c
...
...
@@ -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
()):
...
...
dmt/commonPy/lockResource.py
0 → 100644
View file @
3df8826c
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
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment