Skip to content

Commit

Permalink
Properly close socket and exit threads before exit
Browse files Browse the repository at this point in the history
  • Loading branch information
FilipeMaia committed Dec 30, 2023
1 parent 97ac1c9 commit 5b403c0
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
3 changes: 1 addition & 2 deletions hummingbird/interface/data_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ def __init__(self, parent, hostname, port, ssh_tunnel=None, conf={}):
# Move the data socket to its own thread to ensure it's not blocked by the GUI
self._data_socket.moveToThread(self.thread)
self.thread.started.connect(self._data_socket.init_socket)
self.thread.finished.connect(self._data_socket.deleteLater)
self.thread.start()
self.destroyed.connect(self.thread.quit)
self._data_socket.closed.connect(self.thread.quit)

self.conf = conf
self._group_structure = {}
Expand Down
2 changes: 0 additions & 2 deletions hummingbird/interface/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,6 @@ def save_settings(self, filename=None):
ds_settings = []
for ds in self._data_sources:
ds_settings.append([ds.hostname, ds.port, ds.ssh_tunnel, ds.conf])
# Exit the thread associated with the data source
ds.thread.exit()
s.setValue("dataSources", ds_settings)
self.plotdata_widget.save_state(s)
s.setValue("plotData", self.plotdata_widget.save_plotdata())
Expand Down
12 changes: 9 additions & 3 deletions hummingbird/interface/zmqsocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ class ZmqSocket(QtCore.QObject):
"""Wrapper around a zmq socket. Provides Qt signal handling"""
ready_read = QtCore.Signal()
ready_write = QtCore.Signal()
closed = QtCore.Signal()
def __init__(self, _type, parent=None, **kwargs):
QtCore.QObject.__init__(self, parent, **kwargs)
self._type = _type
self._socket = None
QtCore.QCoreApplication.instance().aboutToQuit.connect(self.close)

def init_socket(self):
ctx = ZmqContext.instance()
Expand All @@ -33,9 +35,13 @@ def init_socket(self):
self._socket.setsockopt(RCVHWM, 100)
self.filters = []

def __del__(self):
"""Close socket on deletion"""
self._socket.close()
def close(self):
"""Close socket"""
try:
self._socket.close()
except:
pass
self.closed.emit()

def set_identity(self, name):
"""Set zmq socket identity"""
Expand Down

0 comments on commit 5b403c0

Please sign in to comment.