Skip to content
Myr

asyncio: Wait for the stream to close when closing (#494)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Jamie Hewland · committed by Florimond Manca · parent f68b3df · Browse files · 2 files+18 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+18 added−1 removed

  • tests:+15 added−1 removed
  • other:+3 added−0 removed

2 files+18 added−1 removed

modified:httpx/concurrency/asyncio.py+3 added
1import asyncio2import functools3import ssl4import sys5import typing6from types import TracebackType78from ..config import PoolLimits, TimeoutConfig
161 unmodified lines
170 return self.stream_reader.at_eof()171172 async def close(self) -> None:173 self.stream_writer.close()174 if sys.version_info >= (3, 7):175 await self.stream_writer.wait_closed()176177178class PoolSemaphore(BasePoolSemaphore):179 def __init__(self, pool_limits: PoolLimits):
118 unmodified lines
modified:tests/test_concurrency.py+15 added-1 removed
48 unmodified lines
49 assert stream.is_connection_dropped() is False50 assert get_cipher(stream) is not None5152 await stream.write(b"GET / HTTP/1.1\r\n\r\n")53 assert (await stream.read(8192, timeout)).startswith(b"HTTP/1.1 200 OK\r\n")5354 # stream.read() only gives us *up to* as much data as we ask for. In order to55 # cleanly close the stream, we must read until the end of the HTTP response.56 read = b""57 ended = False58 for _ in range(5): # Try read some (not too large) number of times...59 read += await stream.read(8192, timeout)60 # We know we're at the end of the response when we've received the body plus61 # the terminating CRLFs.62 if b"Hello, world!" in read and read.endswith(b"\r\n\r\n"):63 ended = True64 break6566 assert ended67 assert read.startswith(b"HTTP/1.1 200 OK\r\n")6869 finally:70 await stream.close()