aiohttp icon indicating copy to clipboard operation
aiohttp copied to clipboard

Accept async context managers for cleanup contexts (#11681)

Open MannXo opened this issue 2 months ago • 2 comments

What do these changes do?

Adapt async context managers to behave like single-yield async iterators.

1- Allow app.cleanup_ctx callbacks to return either: a single-yield async generator (existing behavior), or an async context manager (e.g. @contextlib.asynccontextmanager).

2- Internally adapt async context managers with a small adapter so: __aenter__ runs at startup (equivalent to the generator yielding), and __aexit__ runs at cleanup (equivalent to finishing the generator).

3- Preserve previous cleanup/error aggregation semantics.

Are there changes in behavior for the user?

No breaking changes; this is fully backwards compatible.

  • Existing async-generator cleanup contexts continue to work unchanged.
  • New: callers may pass contextlib.asynccontextmanager instances for cleanup contexts.

Is it a substantial burden for the maintainers to support this?

No

Related issue number

Closes #11681

Checklist

  • [x] I think the code is well written
  • [x] Unit tests for the changes exist
  • [x] Documentation reflects the changes
  • [x] If you provide code modification, please add yourself to CONTRIBUTORS.txt
    • The format is <Name> <Surname>.
    • Please keep alphabetical order, the file is sorted by names.
  • [x] Add a new news fragment into the CHANGES/ folder
    • name it <issue_or_pr_num>.<type>.rst (e.g. 588.bugfix.rst)

    • if you don't have an issue number, change it to the pull request number after creating the PR

      • .bugfix: A bug fix for something the maintainers deemed an improper undesired behavior that got corrected to match pre-agreed expectations.
      • .feature: A new behavior, public APIs. That sort of stuff.
      • .deprecation: A declaration of future API removals and breaking changes in behavior.
      • .breaking: When something public is removed in a breaking way. Could be deprecated in an earlier release.
      • .doc: Notable updates to the documentation structure or build process.
      • .packaging: Notes for downstreams about unobvious side effects and tooling. Changes in the test invocation considerations and runtime assumptions.
      • .contrib: Stuff that affects the contributor experience. e.g. Running tests, building the docs, setting up the development environment.
      • .misc: Changes that are hard to assign to any of the above categories.
    • Make sure to use full sentences with correct case and punctuation, for example:

      Fixed issue with non-ascii contents in doctest text files
      -- by :user:`contributor-gh-handle`.
      

      Use the past tense or the present tense a non-imperative mood, referring to what's changed compared to the last released version of this project.

MannXo avatar Oct 22 '25 12:10 MannXo

CodSpeed Performance Report

Merging #11704 will not alter performance

Comparing MannXo:feature/accept-async-context-manager-for-cleanup-context (5f5dd32) with master (1fbb1bb)

Summary

✅ 59 untouched

codspeed-hq[bot] avatar Oct 22 '25 12:10 codspeed-hq[bot]

Codecov Report

:white_check_mark: All modified and coverable lines are covered by tests. :white_check_mark: Project coverage is 98.73%. Comparing base (1fbb1bb) to head (5f5dd32). :white_check_mark: All tests successful. No failed tests found.

Additional details and impacted files
@@           Coverage Diff            @@
##           master   #11704    +/-   ##
========================================
  Coverage   98.73%   98.73%            
========================================
  Files         127      127            
  Lines       43546    43646   +100     
  Branches     2320     2323     +3     
========================================
+ Hits        42996    43095    +99     
  Misses        390      390            
- Partials      160      161     +1     
Flag Coverage Δ
CI-GHA 98.61% <100.00%> (+<0.01%) :arrow_up:
OS-Linux 98.34% <100.00%> (+<0.01%) :arrow_up:
OS-Windows 96.68% <100.00%> (+<0.01%) :arrow_up:
OS-macOS 97.56% <100.00%> (+<0.01%) :arrow_up:
Py-3.10.11 97.11% <100.00%> (+<0.01%) :arrow_up:
Py-3.10.18 97.61% <100.00%> (+<0.01%) :arrow_up:
Py-3.11.13 97.81% <100.00%> (+<0.01%) :arrow_up:
Py-3.11.9 97.32% <100.00%> (-0.01%) :arrow_down:
Py-3.12.10 97.42% <100.00%> (+<0.01%) :arrow_up:
Py-3.12.11 97.91% <100.00%> (+<0.01%) :arrow_up:
Py-3.13.7 97.91% <100.00%> (-0.01%) :arrow_down:
Py-3.13.9 97.40% <100.00%> (-0.01%) :arrow_down:
Py-3.14.0 98.13% <100.00%> (+0.01%) :arrow_up:
Py-3.14.0t 97.19% <100.00%> (+0.01%) :arrow_up:
Py-pypy3.10.16-7.3.19 92.28% <100.00%> (-3.09%) :arrow_down:
VM-macos 97.56% <100.00%> (+<0.01%) :arrow_up:
VM-ubuntu 98.34% <100.00%> (+<0.01%) :arrow_up:
VM-windows 96.68% <100.00%> (+<0.01%) :arrow_up:

Flags with carried forward coverage won't be shown. Click here to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codecov[bot] avatar Oct 22 '25 12:10 codecov[bot]