Skip to content

no way to explicitly shutdown async generators #1953

@graingert

Description

@graingert

given:

import sys
import trio

from jinja2 import Template, select_autoescape


class MyModel:
    class objects:
        @staticmethod
        async def all():
            while True:
                yield "hello"


template = """
<html>
  <head></head>
  <body>
    {% for m in model.objects.all() %}
        {{ m }}
        {% break %}
    {% endfor %}
  </body>
</html>
"""


async def amain():
    return await Template(
        source=template, enable_async=True, extensions=["jinja2.ext.loopcontrols"]
    ).render_async(model=MyModel)


def main():
    trio.run(amain)


if __name__ == "__main__":
    sys.exit(main())

this results in:

 python -Wall demo_jinja_asyncgens.py
<template>:19: ResourceWarning: Async generator 'jinja2.async_utils.auto_aiter' was garbage collected before it had been exhausted. Surround its use in 'async with aclosing(...):' to ensure that it gets cleaned up as soon as you're done using it.
ResourceWarning: Enable tracemalloc to get the object allocation traceback
/home/graingert/.virtualenvs/demo-jinja-asyncgens/lib/python3.12/site-packages/trio/_core/_asyncgens.py:204: ResourceWarning: Async generator '__main__.MyModel.objects.all' was garbage collected before it had been exhausted. Surround its use in 'async with aclosing(...):' to ensure that it gets cleaned up as soon as you're done using it.
  await agen.aclose()
ResourceWarning: Enable tracemalloc to get the object allocation traceback

but there's no way to wrap generators with async with aclosing(...): in jinja2

Environment:

  • Python version: 3.12.2
  • Jinja version: 3.1.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions