Skip to content

Commit

Permalink
[trees] play nicely, reset original signal handler after setup (#262)
Browse files Browse the repository at this point in the history
  • Loading branch information
stonier authored Nov 23, 2019
1 parent c7cd069 commit 13f74ef
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
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

0 comments on commit 13f74ef

Please sign in to comment.