diff options
| author | Andrew Svetlov <andrew.svetlov@gmail.com> | 2020-11-10 13:58:31 (GMT) |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-11-10 13:58:31 (GMT) |
| commit | 42d873c63aa9d160c132be4a34599531574db12c (patch) | |
| tree | 52d68f9f283cfe6c7b91239f7bce075ed1e1f418 /Lib/asyncio/base_futures.py | |
| parent | 0b9c4c6fcf2b0673fa45ddfa092934a9d5479b8c (diff) | |
| download | cpython-42d873c63aa9d160c132be4a34599531574db12c.zip cpython-42d873c63aa9d160c132be4a34599531574db12c.tar.gz cpython-42d873c63aa9d160c132be4a34599531574db12c.tar.bz2 | |
bpo-42183: Fix a stack overflow error for asyncio Task or Future repr() (GH-23020)
The overflow occurs under some circumstances when a task or future
recursively returns itself.
Co-authored-by: Kyle Stanley <aeros167@gmail.com>
Diffstat (limited to 'Lib/asyncio/base_futures.py')
| -rw-r--r-- | Lib/asyncio/base_futures.py | 25 |
1 files changed, 22 insertions, 3 deletions
diff --git a/Lib/asyncio/base_futures.py b/Lib/asyncio/base_futures.py index 22f2980..2c01ac9 100644 --- a/Lib/asyncio/base_futures.py +++ b/Lib/asyncio/base_futures.py @@ -1,6 +1,7 @@ __all__ = () import reprlib +from _thread import get_ident from . import format_helpers @@ -41,6 +42,16 @@ def _format_callbacks(cb): return f'cb=[{cb}]' +# bpo-42183: _repr_running is needed for repr protection +# when a Future or Task result contains itself directly or indirectly. +# The logic is borrowed from @reprlib.recursive_repr decorator. +# Unfortunately, the direct decorator usage is impossible because of +# AttributeError: '_asyncio.Task' object has no attribute '__module__' error. +# +# After fixing this thing we can return to the decorator based approach. +_repr_running = set() + + def _future_repr_info(future): # (Future) -> str """helper function for Future.__repr__""" @@ -49,9 +60,17 @@ def _future_repr_info(future): if future._exception is not None: info.append(f'exception={future._exception!r}') else: - # use reprlib to limit the length of the output, especially - # for very long strings - result = reprlib.repr(future._result) + key = id(future), get_ident() + if key in _repr_running: + result = '...' + else: + _repr_running.add(key) + try: + # use reprlib to limit the length of the output, especially + # for very long strings + result = reprlib.repr(future._result) + finally: + _repr_running.discard(key) info.append(f'result={result}') if future._callbacks: info.append(_format_callbacks(future._callbacks)) |
