Skip to content

asyncio errors on Python 3.11 (and older) #3715

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mdmintz opened this issue Apr 30, 2025 · 3 comments · Fixed by #3717
Closed

asyncio errors on Python 3.11 (and older) #3715

mdmintz opened this issue Apr 30, 2025 · 3 comments · Fixed by #3717
Assignees
Labels
bug Uh oh... Something needs to be fixed UC Mode / CDP Mode Undetected Chromedriver Mode / CDP Mode

Comments

@mdmintz
Copy link
Member

mdmintz commented Apr 30, 2025

asyncio errors on Python 3.11 (and older)

Caused by a regression in4.37.10 for an edge case.

This isn't happening on Python 3.12 (or newer).

Python 3.12 made improvements to asyncio:

Image

There's a recent SeleniumBase asyncio memory-saving feature that works great on Python 3.12 (and newer), but it causes some asyncio errors on 3.11 and older because those versions of Python have an asyncio issue that wasn't fixed until Python 3.12.

This reproduces the issue on Python 3.11:

from seleniumbase import SB

with SB(uc=True, test=True) as sb:
    sb.activate_cdp_mode()
    sb.open("data:text/html,<h1>Page A</h1>")
    sb.assert_text("Page A")
    sb.open_new_tab()
    sb.open("data:text/html,<h1>Page B</h1>")
    sb.assert_text("Page B")
    sb.switch_to_tab(0)
    sb.assert_text("Page A")
    sb.assert_text_not_visible("Page B")
    sb.switch_to_tab(1)
    sb.assert_text("Page B")
    sb.assert_text_not_visible("Page A")
ERROR:asyncio:Task was destroyed but it is pending!
task: <Task cancelling name='Task-3' coro=<Listener.listener_loop() running at ~/SeleniumBase/seleniumbase/undetected/cdp_driver/connection.py:575> wait_for=<Future pending cb=[Task.task_wakeup()]>>
@mdmintz mdmintz added bug Uh oh... Something needs to be fixed UC Mode / CDP Mode Undetected Chromedriver Mode / CDP Mode labels Apr 30, 2025
@mdmintz mdmintz self-assigned this Apr 30, 2025
@mdmintz
Copy link
Member Author

mdmintz commented Apr 30, 2025

This was resolved in 4.37.11 - https://github.com/seleniumbase/SeleniumBase/releases/tag/v4.37.11

@felipehertzer
Copy link

Hey @mdmintz,

I tested this on Python 3.11 and was able to reproduce the bug. I resolved the issue by modifying the Listener.cancel method. With this change, the leak no longer occurs in Python 3.11. The root cause was that Listener.cancel was not awaiting the cancellation process, but only on 3.11 (That is weird).

async def cancel(self):
    if self.task and not self.task.cancelled():
        self.task.cancel()
        try:
            await self.task
        except asyncio.CancelledError:
            pass

and then add the awaits:

await self.listener.cancel()

@mdmintz
Copy link
Member Author

mdmintz commented May 1, 2025

@felipehertzer Thanks for investigating that. I'll ship a new release shortly with the fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Uh oh... Something needs to be fixed UC Mode / CDP Mode Undetected Chromedriver Mode / CDP Mode
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants