Skip to content

Commit 1b7717f

Browse files
committed
Explicitely wrap base exceptions in run_as_daemon
1 parent 2c94c85 commit 1b7717f

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

aioconsole/stream.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,28 @@ async def run_as_daemon(func, *args):
3232
future = Future()
3333
future.set_running_or_notify_cancel()
3434

35+
# A bug in python 3.7 makes it a bad idea to set a BaseException
36+
# in a wrapped future (see except statement in asyncio.Task.__wakeup)
37+
# Instead, we'll wrap base exceptions into exceptions and unwrap them
38+
# on the other side of the call.
39+
class BaseExceptionWrapper(Exception):
40+
pass
41+
3542
def daemon():
3643
try:
3744
result = func(*args)
38-
except BaseException as e:
45+
except Exception as e:
3946
future.set_exception(e)
47+
except BaseException as e:
48+
future.set_exception(BaseExceptionWrapper(e))
4049
else:
4150
future.set_result(result)
4251

4352
Thread(target=daemon, daemon=True).start()
44-
return await asyncio.wrap_future(future)
53+
try:
54+
return await asyncio.wrap_future(future)
55+
except BaseExceptionWrapper as exc:
56+
raise exc.args[0]
4557

4658

4759
def protect_standard_streams(stream):

0 commit comments

Comments
 (0)