Skip to content

Commit

Permalink
refactor: remove unnecessary do_not_cancel logic (#80)
Browse files Browse the repository at this point in the history
* refactor: remove unnecessary `do_not_cancel` set

`do_not_cancel` was used to keep track of tasks that should not be
canceled vs. tasks that should during shutdown phase. However, this
separation had no real effect.

* test: update expected text in assertition

The expected output when tasks outlive their grace period has changed,
assert accordingly
  • Loading branch information
zbroniszewski committed Mar 18, 2023
1 parent ab1bd97 commit 354b1e5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 19 deletions.
28 changes: 10 additions & 18 deletions aiorun.py
Expand Up @@ -306,24 +306,16 @@ async def new_coro():
else:
pending_exception_to_raise = exc

def sep():
tasks = all_tasks(loop=loop)
do_not_cancel = set()
for t in tasks:
# TODO: we don't need access to the coro. We could simply
# TODO: store the task itself in the weakset.
if t._coro in _DO_NOT_CANCEL_COROS:
do_not_cancel.add(t)

tasks -= do_not_cancel
tasks = all_tasks(loop=loop)

if tasks:
logger.info("Cancelling pending tasks.")
for t in tasks:
logger.debug("Cancelling task: %s", t)
t.cancel()
return tasks, do_not_cancel

tasks, do_not_cancel = sep()
# TODO: we don't need access to the coro. We could simply
# TODO: store the task itself in the weakset.
if t._coro not in _DO_NOT_CANCEL_COROS:
logger.debug("Cancelling task: %s", t)
t.cancel()

async def wait_for_cancelled_tasks(timeout):
""" Wait for the cancelled tasks to finish up. They have received
Expand Down Expand Up @@ -362,19 +354,19 @@ async def wait_for_cancelled_tasks(timeout):
<snip>
"""
_, pending = await asyncio.wait([*tasks, *do_not_cancel], timeout=timeout)
_, pending = await asyncio.wait([*tasks], timeout=timeout)
if pending:
tasks_info = '\n\n'.join(str(t.get_stack()) for t in pending)
msg = (
"During shutdown, the following tasks were cancelled but refused "
"During shutdown, the following tasks refused "
"to exit after {timeout} seconds: {tasks_info}".format(
timeout=timeout,
tasks_info=tasks_info
)
)
logger.warning(msg)

if tasks or do_not_cancel:
if tasks:
logger.info("Running pending tasks till complete")
# TODO: obtain all the results, and log any results that are exceptions
# other than CancelledError. Will be useful for troubleshooting.
Expand Down
2 changes: 1 addition & 1 deletion tests/test_stop_on_errors.py
Expand Up @@ -92,7 +92,7 @@ async def main():

print(excinfo.value)
print(excinfo.traceback)
assert "tasks were cancelled but refused to exit after 2.0 seconds" in caplog.text
assert "tasks refused to exit after 2.0 seconds" in caplog.text
assert "Stops the loop" in str(excinfo.value)
assert all(t.cancelled for t in created_tasks)

Expand Down

0 comments on commit 354b1e5

Please sign in to comment.