Skip to content

Commit fea6434

Browse files
authored
fix(exit): fix the task pending error on async context manager closure (#519)
1 parent c05cad2 commit fea6434

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed

playwright/_impl/_connection.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,9 @@ def stop_sync(self) -> None:
168168
self._transport.stop()
169169
self._dispatcher_fiber.switch()
170170

171-
def stop_async(self) -> None:
171+
async def stop_async(self) -> None:
172172
self._transport.stop()
173+
await self._transport.wait_until_stopped()
173174

174175
async def wait_for_object_with_known_name(self, guid: str) -> Any:
175176
if guid in self._objects:

playwright/_impl/_transport.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,12 @@ def stop(self) -> None:
4646
self._stopped = True
4747
self._output.close()
4848

49+
async def wait_until_stopped(self) -> None:
50+
await self._stopped_future
51+
4952
async def run(self) -> None:
5053
self._loop = asyncio.get_running_loop()
54+
self._stopped_future: asyncio.Future = asyncio.Future()
5155

5256
driver_env = os.environ.copy()
5357
# VSCode's JavaScript Debug Terminal provides it but driver/pkg does not support it
@@ -87,6 +91,7 @@ async def run(self) -> None:
8791
except asyncio.IncompleteReadError:
8892
break
8993
await asyncio.sleep(0)
94+
self._stopped_future.set_result(None)
9095

9196
def send(self, message: Dict) -> None:
9297
msg = json.dumps(message)

playwright/async_api/_context_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,4 @@ async def start(self) -> AsyncPlaywright:
4242
return await self.__aenter__()
4343

4444
async def __aexit__(self, *args: Any) -> None:
45-
self._connection.stop_async()
45+
await self._connection.stop_async()

0 commit comments

Comments
 (0)