Skip to content

Commit

Permalink
Merge pull request #39 from Hundemeier/socket-close
Browse files Browse the repository at this point in the history
Close Sockets on Stop
  • Loading branch information
Hundemeier authored Aug 18, 2021
2 parents 21b68ed + 6d352e9 commit 1542d5a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
3 changes: 2 additions & 1 deletion sacn/receiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ def start(self) -> None:

def stop(self) -> None:
"""
Stops a running thread. If no thread was started nothing happens.
Stops a running thread and closes the underlying socket. If no thread was started, nothing happens.
Do not reuse the socket after calling stop once.
"""
self._handler.socket.stop()

Expand Down
15 changes: 11 additions & 4 deletions sacn/receiving/receiver_socket_udp.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ def __init__(self, listener: ReceiverSocketListener, bind_address: str, bind_por

def start(self):
# initialize thread infos
thread = threading.Thread(
self._thread = threading.Thread(
target=self.receive_loop,
name=THREAD_NAME
)
# thread.setDaemon(True) # TODO: might be beneficial to use a daemon thread
thread.start()
# self._thread.setDaemon(True) # TODO: might be beneficial to use a daemon thread
self._thread.start()

def receive_loop(self) -> None:
"""
Expand All @@ -60,9 +60,16 @@ def receive_loop(self) -> None:

def stop(self) -> None:
"""
Stop a potentially running thread by gracefull shutdown. Does not stop the thread immediately.
Stops a running thread and closes the underlying socket. If no thread was started, nothing happens.
Do not reuse the socket after calling stop once.
"""
self._enabled_flag = False
try:
self._thread.join()
# stop the socket, after the loop terminated
self._socket.close()
except AttributeError:
pass

def join_multicast(self, multicast_addr: str) -> None:
"""
Expand Down
3 changes: 2 additions & 1 deletion sacn/sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ def start(self) -> None:

def stop(self) -> None:
"""
Tries to stop a current running sender. A running Thread will be stopped and should terminate.
Stops a running thread and closes the underlying socket. If no thread was started, nothing happens.
Do not reuse the socket after calling stop once.
"""
self._sender_handler.stop()

Expand Down
16 changes: 12 additions & 4 deletions sacn/sending/sender_socket_udp.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ def __init__(self, listener: SenderSocketListener, bind_address: str, bind_port:

def start(self):
# initialize thread infos
thread = threading.Thread(
self._thread = threading.Thread(
target=self.send_loop,
name=THREAD_NAME
)
# thread.setDaemon(True) # TODO: might be beneficial to use a daemon thread
thread.start()
# self._thread.setDaemon(True) # TODO: might be beneficial to use a daemon thread
self._thread.start()

def send_loop(self) -> None:
self._logger.info(f'Started {THREAD_NAME}')
Expand All @@ -63,9 +63,17 @@ def send_loop(self) -> None:

def stop(self) -> None:
"""
Stop a potentially running thread by gracefull shutdown. Does not stop the thread immediately.
Stops a running thread and closes the underlying socket. If no thread was started, nothing happens.
Do not reuse the socket after calling stop once.
"""
self._enabled_flag = False
# wait for the thread to finish
try:
self._thread.join()
# stop the socket, after the loop terminated
self._socket.close()
except AttributeError:
pass

def send_unicast(self, data: RootLayer, destination: str) -> None:
self.send_packet(data.getBytes(), destination)
Expand Down

0 comments on commit 1542d5a

Please sign in to comment.