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

options.add_argument("--disable-gpu") potentially fixes zombie processes not being closed properly.

Open 11AnJo opened this issue 1 year ago • 7 comments

A lot of people including me had a problem with chrome processes not being closed correctly sometimes. I found out that adding options.add_argument("--disable-gpu") fixes it.

I don't really know what to do with that because I am not an expert but I just spent all day looking for answers and that fixed it for me. Hope that could help somebody.

11AnJo avatar Jan 11 '24 16:01 11AnJo

I have the same problem and after trying what you mentioned, the problem is still there. I don't know what to do anymore... I've even tried use_subprocess=False and it doesn't solve either.

ps -ef | grep -v grep | grep defunct

frikide+ 31066 31012  0 10:13 pts/1    00:00:00 [chromedriver] <defunct>
frikide+ 31313 31012  0 10:13 pts/1    00:00:00 [chromedriver] <defunct>
frikide+ 31923 31012 16 10:15 pts/1    00:00:06 [chromium-browse] <defunct>
frikide+ 31925 31012  0 10:15 pts/1    00:00:00 [chromedriver] <defunct>
frikide+ 32152 31012 23 10:15 pts/1    00:00:05 [chromium-browse] <defunct>
frikide+ 32166 31012  1 10:15 pts/1    00:00:00 [chromedriver] <defunct>

FRIKIdelTO avatar Jan 12 '24 09:01 FRIKIdelTO

@FRIKIdelTO Did you tried https://github.com/ultrafunkamsterdam/undetected-chromedriver/issues/1507#issuecomment-1694627996 ?

11AnJo avatar Jan 12 '24 15:01 11AnJo

@FRIKIdelTO Did you tried #1507 (comment) ?

I just tried it now and the problem is still there

FRIKIdelTO avatar Jan 12 '24 16:01 FRIKIdelTO

It solved on my end too, I was using this nuclear option https://github.com/ultrafunkamsterdam/undetected-chromedriver/issues/1667#issuecomment-1847275773

will try for a couple days to see if it's a long-term solution

bigcharl avatar Jan 13 '24 05:01 bigcharl

Finally solved with this function (copy/paste and modified from seleniumbase):

def quit_driver(driver):
    try:
        os.kill(driver.browser_pid, 15)
        if "linux" in sys.platform:
            os.waitpid(driver.browser_pid, 0)
            time.sleep(0.02)
        else:
            time.sleep(0.04)
    except:
        pass
    if hasattr(driver, "service") and getattr(driver.service, "process", None):
        driver.service.stop()
    try:
        if driver.reactor and isinstance(driver.reactor, Reactor):
            driver.reactor.event.set()
    except:
        pass
    if (
        hasattr(driver, "keep_user_data_dir")
        and hasattr(driver, "user_data_dir")
        and not driver.keep_user_data_dir
    ):
        import shutil
        for _ in range(5):
            try:
                shutil.rmtree(driver.user_data_dir, ignore_errors=False)
            except FileNotFoundError:
                pass
            else:
                break
            time.sleep(0.1)
    driver.patcher = None

FRIKIdelTO avatar Jan 13 '24 11:01 FRIKIdelTO

Finally solved with this function (copy/paste and modified from seleniumbase):

def quit_driver(driver):
    try:
        os.kill(driver.browser_pid, 15)
        if "linux" in sys.platform:
            os.waitpid(driver.browser_pid, 0)
            time.sleep(0.02)
        else:
            time.sleep(0.04)
    except:
        pass
    if hasattr(driver, "service") and getattr(driver.service, "process", None):
        driver.service.stop()
    try:
        if driver.reactor and isinstance(driver.reactor, Reactor):
            driver.reactor.event.set()
    except:
        pass
    if (
        hasattr(driver, "keep_user_data_dir")
        and hasattr(driver, "user_data_dir")
        and not driver.keep_user_data_dir
    ):
        import shutil
        for _ in range(5):
            try:
                shutil.rmtree(driver.user_data_dir, ignore_errors=False)
            except FileNotFoundError:
                pass
            else:
                break
            time.sleep(0.1)
    driver.patcher = None

I just want to say this is the ONLY solution that worked. Everyone here is talking about just killing the process while the problem is clearly a file lock which prevents the defunct process from being killed. So check your file descriptors folks. Can we get this patched ?

ifeldshteyn avatar Mar 09 '24 18:03 ifeldshteyn

The only way I fixed this issue was calling close() before quit() like:

driver.close()
driver.quit()

0x1991 avatar May 04 '24 10:05 0x1991