Skip to content

Commit

Permalink
Avoid ignoring asyncio exceptions in coroutines
Browse files Browse the repository at this point in the history
Exceptions that happen when a coroutine is being destroyed are
ignored, but they cause backround noise such as "Exception ignored in
coroutine ..." exceptions etc. This change tries to propogate the
exceptions so that the asyncio base handler itself can ignore the
exception as an attempt to get rid of that exception ignored noise in
test outputs etc.

Should fix juju#657
  • Loading branch information
cderici committed Mar 29, 2022
1 parent 6c5255b commit f7552be
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
6 changes: 3 additions & 3 deletions juju/client/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ async def _debug_logger(self):
# be cancelled by the reconnect and we don't want the reconnect
# to be aborted half-way through
jasyncio.ensure_future(self.reconnect())
return
raise
except Exception as e:
log.exception("Error in debug logger : %s" % e)
jasyncio.create_task(self.close())
Expand All @@ -539,7 +539,7 @@ async def _receiver(self):
# be cancelled by the reconnect and we don't want the reconnect
# to be aborted half-way through
jasyncio.ensure_future(self.reconnect())
return
raise
except Exception as e:
log.exception("Error in receiver")
# make pending listeners aware of the error
Expand Down Expand Up @@ -576,7 +576,7 @@ async def _do_ping():
# The connection has closed - we can't do anything
# more until the connection is restarted.
log.debug('ping failed because of closed connection')
pass
raise

async def rpc(self, msg, encoder=None):
'''Make an RPC to the API. The message is encoded as JSON
Expand Down
5 changes: 3 additions & 2 deletions juju/jasyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ def _task_result_exp_handler(task, task_name=task_name, logger=logger):
try:
task.result()
except CancelledError:
pass
raise
except websockets.exceptions.ConnectionClosed:
return
raise
except Exception as e:
# This really is an arbitrary exception we need to catch
#
Expand All @@ -76,6 +76,7 @@ def _task_result_exp_handler(task, task_name=task_name, logger=logger):
# event_handler, which won't do anything but yell 'Task
# exception was never retrieved' anyways.
logger.exception("Task %s raised an exception: %s" % (task_name, e))
raise

task = create_task(coro)
task.add_done_callback(functools.partial(_task_result_exp_handler, task_name=task_name, logger=logger))
Expand Down

0 comments on commit f7552be

Please sign in to comment.