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 exception handling #199

Merged
merged 9 commits into from
Jun 28, 2018
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: 2 additions & 0 deletions rclpy/rclpy/executors.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,8 @@ def spin_once(self, timeout_sec=None):
pass
else:
handler()
if handler.exception() is not None:
raise handler.exception()


class MultiThreadedExecutor(Executor):
Expand Down
10 changes: 10 additions & 0 deletions rclpy/rclpy/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

import inspect
import sys
import threading
import weakref

Expand All @@ -34,6 +35,7 @@ def __init__(self, *, executor=None):
self._result = None
# An exception raised by the handler when called
self._exception = None
self._exception_fetched = False
# callbacks to be scheduled after this task completes
self._callbacks = []
# Lock for threadsafety
Expand All @@ -42,6 +44,12 @@ def __init__(self, *, executor=None):
self._executor = None
self._set_executor(executor)

def __del__(self):
if self._exception is not None and not self._exception_fetched:
print(
'The following exception was never retrieved: ' +
str(self._exception), file=sys.stderr)

def __await__(self):
# Yield if the task is not finished
while not self._done:
Expand Down Expand Up @@ -87,6 +95,7 @@ def exception(self):

:return: The exception raised by the task
"""
self._exception_fetched = True
return self._exception

def set_result(self, result):
Expand All @@ -109,6 +118,7 @@ def set_exception(self, exception):
"""
with self._lock:
self._exception = exception
self._exception_fetched = False
self._done = True
self._cancelled = False
self._schedule_done_callbacks()
Expand Down