diff --git a/app/auto_login.py b/app/auto_login.py index 8059fbd..876c35d 100644 --- a/app/auto_login.py +++ b/app/auto_login.py @@ -131,13 +131,25 @@ class AutoLoginManager: # Wait and validate the core login API response. login_resp = None + loop = asyncio.get_running_loop() + login_resp_future: asyncio.Future = loop.create_future() + + def on_response(resp): + try: + if "/users/ajax/login" in resp.url and resp.request.method == "POST": + if not login_resp_future.done(): + login_resp_future.set_result(resp) + except Exception: + pass + + page.on("response", on_response) try: - login_resp = await page.wait_for_response( - lambda r: "/users/ajax/login" in r.url and r.request.method == "POST", - timeout=20000, - ) - except PlaywrightTimeoutError: + login_resp = await asyncio.wait_for(login_resp_future, timeout=20) + except asyncio.TimeoutError: login_resp = None + finally: + with contextlib.suppress(Exception): + page.remove_listener("response", on_response) if login_resp is None: debug_file = await self._dump_debug_page(page)