Skip to content

Commit 111e81f

Browse files
committed
handle async methods in logs_errors decorator
1 parent d28dc0b commit 111e81f

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

ipyparallel/util.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
"""Some generic utilities for dealing with classes, urls, and serialization."""
33
# Copyright (c) IPython Development Team.
44
# Distributed under the terms of the Modified BSD License.
5+
import asyncio
6+
import functools
7+
import inspect
58
import logging
69
import os
710
import re
@@ -23,7 +26,6 @@
2326
import zmq
2427
from dateutil.parser import parse as dateutil_parse
2528
from dateutil.tz import tzlocal
26-
from decorator import decorator
2729
from IPython import get_ipython
2830
from IPython.core.profiledir import ProfileDir
2931
from IPython.core.profiledir import ProfileDirError
@@ -101,17 +103,32 @@ def get(self, key, default=None):
101103
# -----------------------------------------------------------------------------
102104

103105

104-
@decorator
105-
def log_errors(f, self, *args, **kwargs):
106+
def log_errors(f):
106107
"""decorator to log unhandled exceptions raised in a method.
107108
108109
For use wrapping on_recv callbacks, so that exceptions
109110
do not cause the stream to be closed.
110111
"""
111-
try:
112-
return f(self, *args, **kwargs)
113-
except Exception as e:
114-
self.log.error("Uncaught exception in %r" % f, exc_info=True)
112+
113+
@functools.wraps(f)
114+
def logs_errors(self, *args, **kwargs):
115+
try:
116+
result = f(self, *args, **kwargs)
117+
except Exception:
118+
self.log.error("Uncaught exception in {f}: {future.exception()}")
119+
return
120+
121+
if inspect.isawaitable(result):
122+
# if it's async, schedule logging for when the future resolves
123+
future = asyncio.ensure_future(result)
124+
125+
def _log_error(future):
126+
if future.exception():
127+
self.log.error("Uncaught exception in {f}: {future.exception()}")
128+
129+
future.add_done_callback(_log_error)
130+
131+
return logs_errors
115132

116133

117134
def is_url(url):

0 commit comments

Comments
 (0)