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

[trees] handling signals better #262

Merged
merged 3 commits into from
Nov 23, 2019
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
2 changes: 1 addition & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Release Notes

Forthcoming
-----------
* ...
* [trees] play nicely, reset signal handlers after setup, `#262 <https://github.com/splintered-reality/py_trees/pull/262>`_

2.0.1 (2019-11-19)
------------------
Expand Down
4 changes: 0 additions & 4 deletions py_trees/blackboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -1122,12 +1122,8 @@ def unregister_key(self, key: str, clear: bool=True):
Raises:
KeyError if the key has not been previously registered
"""
print("DJS: Unregister Key")
print("DJS: Unregister Key: {}".format(key))
key = Blackboard.absolute_name(super().__getattribute__("namespace"), key)
print("DJS: Unregister Key: {}".format(key))
remapped_key = super().__getattribute__("remappings")[key]
print("DJS: Unregister Remapped Key: {}".format(remapped_key))
super().__getattribute__("read").discard(key) # doesn't throw exceptions if it not present
super().__getattribute__("write").discard(key)
super().__getattribute__("exclusive").discard(key)
Expand Down
16 changes: 12 additions & 4 deletions py_trees/trees.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
##############################################################################
# Documentation
##############################################################################
from distutils.core import setup

"""
While a graph of connected behaviours and composites form a tree in their own right
Expand All @@ -26,6 +25,7 @@
# Imports
##############################################################################

import functools
import os
import signal
import threading
Expand Down Expand Up @@ -75,8 +75,9 @@ def setup(root: behaviour.Behaviour,
def on_timer_timed_out():
os.kill(os.getpid(), _SIGNAL)

def signal_handler(unused_signum, unused_frame):
raise RuntimeError("tree setup timed out")
def signal_handler(unused_signum, unused_frame, original_signal_handler):
signal.signal(_SIGNAL, original_signal_handler)
raise RuntimeError("tree setup interrupted or timed out")

def visited_setup():
for node in root.iterate():
Expand All @@ -87,11 +88,18 @@ def visited_setup():
if timeout == common.Duration.INFINITE:
visited_setup()
else:
signal.signal(_SIGNAL, signal_handler)
original_signal_handler = signal.getsignal(_SIGNAL)
signal.signal(
_SIGNAL,
functools.partial(
signal_handler,
original_signal_handler=original_signal_handler)
)
timer = threading.Timer(interval=timeout, function=on_timer_timed_out)
timer.start()
visited_setup()
timer.cancel() # this only works if the timer is still waiting
signal.signal(_SIGNAL, original_signal_handler)

##############################################################################
# Trees
Expand Down