daijro / camoufox

🦊 Anti-detect browser
https://camoufox.com
Mozilla Public License 2.0
640 stars 49 forks source link

When headless='virtual' is enabled, Xvfb does not terminate automatically, causing threads not to exit #49

Closed Nongzhsh closed 3 weeks ago

Nongzhsh commented 3 weeks ago

Describe the bug:

When headless='virtual' is enabled in the Camoufox library, Xvfb does not terminate automatically at the end of the program. This behavior prevents the thread from exiting properly.

To Reproduce:

import threading
from concurrent.futures import ThreadPoolExecutor
from camoufox import Camoufox

def run_job(url):
    with Camoufox(
            headless='virtual',
            humanize=True,
            block_webrtc=True,
            block_images=False, ) as browser:
        page = browser.new_page()
        page.goto(url)
        page.wait_for_timeout(6000)
        page.screenshot(path=f"{threading.current_thread().name}.png")
        page.close()

if __name__ == "__main__":
    with ThreadPoolExecutor(max_workers=3) as executor:
        for index in range(5):
            executor.submit(run_job, f"https://bounty-nodejs.datashield.co/scraping/{index}")

Version:

OS: WSL2 (Ubuntu 22.04.4 LTS) Pip package: v0.3.0 Camoufox: v130.0.1-beta.13 (Up to date!)

Nongzhsh commented 3 weeks ago

os.system("pkill -u $(whoami) Xvfb") works for me after all futures are completed. However, I feel like this isn't a good idea. 😂

daijro commented 3 weeks ago

Added an experimental fix in 0.3.1. :+1:

Let me know how it works for you.

Nongzhsh commented 3 weeks ago

Added an experimental fix in 0.3.1. 👍

Let me know how it works for you.

version 0.3.2 works great! I’ve tested it, much appreciated! 👍

Nongzhsh commented 2 weeks ago

Hello, @daijro ! The Xvfb auto-termination fix worked great, but I’m noticing a new issue when running multiple Camoufox instances at the same time. If I launch the example script in different shell sessions concurrently, the browser takes a really long time to start up successfully. I wonder if there’s some kind of resource contention happening, maybe around the DISPLAYenvironment variable? Just a guess (I’m not super familiar with Python), but this might be a bug. 😅

图片