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

make sure coroutine locals are promptly destroyed #1864

Merged
merged 6 commits into from
Jan 31, 2021
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 newsfragments/1864.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The event loop now holds on to references of coroutine frames for only
the minimum necessary period of time.
14 changes: 14 additions & 0 deletions trio/_core/_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -2176,13 +2176,21 @@ def unrolled_run(runner, async_fn, args, host_uses_signal_set_wakeup_fd=False):
for _ in range(CONTEXT_RUN_TB_FRAMES):
tb = tb.tb_next
final_outcome = Error(task_exc.with_traceback(tb))
# Remove local refs so that e.g. cancelled coroutine locals
# are not kept alive by this frame until another exception
# comes along.
del tb

if final_outcome is not None:
# We can't call this directly inside the except: blocks
# above, because then the exceptions end up attaching
# themselves to other exceptions as __context__ in
# unwanted ways.
runner.task_exited(task, final_outcome)
# final_outcome may contain a traceback ref. It's not as
# crucial compared to the above, but this will allow more
# prompt release of resources in coroutine locals.
final_outcome = None
else:
task._schedule_points += 1
if msg is CancelShieldedCheckpoint:
Expand Down Expand Up @@ -2211,10 +2219,16 @@ def unrolled_run(runner, async_fn, args, host_uses_signal_set_wakeup_fd=False):
# which works for at least asyncio and curio.
runner.reschedule(task, exc)
task._next_send_fn = task.coro.throw
# prevent long-lived reference
# TODO: develop test for this deletion
del msg

if "after_task_step" in runner.instruments:
runner.instruments.call("after_task_step", task)
del GLOBAL_RUN_CONTEXT.task
# prevent long-lived references
# TODO: develop test for these deletions
del task, next_send, next_send_fn

except GeneratorExit:
# The run-loop generator has been garbage collected without finishing
Expand Down
25 changes: 25 additions & 0 deletions trio/_core/tests/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import time
import types
import warnings
import weakref
from contextlib import contextmanager, ExitStack
from math import inf
from textwrap import dedent
Expand Down Expand Up @@ -2249,3 +2250,27 @@ async def test_nursery_cancel_doesnt_create_cyclic_garbage():
finally:
gc.set_debug(old_flags)
gc.garbage.clear()


@pytest.mark.skipif(
sys.implementation.name != "cpython", reason="Only makes sense with refcounting GC"
)
async def test_locals_destroyed_promptly_on_cancel():
destroyed = False

def finalizer():
nonlocal destroyed
destroyed = True

class A:
pass

async def task():
a = A()
weakref.finalize(a, finalizer)
await _core.checkpoint()

async with _core.open_nursery() as nursery:
nursery.start_soon(task)
nursery.cancel_scope.cancel()
assert destroyed