Skip to content
Myr

Lock around stream.write and stream.close operations (#699)

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

authored by Tom Christie · committed by GitHub · parent 22663bc · Browse files · 2 files+10 added−6 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+10 added−6 removed

  • other:+10 added−6 removed

2 files+10 added−6 removed

modified:httpx/backends/asyncio.py+8 added-5 removed
75 unmodified lines
76 ):77 self.stream_reader = stream_reader78 self.stream_writer = stream_writer79 self.read_lock = asyncio.Lock()80 self.write_lock = asyncio.Lock()8182 self._inner: typing.Optional[SocketStream] = None8384 async def start_tls(
50 unmodified lines
135 async def write(self, data: bytes, timeout: Timeout) -> None:136 if not data:137 return138138 self.stream_writer.write(data)139 try:140 async with self.write_lock:141 self.stream_writer.write(data)140 return await asyncio.wait_for(141 self.stream_writer.drain(), timeout.write_timeout142 )142 return await asyncio.wait_for(143 self.stream_writer.drain(), timeout.write_timeout144 )145 except asyncio.TimeoutError:146 raise WriteTimeout() from None147148 def is_connection_dropped(self) -> bool:
20 unmodified lines
169 # why we don't call it here.170 # This is fine, though, because '.close()' schedules the actual closing of the171 # stream, meaning that at best it will happen during the next event loop172 # iteration, and at worst asyncio will take care of it on program exit.173 async with self.write_lock:171 self.stream_writer.close()174 self.stream_writer.close()175176177class AsyncioBackend(ConcurrencyBackend):178 def __init__(self) -> None:
113 unmodified lines
modified:httpx/backends/trio.py+2 added-1 removed
77 unmodified lines
78 # See: https://github.com/encode/httpx/pull/143#issuecomment-51518177879 return stream.socket.is_readable()8081 async def close(self) -> None:82 async with self.write_lock:82 await self.stream.aclose()83 await self.stream.aclose()848586class TrioBackend(ConcurrencyBackend):87 async def open_tcp_stream(
92 unmodified lines