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

Fix closing client session #3733

Merged
merged 21 commits into from
Jul 4, 2019
Merged

Fix closing client session #3733

merged 21 commits into from
Jul 4, 2019

Conversation

atemate
Copy link
Contributor

@atemate atemate commented May 7, 2019

What do these changes do?

Make BaseConnector.close() an asynchronous function (still BaseConnector._close() is synchronous and preserves the old behaviour).

Now, await connector.close() waits until all the connections are closed instead of just sending the closing signal to all the connections. The "Unclosed connection" warnings appeared because some connections required more time to be closed because some protocols (TLS) perform additional steps on connection-close and thus require longer time.

Are there changes in behavior for the user?

Yes:

  • BaseConnector.close() -> await BaseConnector.close()
  • Deprecated with Connector(): syntax is dropped

Related issue number

#3736 #1925.
This PR removes functionality deprecated in PR #3417.

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

@atemate atemate changed the title [WIP] Fix closing client session Fix closing client session May 7, 2019
@atemate atemate marked this pull request as ready for review May 7, 2019 21:48
@atemate atemate requested a review from asvetlov as a code owner May 7, 2019 21:48
aiohttp/base_protocol.py Outdated Show resolved Hide resolved
aiohttp/connector.py Outdated Show resolved Hide resolved
@codecov-io
Copy link

codecov-io commented May 7, 2019

Codecov Report

Merging #3733 into master will decrease coverage by 0.1%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #3733      +/-   ##
==========================================
- Coverage   97.96%   97.86%   -0.11%     
==========================================
  Files          43       43              
  Lines        8590     8602      +12     
  Branches     1373     1375       +2     
==========================================
+ Hits         8415     8418       +3     
- Misses         71       79       +8     
- Partials      104      105       +1
Impacted Files Coverage Δ
aiohttp/client_proto.py 96.64% <100%> (+0.09%) ⬆️
aiohttp/connector.py 96.72% <100%> (-0.96%) ⬇️
aiohttp/web_fileresponse.py 96.55% <0%> (-1.15%) ⬇️
aiohttp/helpers.py 97.22% <0%> (-0.26%) ⬇️

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 dcce5ea...fd2f90e. Read the comment docs.

aiohttp/base_protocol.py Outdated Show resolved Hide resolved
aiohttp/connector.py Outdated Show resolved Hide resolved
aiohttp/connector.py Outdated Show resolved Hide resolved
aiohttp/base_protocol.py Outdated Show resolved Hide resolved
@asvetlov
Copy link
Member

Please don't forget to update docs.
http://docs.aiohttp.org/en/stable/client_advanced.html and http://docs.aiohttp.org/en/stable/client_reference.html mention connectors. These pages should reflect the PR changes.

@atemate
Copy link
Contributor Author

atemate commented Jun 7, 2019

Sorry for this long-pending PR, I will return back to it in the end of June 2019 when I have more time, thank you

@atemate atemate requested a review from webknjaz as a code owner June 28, 2019 13:04
if waiters:
await asyncio.gather(*waiters,
loop=self._loop,
return_exceptions=True)
Copy link
Contributor

@socketpair socketpair Jun 28, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

possibly re-raise ? I'm unsure

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ineed, we lost exceptions here. But maybe we should just log (as warnings) them instead?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heh, very interesting question.
Python and asyncio itself have no MultipleException yet.
Maybe we add it in Python 3.9.
I'm not sure if warnings.warn() is a good choice here: it is not a programming error but network issue.
Please use logging.error() instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok


# TODO (A.Yushovskiy, 24-May-2019) collect transp. closing futures
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

incomplete task ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

work with transports here, especially self._cleanup_closed_transports, seem to be a workaround of the non-closed transports for SSL only:

if (key.is_ssl and
not self._cleanup_closed_disabled):
self._cleanup_closed_transports.append(
transport)

Also, transports in _cleanup_closed_transports are of type asyncio.Transport so we can not modify them to save closing future same way as we did in this PR with ResponseHandler, thus I don't see a way to get the awaitable result of transport.abort().

Good news is that most likely these calls are redundant since we close all protocols (i.e., save them to the list of futures: waiters.append(proto.closed))

Copy link
Member

@asvetlov asvetlov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good but please fix my comments.

@@ -72,6 +71,8 @@
from .client_reqrep import ConnectionKey # noqa
from .tracing import Trace # noqa

_ListOfFutures = List['asyncio.Future[Optional[Exception]]']
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it a list of futures that contains None or Exception as a value or list of futures with fut.set_result(None) or fut.set_exception(exc)?
If later -- please use Future[None].
I believe we never return an exception but set it explicitly.

if waiters:
await asyncio.gather(*waiters,
loop=self._loop,
return_exceptions=True)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heh, very interesting question.
Python and asyncio itself have no MultipleException yet.
Maybe we add it in Python 3.9.
I'm not sure if warnings.warn() is a good choice here: it is not a programming error but network issue.
Please use logging.error() instead.

@@ -49,7 +49,6 @@
CeilTimeout,
get_running_loop,
is_ip_address,
noop2,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please delete helpers.noop2 function as well.

Copy link
Member

@asvetlov asvetlov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@asvetlov asvetlov merged commit 6b0bc4e into aio-libs:master Jul 4, 2019
@atemate atemate deleted the ay/fix-close-client-session branch July 4, 2019 13:10
blueyed added a commit to blueyed/aioresponses that referenced this pull request Jul 16, 2019
This is required since
aio-libs/aiohttp@6b0bc4e.

Fixes:

    tests/test_aioresponses.py:266: in test_address_as_instance_of_url_combined_with_pass_through
        api, ext = yield from doit()
    tests/test_aioresponses.py:258: in doit
        api_resp = yield from self.session.get(self.url)
    aioresponses/core.py:328: in _request_mock
        response = await self.match(method, url, **kwargs)
    aioresponses/core.py:304: in match
        response = await matcher.build_response(url, **kwargs)
    aioresponses/core.py:184: in build_response
        reason=result.reason)
    aioresponses/core.py:157: in _build_response
        resp.content = stream_reader_factory()
    aioresponses/compat.py:26: in stream_reader_factory
        protocol = ResponseHandler(loop=loop)
    ../aiohttp/aiohttp/client_proto.py:41: in __init__
        self.closed = self._loop.create_future()  # type: asyncio.Future[None]
    E   AttributeError: 'NoneType' object has no attribute 'create_future'

Ref: aio-libs/aiohttp#3733
blueyed added a commit to blueyed/aioresponses that referenced this pull request Jul 16, 2019
This is required since
aio-libs/aiohttp@6b0bc4e.

Fixes:

    tests/test_aioresponses.py:266: in test_address_as_instance_of_url_combined_with_pass_through
        api, ext = yield from doit()
    tests/test_aioresponses.py:258: in doit
        api_resp = yield from self.session.get(self.url)
    aioresponses/core.py:328: in _request_mock
        response = await self.match(method, url, **kwargs)
    aioresponses/core.py:304: in match
        response = await matcher.build_response(url, **kwargs)
    aioresponses/core.py:184: in build_response
        reason=result.reason)
    aioresponses/core.py:157: in _build_response
        resp.content = stream_reader_factory()
    aioresponses/compat.py:26: in stream_reader_factory
        protocol = ResponseHandler(loop=loop)
    ../aiohttp/aiohttp/client_proto.py:41: in __init__
        self.closed = self._loop.create_future()  # type: asyncio.Future[None]
    E   AttributeError: 'NoneType' object has no attribute 'create_future'

Ref: aio-libs/aiohttp#3733
blueyed added a commit to blueyed/aioresponses that referenced this pull request Jul 16, 2019
This is required since
aio-libs/aiohttp@6b0bc4e.

Fixes:

    tests/test_aioresponses.py:266: in test_address_as_instance_of_url_combined_with_pass_through
        api, ext = yield from doit()
    tests/test_aioresponses.py:258: in doit
        api_resp = yield from self.session.get(self.url)
    aioresponses/core.py:328: in _request_mock
        response = await self.match(method, url, **kwargs)
    aioresponses/core.py:304: in match
        response = await matcher.build_response(url, **kwargs)
    aioresponses/core.py:184: in build_response
        reason=result.reason)
    aioresponses/core.py:157: in _build_response
        resp.content = stream_reader_factory()
    aioresponses/compat.py:26: in stream_reader_factory
        protocol = ResponseHandler(loop=loop)
    ../aiohttp/aiohttp/client_proto.py:41: in __init__
        self.closed = self._loop.create_future()  # type: asyncio.Future[None]
    E   AttributeError: 'NoneType' object has no attribute 'create_future'

Ref: aio-libs/aiohttp#3733
blueyed added a commit to blueyed/aioresponses that referenced this pull request Sep 5, 2019
This is required since
aio-libs/aiohttp@6b0bc4e.

Fixes:

    tests/test_aioresponses.py:266: in test_address_as_instance_of_url_combined_with_pass_through
        api, ext = yield from doit()
    tests/test_aioresponses.py:258: in doit
        api_resp = yield from self.session.get(self.url)
    aioresponses/core.py:328: in _request_mock
        response = await self.match(method, url, **kwargs)
    aioresponses/core.py:304: in match
        response = await matcher.build_response(url, **kwargs)
    aioresponses/core.py:184: in build_response
        reason=result.reason)
    aioresponses/core.py:157: in _build_response
        resp.content = stream_reader_factory()
    aioresponses/compat.py:26: in stream_reader_factory
        protocol = ResponseHandler(loop=loop)
    ../aiohttp/aiohttp/client_proto.py:41: in __init__
        self.closed = self._loop.create_future()  # type: asyncio.Future[None]
    E   AttributeError: 'NoneType' object has no attribute 'create_future'

Ref: aio-libs/aiohttp#3733
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants