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

Close unclosed client sessions properly #2523

merged 6 commits into from Nov 17, 2017

Close unclosed client sessions properly #2523

merged 6 commits into from Nov 17, 2017


Copy link

@hynek hynek commented Nov 16, 2017

What do these changes do?

ClientSession.__del__ uses a bare self.close() which causes a warning. This fixes that warning. I think this should be fine like that?

Are there changes in behavior for the user?

One warning less.

Related issue number



  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes
  • 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.
  • Add a new news fragment into the CHANGES folder
    • name it <issue_id>.<type> for example (588.bug)
    • if you don't have an issue_id change it to the pr id after creating the pr
    • ensure type is one of the following:
      • .feature: Signifying a new feature.
      • .bugfix: Signifying a bug fix.
      • .doc: Signifying a documentation improvement.
      • .removal: Signifying a deprecation or removal of public API.
      • .misc: A ticket has been closed, but it is not of interest to users.
    • Make sure to use full sentences with correct case and punctuation, for example: "Fix issue with non-ascii contents in doctest text files."
hynek added 2 commits Nov 16, 2017
Copy link

@asvetlov asvetlov commented Nov 16, 2017

It's a complex question.
__del__ is called on session destruction.
It could be even interpreter finalization stage if the session has global scope (we discourage this approach but still support it).

If the loop is closed before ClientSession.__del__ -- nothing to do.

On other hand the session could be dropped in coroutine context:

async def f():
    session = aiohttp.ClientSession()
    del session  # or just implicit decref on frame destruction

In this case the loop is running, loop.run_until_complete() is prohibited.

Copy link
Contributor Author

@hynek hynek commented Nov 16, 2017

Yeah, I suspected it’s not clear cut.

I’d suggest to either drop the close altogether or wrap it in a with suppress(Exception): for an best-effort try. As it’s now it’s kind useless, isn’t it?

Copy link

@asvetlov asvetlov commented Nov 16, 2017

I think dropping is just fine.
Let's keep a warning report part but don't try to close the session: it is not very reliable.

hynek added 2 commits Nov 16, 2017
Copy link

@codecov-io codecov-io commented Nov 16, 2017

Codecov Report

Merging #2523 into master will decrease coverage by <.01%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2523      +/-   ##
- Coverage   97.13%   97.13%   -0.01%     
  Files          39       39              
  Lines        8066     8065       -1     
  Branches     1414     1414              
- Hits         7835     7834       -1     
  Misses         99       99              
  Partials      132      132
Impacted Files Coverage Δ
tests/autobahn/ 94.91% <0%> (-0.02%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 48a704d...d573a63. Read the comment docs.

@asvetlov asvetlov changed the base branch from master to 2.3 Nov 16, 2017
@asvetlov asvetlov changed the base branch from 2.3 to master Nov 16, 2017
@asvetlov asvetlov merged commit d6426ad into aio-libs:master Nov 17, 2017
3 checks passed
3 checks passed
codecov/patch Coverage not affected when comparing 48a704d...d573a63
continuous-integration/appveyor/pr AppVeyor build succeeded
continuous-integration/travis-ci/pr The Travis CI build passed
asvetlov added a commit that referenced this pull request Nov 17, 2017
* Close unclosed client sessions properly

* Add news fragment

* Drop it all together
Copy link

@asvetlov asvetlov commented Nov 17, 2017

Backported to 2.3, aiohttp 2.3.3 released

Copy link

@lock lock bot commented Oct 28, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a [new issue] for related bugs.
If you feel like there's important points made in this discussion, please include those exceprts into that [new issue].
[new issue]:

@lock lock bot added the outdated label Oct 28, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 28, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

3 participants