Skip to content

Persistent exceptions thrown into generators can grow indefinitely #105795

@mbish

Description

@mbish

Bug report

Exceptions thrown into a generator multiple times have multiple tracebacks which can lead to unbounded growth of exceptions.

It's not clear to me if this an issue or working as designed honestly. I do find the behavior to be unexpected.

Exceptions which persist across multiple calls to generator.throw accumulate stack frames inside of their __traceback__ object. This can lead to unbounded object growth if the exception is particularly long lived and frequently used (see twisted/twisted#11874 for an example).

Failing unit test displaying this behavior. One would expect the exception to have 2 stack frames but instead it has 3 (2 from line 14 and one from line 6).
https://gist.github.com/mbish/25f67b0e56d944103108fbc515b0f821

Your environment

Linux 6.3.6-arch1-1 twisted/twisted#1 SMP PREEMPT_DYNAMIC Mon, 05 Jun 2023 15:12:57 +0000 x86_64 GNU/Linux
DISTRIB_ID="Arch"
DISTRIB_RELEASE="rolling"
DISTRIB_DESCRIPTION="Arch Linux"

  • CPython versions tested on: Python 3.11.3
  • Operating system and architecture:

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions