From 2a61ed914be543dd0db59e7c7c1c23e0ba0e7a9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pierre-Luc=20Tessier=20Gagn=C3=A9?= Date: Tue, 11 Jun 2019 08:14:07 -0400 Subject: [PATCH 1/6] Use inter-process mutex to prevent concurrent neoVI device open When neoVI server is enabled, there is an issue with concurrent device open. --- can/interfaces/ics_neovi/neovi_bus.py | 12 +++++++++++- setup.py | 1 + 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/can/interfaces/ics_neovi/neovi_bus.py b/can/interfaces/ics_neovi/neovi_bus.py index 3df0ccc39..9f341963e 100644 --- a/can/interfaces/ics_neovi/neovi_bus.py +++ b/can/interfaces/ics_neovi/neovi_bus.py @@ -11,9 +11,12 @@ """ import logging +import os +import tempfile from collections import deque from can import Message, CanError, BusABC +from filelock import FileLock logger = logging.getLogger(__name__) @@ -122,7 +125,14 @@ def __init__(self, channel, can_filters=None, **kwargs): type_filter = kwargs.get("type_filter") serial = kwargs.get("serial") self.dev = self._find_device(type_filter, serial) - ics.open_device(self.dev) + + # Use inter-process mutex to prevent concurrent device open. + # When neoVI server is enabled, there is an issue with concurrent + # device open. + open_lock = FileLock( + os.path.join(tempfile.gettempdir(), 'neovi.lock')) + with open_lock: + ics.open_device(self.dev) if "bitrate" in kwargs: for channel in self.channels: diff --git a/setup.py b/setup.py index 1918b644b..576bf1e28 100644 --- a/setup.py +++ b/setup.py @@ -90,6 +90,7 @@ "wrapt~=1.10", "aenum", 'windows-curses;platform_system=="Windows"', + 'filelock' ], setup_requires=["pytest-runner"], extras_require=extras_require, From d1aa80c0040af89f19c7c1e848f937d469b3a3f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pierre-Luc=20Tessier=20Gagn=C3=A9?= Date: Tue, 11 Jun 2019 08:51:25 -0400 Subject: [PATCH 2/6] Move neoVI open lock to module level --- can/interfaces/ics_neovi/neovi_bus.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/can/interfaces/ics_neovi/neovi_bus.py b/can/interfaces/ics_neovi/neovi_bus.py index 9f341963e..ad53850b3 100644 --- a/can/interfaces/ics_neovi/neovi_bus.py +++ b/can/interfaces/ics_neovi/neovi_bus.py @@ -15,9 +15,10 @@ import tempfile from collections import deque -from can import Message, CanError, BusABC from filelock import FileLock +from can import Message, CanError, BusABC + logger = logging.getLogger(__name__) try: @@ -31,6 +32,11 @@ ics = None +# Use inter-process mutex to prevent concurrent device open. +# When neoVI server is enabled, there is an issue with concurrent device open. +open_lock = FileLock(os.path.join(tempfile.gettempdir(), "neovi.lock")) + + class ICSApiError(CanError): """ Indicates an error with the ICS API. @@ -126,11 +132,6 @@ def __init__(self, channel, can_filters=None, **kwargs): serial = kwargs.get("serial") self.dev = self._find_device(type_filter, serial) - # Use inter-process mutex to prevent concurrent device open. - # When neoVI server is enabled, there is an issue with concurrent - # device open. - open_lock = FileLock( - os.path.join(tempfile.gettempdir(), 'neovi.lock')) with open_lock: ics.open_device(self.dev) From c2e767b35887b5cf4b3994a32fa2c91939c660ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pierre-Luc=20Tessier=20Gagn=C3=A9?= Date: Tue, 11 Jun 2019 10:55:59 -0400 Subject: [PATCH 3/6] Adding filelock to neovi extra requirements --- setup.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 576bf1e28..9ea7b78fb 100644 --- a/setup.py +++ b/setup.py @@ -24,7 +24,10 @@ long_description = f.read() # Dependencies -extras_require = {"serial": ["pyserial~=3.0"], "neovi": ["python-ics>=2.12"]} +extras_require = { + "serial": ["pyserial~=3.0"], + "neovi": ["python-ics>=2.12", "filelock"], +} tests_require = [ "pytest~=4.3", From 3dc6dfc60eb73231af00da596d845cd5bc009fc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pierre-Luc=20Tessier=20Gagn=C3=A9?= Date: Tue, 11 Jun 2019 13:37:41 -0400 Subject: [PATCH 4/6] Adding dummy file lock when importing FileLock fails --- can/interfaces/ics_neovi/neovi_bus.py | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/can/interfaces/ics_neovi/neovi_bus.py b/can/interfaces/ics_neovi/neovi_bus.py index ad53850b3..55a510283 100644 --- a/can/interfaces/ics_neovi/neovi_bus.py +++ b/can/interfaces/ics_neovi/neovi_bus.py @@ -15,8 +15,6 @@ import tempfile from collections import deque -from filelock import FileLock - from can import Message, CanError, BusABC logger = logging.getLogger(__name__) @@ -32,6 +30,30 @@ ics = None +try: + from filelock import FileLock +except ImportError as ie: + + logger.warning( + "Using ICS NeoVi can backend without the " + "filelock module installed may cause some issues!: %s", + ie, + ) + + class FileLock: + """Dummy file lock that do not actually do anything""" + + def __init__(self, lock_file, timeout=-1): + self._lock_file = lock_file + self.timeout = timeout + + def __enter__(self): + return self + + def __exit__(self, exc_type, exc_val, exc_tb): + return None + + # Use inter-process mutex to prevent concurrent device open. # When neoVI server is enabled, there is an issue with concurrent device open. open_lock = FileLock(os.path.join(tempfile.gettempdir(), "neovi.lock")) From e18c2d51db76bb6cd9f8f4d285a7bba44205f53d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pierre-Luc=20Tessier=20Gagn=C3=A9?= Date: Tue, 11 Jun 2019 14:08:59 -0400 Subject: [PATCH 5/6] Using doubles quote since Black refers double quotes --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 9ea7b78fb..fb1a4de54 100644 --- a/setup.py +++ b/setup.py @@ -93,7 +93,7 @@ "wrapt~=1.10", "aenum", 'windows-curses;platform_system=="Windows"', - 'filelock' + "filelock", ], setup_requires=["pytest-runner"], extras_require=extras_require, From 587de9b663dee7fded611314aafcfa7779704b74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pierre-Luc=20Tessier=20Gagn=C3=A9?= Date: Wed, 12 Jun 2019 07:44:36 -0400 Subject: [PATCH 6/6] Grammar fix --- can/interfaces/ics_neovi/neovi_bus.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/can/interfaces/ics_neovi/neovi_bus.py b/can/interfaces/ics_neovi/neovi_bus.py index 55a510283..176747ad2 100644 --- a/can/interfaces/ics_neovi/neovi_bus.py +++ b/can/interfaces/ics_neovi/neovi_bus.py @@ -41,7 +41,7 @@ ) class FileLock: - """Dummy file lock that do not actually do anything""" + """Dummy file lock that does not actually do anything""" def __init__(self, lock_file, timeout=-1): self._lock_file = lock_file