@@ -59,7 +59,7 @@ def __init__(
5959 ) -> None :
6060 self ._origin = origin
6161 self ._network_stream = stream
62- self ._keepalive_expiry : Optional [ float ] = keepalive_expiry
62+ self ._keepalive_expiry = keepalive_expiry
6363 self ._expire_at : Optional [float ] = None
6464 self ._state = HTTPConnectionState .NEW
6565 self ._state_lock = AsyncLock ()
@@ -77,6 +77,16 @@ async def handle_async_request(self, request: Request) -> Response:
7777 )
7878
7979 async with self ._state_lock :
80+ # If the HTTP connection is idle but the socket is readable, then the
81+ # only valid state is that the socket is about to return b"", indicating
82+ # a server-initiated disconnect.
83+ server_disconnected = (
84+ self ._state == HTTPConnectionState .IDLE
85+ and self ._network_stream .get_extra_info ("is_readable" )
86+ )
87+ if server_disconnected :
88+ raise ConnectionNotAvailable ()
89+
8090 if self ._state in (HTTPConnectionState .NEW , HTTPConnectionState .IDLE ):
8191 self ._request_count += 1
8292 self ._state = HTTPConnectionState .ACTIVE
@@ -280,17 +290,7 @@ def is_available(self) -> bool:
280290
281291 def has_expired (self ) -> bool :
282292 now = time .monotonic ()
283- keepalive_expired = self ._expire_at is not None and now > self ._expire_at
284-
285- # If the HTTP connection is idle but the socket is readable, then the
286- # only valid state is that the socket is about to return b"", indicating
287- # a server-initiated disconnect.
288- server_disconnected = (
289- self ._state == HTTPConnectionState .IDLE
290- and self ._network_stream .get_extra_info ("is_readable" )
291- )
292-
293- return keepalive_expired or server_disconnected
293+ return self ._expire_at is not None and now > self ._expire_at
294294
295295 def is_idle (self ) -> bool :
296296 return self ._state == HTTPConnectionState .IDLE
0 commit comments