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
merged 6 commits into from
Nov 17, 2017
Merged

Close unclosed client sessions properly #2523

merged 6 commits into from
Nov 17, 2017

Conversation

hynek
Copy link
Contributor

@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

n/a

Checklist

  • 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."

@asvetlov
Copy link
Member

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.

@hynek
Copy link
Contributor Author

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?

@asvetlov
Copy link
Member

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.

@codecov-io
Copy link

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/client.py 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 November 16, 2017 20:12
@asvetlov asvetlov changed the base branch from 2.3 to master November 16, 2017 20:13
@asvetlov asvetlov merged commit d6426ad into aio-libs:master Nov 17, 2017
asvetlov pushed a commit that referenced this pull request Nov 17, 2017
* Close unclosed client sessions properly

* Add news fragment

* Drop it all together
@asvetlov
Copy link
Member

Thanks!
Backported to 2.3, aiohttp 2.3.3 released

@lock
Copy link

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]: https://github.com/aio-libs/aiohttp/issues/new

@lock lock bot added the outdated label Oct 28, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 28, 2019
@psf-chronographer psf-chronographer bot added the bot:chronographer:provided There is a change note present in this PR label Oct 28, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bot:chronographer:provided There is a change note present in this PR outdated
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants