Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add event to avoid deadlocks on run_command calls #386

Merged
merged 3 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/pykiso/interfaces/dt_auxiliary.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def __init__(
self.is_proxy_capable = is_proxy_capable
self.auto_start = auto_start
self.lock = threading.RLock()
self._stop_event = threading.Event()
self.stop_tx = threading.Event()
self.stop_rx = threading.Event()
self.queue_in = queue.Queue()
Expand Down Expand Up @@ -116,6 +117,11 @@ def run_command(
:return: True if the request is correctly executed otherwise
False
"""
# avoid a deadlock in case run_command is called within a _receive_message implementation
# (for e.g. callback implementation) while delete_instance is being executed from the main thread
if self._stop_event.is_set():
return timeout_result

with self.lock:
if not self.is_instance:
raise AuxiliaryNotStarted(self.name)
Expand Down Expand Up @@ -171,10 +177,11 @@ def delete_instance(self) -> bool:
log.internal_info(f"Deleting instance of auxiliary {self.name}")

with self.lock:
# if the current aux is not alive don't try to delete it
# again
self._stop_event.set()
# if the current aux is not alive don't try to delete it again
if not self.is_instance:
log.internal_info(f"Auxiliary {self.name} is already deleted")
self._stop_event.clear()
return True

# stop each auxiliary's tasks
Expand All @@ -189,6 +196,7 @@ def delete_instance(self) -> bool:
)

self.is_instance = False
self._stop_event.clear()
return is_deleted

def _start_tx_task(self) -> None:
Expand Down
22 changes: 22 additions & 0 deletions tests/test_dt_auxiliary_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,28 @@ def test_run_command(mocker, aux_inst):
assert value == b"\x02"


def test_run_command_stop_event_set(mocker, aux_inst):
queue_put = mocker.patch.object(aux_inst.queue_in, "put")
queue_get = mocker.patch.object(aux_inst.queue_out, "get", return_value=b"\x02")

aux_inst.is_instance = True
aux_inst._stop_event.set()

value = aux_inst.run_command(
cmd_message="send",
cmd_data=b"\x01",
blocking=False,
timeout_in_s=0,
timeout_result="dummy",
)

aux_inst._stop_event.clear()

assert queue_get.call_count == 0
assert queue_put.call_count == 0
assert value == "dummy"


def test_run_command_timeout(mocker, aux_inst):
queue_put = mocker.patch.object(aux_inst.queue_in, "put")

Expand Down
Loading