Closed pedro-peixot0 closed 1 year ago
thought it was a fix, but it did not work
I also met this issue, did you solved it?
thought it was a fix, but it did not work
I solved it, you need to shut down the browser, please use browser.close()
to release all resources, browser.quit()
does not release resources, there is reverse with selenium, I don't know why, you can also use browser.clear_cdp_listeners()
or browser.execute_cdp_cmd("Network.disable", {})
to remove cdp listener
Hey @dylankeep, this did not work in my case. I actually found what was the issue:
When enable_cdp_events is set to True, a Reactor object is created, and with it, the object's listen function is called, starting a loop that invokes the driver.get_log function. This function uses a PoolManager / ConnectionPool object from urllib3 to monitor the network. By default, these objects handle a maximum of 1 connection, which is already being used by the aforementioned loop.
At some point, when we call the driver.quit() function to close the webdriver (I haven't tried to find where), it seems that this same object is accessed, surpassing the connection limit, thus throwing those errors.
The solution is quite simple; we just need to stop the loop started by the listen function from the Reactor object before calling the driver.quit() function. It can be done like this:
import undetected_chromedriver as uc
import time
driver = uc.Chrome(
enable_cdp_events=True,
headless=True
)
print(f"is reactor loop closed? {driver.reactor.loop.is_closed()}")
# >>> is reactor loop closed? False
while not driver.reactor.loop.is_closed():
try:
driver.reactor.loop.close()
except:
driver.reactor.event.set()
time.sleep(0.5)
print(f"is reactor loop closed? {driver.reactor.loop.is_closed()}")
# >>> is reactor loop closed? True
driver.quit()
Thanks for your help @pedro-peixot0 , this is the real reason, I also tried it after got your reply, it works fine. Nice~
I edited my previous comment after testing a bit more and reading what @avasilkov wrote in this issue. Now I think things are fixed
Here is some sample code to reproduce the issue. I am using Mac and haven't tried other operational systems