Skip to content

Commit 9452b54

Browse files
pierreluctgPierre-Luc Tessier Gagné
andauthored
Backport Neovi features for release 3.3.4 (#903)
* Use inter-process mutex to prevent concurrent neoVI device open When neoVI server is enabled, there is an issue with concurrent device open. * Move neoVI open lock to module level * Adding dummy file lock when importing FileLock fails * Grammar fix * Raising more precise API error when set bitrate fails Also calling shutdown before raising exception from the init if the device is opened * Simplified if fd condition in init * Moving filelock to neovi extras_require * Adding comma back * Update CHANGELOG.txt Co-authored-by: Pierre-Luc Tessier Gagné <[email protected]>
1 parent 57b1a34 commit 9452b54

File tree

3 files changed

+52
-10
lines changed

3 files changed

+52
-10
lines changed

CHANGELOG.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Version 3.3.3
1515

1616
Backported fixes from 4.x development branch which targets Python 3.
1717

18+
* #846 Use inter-process mutex to prevent concurrent neoVI device open.
1819
* #798 Backport caching msg.data value in neovi interface.
1920
* #796 Fix Vector CANlib treatment of empty app name.
2021
* #771 Handle empty CSV file.

can/interfaces/ics_neovi/neovi_bus.py

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
"""
1212

1313
import logging
14+
import os
15+
import tempfile
1416
from collections import deque
1517

1618
from can import Message, CanError, BusABC
@@ -27,6 +29,35 @@
2729
ics = None
2830

2931

32+
try:
33+
from filelock import FileLock
34+
except ImportError as ie:
35+
36+
logger.warning(
37+
"Using ICS NeoVi can backend without the "
38+
"filelock module installed may cause some issues!: %s",
39+
ie,
40+
)
41+
42+
class FileLock:
43+
"""Dummy file lock that does not actually do anything"""
44+
45+
def __init__(self, lock_file, timeout=-1):
46+
self._lock_file = lock_file
47+
self.timeout = timeout
48+
49+
def __enter__(self):
50+
return self
51+
52+
def __exit__(self, exc_type, exc_val, exc_tb):
53+
return None
54+
55+
56+
# Use inter-process mutex to prevent concurrent device open.
57+
# When neoVI server is enabled, there is an issue with concurrent device open.
58+
open_lock = FileLock(os.path.join(tempfile.gettempdir(), "neovi.lock"))
59+
60+
3061
class ICSApiError(CanError):
3162
"""
3263
Indicates an error with the ICS API.
@@ -118,18 +149,28 @@ def __init__(self, channel, can_filters=None, **kwargs):
118149
type_filter = kwargs.get('type_filter')
119150
serial = kwargs.get('serial')
120151
self.dev = self._find_device(type_filter, serial)
121-
ics.open_device(self.dev)
122152

123-
if 'bitrate' in kwargs:
124-
for channel in self.channels:
125-
ics.set_bit_rate(self.dev, kwargs.get('bitrate'), channel)
153+
with open_lock:
154+
ics.open_device(self.dev)
126155

127-
fd = kwargs.get('fd', False)
128-
if fd:
129-
if 'data_bitrate' in kwargs:
156+
try:
157+
if "bitrate" in kwargs:
130158
for channel in self.channels:
131-
ics.set_fd_bit_rate(
132-
self.dev, kwargs.get('data_bitrate'), channel)
159+
ics.set_bit_rate(self.dev, kwargs.get("bitrate"), channel)
160+
161+
if kwargs.get("fd", False):
162+
if "data_bitrate" in kwargs:
163+
for channel in self.channels:
164+
ics.set_fd_bit_rate(
165+
self.dev, kwargs.get("data_bitrate"), channel
166+
)
167+
except ics.RuntimeError as re:
168+
logger.error(re)
169+
err = ICSApiError(*ics.get_last_api_error(self.dev))
170+
try:
171+
self.shutdown()
172+
finally:
173+
raise err
133174

134175
self._use_system_timestamp = bool(
135176
kwargs.get('use_system_timestamp', False)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
# Dependencies
2828
extras_require = {
2929
'serial': ['pyserial~=3.0'],
30-
'neovi': ['python-ics>=2.12']
30+
'neovi': ['python-ics>=2.12', 'filelock']
3131
}
3232

3333
tests_require = [

0 commit comments

Comments
 (0)