Skip to content

Commit

Permalink
Handle session close during connection, KeyError: <aiohttp.connector.…
Browse files Browse the repository at this point in the history
…_TransportPlaceholder> #2193
  • Loading branch information
fafhrd91 committed Sep 21, 2017
1 parent 1609acd commit 0b807a8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
11 changes: 8 additions & 3 deletions aiohttp/connector.py
Expand Up @@ -10,7 +10,8 @@
from types import MappingProxyType

from . import hdrs, helpers
from .client_exceptions import (ClientConnectorError, ClientHttpProxyError,
from .client_exceptions import (ClientConnectionError, ClientConnectorError,
ClientHttpProxyError,
ClientProxyConnectionError,
ServerFingerprintMismatch)
from .client_proto import ResponseHandler
Expand Down Expand Up @@ -386,11 +387,15 @@ def connect(self, req):
self._acquired_per_host[key].add(placeholder)
try:
proto = yield from self._create_connection(req)
if self._closed:
proto.close()
raise ClientConnectionError("Connector is closed.")
except OSError as exc:
raise ClientConnectorError(key, exc) from exc
finally:
self._acquired.remove(placeholder)
self._acquired_per_host[key].remove(placeholder)
if not self._closed:
self._acquired.remove(placeholder)
self._acquired_per_host[key].remove(placeholder)

self._acquired.add(proto)
self._acquired_per_host[key].add(proto)
Expand Down
1 change: 1 addition & 0 deletions changes/2193.bugfix
@@ -0,0 +1 @@
Handle session close during connection, KeyError: <aiohttp.connector._TransportPlaceholder>
23 changes: 23 additions & 0 deletions tests/test_connector.py
Expand Up @@ -588,6 +588,29 @@ def test_connect_connection_error(loop):
assert ctx.value.ssl == req.ssl


@asyncio.coroutine
def test_close_during_connect(loop):
proto = mock.Mock()
proto.is_connected.return_value = True

fut = helpers.create_future(loop)
req = ClientRequest('GET', URL('http://host:80'), loop=loop)

conn = aiohttp.BaseConnector(loop=loop)
conn._create_connection = mock.Mock()
conn._create_connection.return_value = fut

task = helpers.ensure_future(conn.connect(req), loop=loop)
yield from asyncio.sleep(0, loop=loop)
conn.close()

fut.set_result(proto)
with pytest.raises(aiohttp.ClientConnectionError):
yield from task

assert proto.close.called


def test_ctor_cleanup():
loop = mock.Mock()
loop.time.return_value = 1.5
Expand Down

0 comments on commit 0b807a8

Please sign in to comment.