ui: perform db updates in the main thread
Some checks failed
Build status / Build (push) Has been cancelled

we may end up with db corruptions otherwise.
This commit is contained in:
Gustavo Iñiguez Goia 2025-02-17 16:49:49 +01:00
parent c6b42890c0
commit 65d6cccd63
Failed to generate hash of commit

View file

@ -46,6 +46,7 @@ class UIService(ui_pb2_grpc.UIServicer, QtWidgets.QGraphicsObject):
_status_change_trigger = QtCore.pyqtSignal(bool) _status_change_trigger = QtCore.pyqtSignal(bool)
_notification_callback = QtCore.pyqtSignal(str, ui_pb2.NotificationReply) _notification_callback = QtCore.pyqtSignal(str, ui_pb2.NotificationReply)
_show_message_trigger = QtCore.pyqtSignal(str, str, int, int) _show_message_trigger = QtCore.pyqtSignal(str, str, int, int)
_temp_rule_expired = QtCore.pyqtSignal(str, str)
# .desktop filename located under /usr/share/applications/ # .desktop filename located under /usr/share/applications/
DESKTOP_FILENAME = "opensnitch_ui.desktop" DESKTOP_FILENAME = "opensnitch_ui.desktop"
@ -191,6 +192,7 @@ class UIService(ui_pb2_grpc.UIServicer, QtWidgets.QGraphicsObject):
self._stats_dialog.settings_saved.connect(self._on_settings_saved) self._stats_dialog.settings_saved.connect(self._on_settings_saved)
self._stats_dialog.close_trigger.connect(self._on_close) self._stats_dialog.close_trigger.connect(self._on_close)
self._show_message_trigger.connect(self._show_systray_message) self._show_message_trigger.connect(self._show_systray_message)
self._temp_rule_expired.connect(self._on_temp_rule_expired)
def _setup_icons(self): def _setup_icons(self):
self.off_image = QtGui.QPixmap(os.path.join(self._path, "res/icon-off.png")) self.off_image = QtGui.QPixmap(os.path.join(self._path, "res/icon-off.png"))
@ -719,6 +721,15 @@ class UIService(ui_pb2_grpc.UIServicer, QtWidgets.QGraphicsObject):
return newconf return newconf
@QtCore.pyqtSlot(str, str)
def _on_temp_rule_expired(self, node_addr, rule_name):
self._nodes.disable_rule(node_addr, rule_name)
try:
key = node_addr+rule_name
del self._sched_tasks[key]
except:
pass
@QtCore.pyqtSlot(dict) @QtCore.pyqtSlot(dict)
def _on_node_actions(self, kwargs): def _on_node_actions(self, kwargs):
if kwargs['action'] == self.NODE_ADD: if kwargs['action'] == self.NODE_ADD:
@ -754,20 +765,13 @@ class UIService(ui_pb2_grpc.UIServicer, QtWidgets.QGraphicsObject):
# disable temporal rules in the db # disable temporal rules in the db
def _disable_temp_rule(args): def _disable_temp_rule(args):
srv = args[0] self._temp_rule_expired.emit(args[0], args[1].name)
# sched task key
key = args[1]+args[2].name
srv._nodes.disable_rule(args[1], args[2].name)
try:
del srv._sched_tasks[key]
except:
pass
timeout = duration.to_seconds(rule.duration) timeout = duration.to_seconds(rule.duration)
if rule.duration == Config.DURATION_ONCE: if rule.duration == Config.DURATION_ONCE:
timeout = 1 timeout = 1
if timeout > 0: if timeout > 0:
ost = OneshotTimer(timeout, _disable_temp_rule, (self, node_addr, rule,)) ost = OneshotTimer(timeout, _disable_temp_rule, (node_addr, rule,))
key = node_addr + rule.name key = node_addr + rule.name
self._sched_tasks[key] = ost self._sched_tasks[key] = ost
ost.start() ost.start()