diff --git a/playwright/_impl/_connection.py b/playwright/_impl/_connection.py index 3c0cd6da4..578f77ad6 100644 --- a/playwright/_impl/_connection.py +++ b/playwright/_impl/_connection.py @@ -168,8 +168,9 @@ def stop_sync(self) -> None: self._transport.stop() self._dispatcher_fiber.switch() - def stop_async(self) -> None: + async def stop_async(self) -> None: self._transport.stop() + await self._transport.wait_until_stopped() async def wait_for_object_with_known_name(self, guid: str) -> Any: if guid in self._objects: diff --git a/playwright/_impl/_transport.py b/playwright/_impl/_transport.py index 8e07ad1e7..331b1a523 100644 --- a/playwright/_impl/_transport.py +++ b/playwright/_impl/_transport.py @@ -46,8 +46,12 @@ def stop(self) -> None: self._stopped = True self._output.close() + async def wait_until_stopped(self) -> None: + await self._stopped_future + async def run(self) -> None: self._loop = asyncio.get_running_loop() + self._stopped_future: asyncio.Future = asyncio.Future() driver_env = os.environ.copy() # VSCode's JavaScript Debug Terminal provides it but driver/pkg does not support it @@ -87,6 +91,7 @@ async def run(self) -> None: except asyncio.IncompleteReadError: break await asyncio.sleep(0) + self._stopped_future.set_result(None) def send(self, message: Dict) -> None: msg = json.dumps(message) diff --git a/playwright/async_api/_context_manager.py b/playwright/async_api/_context_manager.py index 16821e762..2b64e20ae 100644 --- a/playwright/async_api/_context_manager.py +++ b/playwright/async_api/_context_manager.py @@ -42,4 +42,4 @@ async def start(self) -> AsyncPlaywright: return await self.__aenter__() async def __aexit__(self, *args: Any) -> None: - self._connection.stop_async() + await self._connection.stop_async()