Skip to content

Commit f4e6cdf

Browse files
Mark async transport closed only after successful close
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent 427f8e0 commit f4e6cdf

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

hyperbrowser/transport/async_transport.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ def __init__(self, api_key: str, headers: Optional[Mapping[str, str]] = None):
2727

2828
async def close(self) -> None:
2929
if not self._closed:
30-
self._closed = True
3130
await self.client.aclose()
31+
self._closed = True
3232

3333
async def __aenter__(self):
3434
return self
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import asyncio
2+
3+
import pytest
4+
5+
from hyperbrowser.transport.async_transport import AsyncTransport
6+
7+
8+
class _FailingAsyncClient:
9+
async def aclose(self) -> None:
10+
raise RuntimeError("close failed")
11+
12+
13+
class _TrackingAsyncClient:
14+
def __init__(self) -> None:
15+
self.close_calls = 0
16+
17+
async def aclose(self) -> None:
18+
self.close_calls += 1
19+
20+
21+
def test_async_transport_close_does_not_mark_closed_when_close_fails():
22+
transport = AsyncTransport(api_key="test-key")
23+
original_client = transport.client
24+
asyncio.run(original_client.aclose())
25+
transport.client = _FailingAsyncClient() # type: ignore[assignment]
26+
27+
async def run() -> None:
28+
with pytest.raises(RuntimeError, match="close failed"):
29+
await transport.close()
30+
31+
asyncio.run(run())
32+
assert transport._closed is False
33+
34+
35+
def test_async_transport_close_is_idempotent_after_success():
36+
transport = AsyncTransport(api_key="test-key")
37+
original_client = transport.client
38+
asyncio.run(original_client.aclose())
39+
tracking_client = _TrackingAsyncClient()
40+
transport.client = tracking_client # type: ignore[assignment]
41+
42+
async def run() -> None:
43+
await transport.close()
44+
await transport.close()
45+
46+
asyncio.run(run())
47+
assert tracking_client.close_calls == 1
48+
assert transport._closed is True

0 commit comments

Comments
 (0)