undetected-chromedriver icon indicating copy to clipboard operation
undetected-chromedriver copied to clipboard

[nodriver] Browser.stop throws exception if browser connection has failed

Open sohaib17 opened this issue 7 months ago • 0 comments

Browser.start() adds newly launched process in registered instances and then tries to connect to the browser. But in case of slow hardware or high system load, Chrome takes some time to response and nodriver throws Failed to connect to browser and exits.

atexit handler util.deconstruct_browser() calls browser.stop() while exiting but since browser.connection haven't initialized yet, it fails with AttributeError. Leaving the running but unresponsive chrome in orphan state.

Exception:

Exception ignored in atexit callback: <function deconstruct_browser at 0x786c14bc85e0>
Traceback (most recent call last):
  File "/home/pyenv/lib/python3.12/site-packages/nodriver/core/util.py", line 138, in deconstruct_browser
    _.stop()
  File "/home/pyenv/lib/python3.12/site-packages/nodriver/core/browser.py", line 553, in stop
    asyncio.get_event_loop().create_task(self.connection.aclose())
                                         ^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'aclose'

Fix: In nodriver.core.browser.Browser.stop, add if self.connection: before close connection call.

# asyncio.get_running_loop().create_task(self.connection.send(cdp.browser.close()))
if self.connection:
    asyncio.get_event_loop().create_task(self.connection.aclose())
    logger.debug(
        "closed the connection using get_event_loop().create_task()"
    )

PS: util.deconstruct_browser() still contains a print statement at the end. It should be changed to logger.info.

print("successfully removed temp profile %s" % _.config.user_data_dir)

sohaib17 avatar Jul 15 '24 20:07 sohaib17