Add support for locking config file (#1652)

* Add support for locking config file

* Remove abi decode test

* Fix typo
pull/1654/head
Nikhil Parasaram 2 years ago committed by GitHub
parent 21999f8b50
commit 5a184e8225
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 25
      mythril/mythril/mythril_config.py
  2. 78
      mythril/support/lock.py
  3. 16
      tests/integration_tests/parallel_test.py

@ -10,6 +10,7 @@ from typing import Optional
from mythril.exceptions import CriticalError
from mythril.ethereum.interface.rpc.client import EthJsonRpc
from mythril.support.lock import LockFile
log = logging.getLogger(__name__)
@ -69,21 +70,23 @@ class MythrilConfig:
config = ConfigParser(allow_no_value=True)
config.optionxform = str
config.read(self.config_path, "utf-8")
if "defaults" not in config.sections():
self._add_default_options(config)
with LockFile(self.config_path):
config.read(self.config_path, "utf-8")
if "defaults" not in config.sections():
self._add_default_options(config)
if not config.has_option("defaults", "dynamic_loading"):
self._add_dynamic_loading_option(config)
if not config.has_option("defaults", "dynamic_loading"):
self._add_dynamic_loading_option(config)
if not config.has_option("defaults", "infura_id"):
config.set("defaults", "infura_id", "")
if not config.has_option("defaults", "infura_id"):
config.set("defaults", "infura_id", "")
with codecs.open(self.config_path, "w", "utf-8") as fp:
config.write(fp)
with codecs.open(self.config_path, "w", "utf-8") as fp:
config.write(fp)
if not self.infura_id:
self.infura_id = config.get("defaults", "infura_id", fallback="")
if not self.infura_id:
self.infura_id = config.get("defaults", "infura_id", fallback="")
@staticmethod
def _add_default_options(config: ConfigParser) -> None:

@ -0,0 +1,78 @@
import os
import time
import errno
"""
credits: https://github.com/dmfrey/FileLock
"""
class LockFileException(Exception):
pass
class LockFile(object):
"""
Locks files.
"""
def __init__(self, file_name, timeout=100, delay=0.05):
"""
Initialises the file locker
"""
if timeout is not None and delay is None:
raise ValueError("If timeout is not None, then delay must not be None.")
self.is_locked = False
self.lockfile = os.path.join(os.getcwd(), f"{file_name}.lock")
self.file_name = file_name
self.timeout = timeout
self.delay = delay
def acquire(self):
"""
Acquires a lock when possible.
"""
start_time = time.time()
while True:
try:
self.fd = os.open(self.lockfile, os.O_CREAT | os.O_EXCL | os.O_RDWR)
self.is_locked = True
break
except OSError as e:
if e.errno != errno.EEXIST:
raise
if (time.time() - start_time) >= self.timeout:
raise FileLockException(
f"Stuck for more than {self.timeout} seconds waiting to unlock the file {self.filename}."
)
time.sleep(self.delay)
def release(self):
"""
Releases the lock
"""
if self.is_locked:
os.close(self.fd)
os.unlink(self.lockfile)
self.is_locked = False
def __enter__(self):
"""
Lock gets acquired at the `with` statement.
"""
if not self.is_locked:
self.acquire()
return self
def __exit__(self, type, value, traceback):
"""
Lock get's released at the end of the `with` block
"""
if self.is_locked:
self.release()
def __del__(self):
"""
Releases the lock during deletion.
"""
self.release()

@ -0,0 +1,16 @@
from subprocess import Popen, PIPE
from tests import PROJECT_DIR, TESTDATA
import json
MYTH = str(PROJECT_DIR / "myth")
def test_parallel():
test_file = str(TESTDATA / "input_contracts" / "origin.sol")
program = f"python3 {MYTH} a {test_file} --solv 0.5.0 -o json"
processes = [Popen(program, stdout=PIPE, shell=True) for i in range(30)]
for p in processes:
out, err = p.communicate()
json_output = json.loads(out.decode("utf-8"))
assert json_output["success"] == True
Loading…
Cancel
Save