Skip to content

Commit

Permalink
Introduce resp.abort()
Browse files Browse the repository at this point in the history
  • Loading branch information
asvetlov committed Nov 23, 2021
1 parent 368446f commit aa6db09
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 12 deletions.
7 changes: 7 additions & 0 deletions aiohttp/client_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,13 @@ def release(self) -> Any:
self._cleanup_writer()
return noop()

async def abort(self) -> None:
if not self._closed:
conn = self._connection
self.close()
if conn is not None:
await conn.cleanup()

@property
def ok(self) -> bool:
"""Returns ``True`` if ``status`` is less than ``400``, ``False`` if not.
Expand Down
4 changes: 2 additions & 2 deletions aiohttp/client_ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ async def close(self, *, code: int = WSCloseCode.OK, message: bytes = b"") -> bo
return True

if self._closing:
self._response.close()
await self._response.abort()
return True

while True:
Expand All @@ -217,7 +217,7 @@ async def close(self, *, code: int = WSCloseCode.OK, message: bytes = b"") -> bo

if msg.type == WSMsgType.CLOSE:
self._close_code = msg.data
self._response.close()
await self._response.abort()
return True
else:
return False
Expand Down
12 changes: 8 additions & 4 deletions aiohttp/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def _notify_release(self) -> None:
with suppress(Exception):
cb()

async def close(self) -> None:
def close(self) -> None:
self._notify_release()

if not self._closed:
Expand All @@ -122,8 +122,6 @@ async def close(self) -> None:
assert connector is not None
# schedule cleanup if needed
connector._release(self._key, proto, should_close=True)
# do actual closing, proto.close() supports reentrancy
await proto.close()
self._closed = True

def release(self) -> None:
Expand All @@ -140,6 +138,12 @@ def release(self) -> None:
def closed(self) -> bool:
return self._closed or not self.protocol.is_connected()

async def cleanup(self) -> None:
proto = self._protocol()
if proto is not None:
# do actual closing, proto.close() supports reentrancy
await proto.close()


class _TransportPlaceholder:
"""placeholder for BaseConnector.connect function"""
Expand Down Expand Up @@ -290,7 +294,7 @@ async def _cleanup(self) -> None:

assert isinstance(delay, Real), type(delay)
when = now + delay
if delay >= 5: # type: ignore[operator]
if delay >= 5:
when = ceil(when)

try:
Expand Down
8 changes: 4 additions & 4 deletions tests/test_client_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def protocol() -> Any:
async def test_ctor(connector: Any, key: Any, protocol: Any) -> None:
conn = Connection(connector, key, protocol)
assert conn.protocol is protocol
await conn.close()
conn.close()


async def test_callbacks_on_close(connector: Any, key: Any, protocol: Any) -> None:
Expand All @@ -42,7 +42,7 @@ def cb() -> None:
notified = True

conn.add_callback(cb)
await conn.close()
conn.close()
assert notified


Expand Down Expand Up @@ -72,14 +72,14 @@ def cb2() -> None:

conn.add_callback(cb1)
conn.add_callback(cb2)
await conn.close()
conn.close()
assert notified


async def test_close(connector: Any, key: Any, protocol: Any) -> None:
conn = Connection(connector, key, protocol)
assert not conn.closed
await conn.close()
conn.close()
# assert conn._protocol() is None
connector._release.assert_called_with(key, protocol, should_close=True)
assert conn.closed
Expand Down
4 changes: 2 additions & 2 deletions tests/test_client_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -2765,7 +2765,7 @@ def connection_lost(self, exc):
await r.read()
assert 0 == len(connector._conns)
await session.close()
connector.close()
await connector.close()
server.close()
await server.wait_closed()

Expand Down Expand Up @@ -2807,7 +2807,7 @@ def connection_lost(self, exc):
assert 0 == len(connector._conns)

await session.close()
connector.close()
await connector.close()
server.close()
await server.wait_closed()

Expand Down

0 comments on commit aa6db09

Please sign in to comment.