hw_drivers: Stop devices threads on disconnect

The threads of devices were still running after `disconnect` had been
called. We add a `_stopped` flag that is set when the thread should be
stopped.

closes odoo/odoo#56500

X-original-commit: 639a4e4fc72567a9655599eef150fabefaf1ff2e
Related: odoo/enterprise#12651
Signed-off-by: Quentin Lejeune (qle) <qle@odoo.com>
Signed-off-by: Antoine Prieëls <aprieels@users.noreply.github.com>
This commit is contained in:
Antoine Prieels
2020-08-25 11:24:56 +00:00
parent 72681dc53f
commit a7bbcd1360
4 changed files with 7 additions and 3 deletions
+3 -1
View File
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from threading import Thread
from threading import Thread, Event
from odoo.addons.hw_drivers.main import drivers, iot_devices
@@ -32,6 +32,7 @@ class Driver(Thread, metaclass=DriverMetaClass):
self.device_type = ''
self.device_manufacturer = ''
self.data = {'value': ''}
self._stopped = Event()
@classmethod
def supported(cls, device):
@@ -48,4 +49,5 @@ class Driver(Thread, metaclass=DriverMetaClass):
raise NotImplementedError()
def disconnect(self):
self._stopped.set()
del iot_devices[self.device_identifier]
@@ -70,7 +70,7 @@ class DisplayDriver(Driver):
event_manager.device_changed(self)
def run(self):
while self.device_identifier != 'distant_display':
while self.device_identifier != 'distant_display' and not self._stopped.isSet():
time.sleep(60)
if self.url != 'http://localhost:8069/point_of_sale/display/' + self.device_identifier:
# Refresh the page every minute
@@ -135,6 +135,8 @@ class KeyboardUSBDriver(Driver):
def run(self):
try:
for event in self.input_device.read_loop():
if self._stopped.isSet():
break
if event.type == evdev.ecodes.EV_KEY:
data = evdev.categorize(event)
@@ -134,7 +134,7 @@ class SerialDriver(Driver):
self._connection = connection
self._status['status'] = self.STATUS_CONNECTED
self._push_status()
while True:
while not self._stopped.isSet():
self._take_measure()
time.sleep(self._protocol.newMeasureDelay)
except Exception: