diff --git a/python/src/camoufox/async_api.py b/python/src/camoufox/async_api.py index 2c9ecacb..d19228e1 100644 --- a/python/src/camoufox/async_api.py +++ b/python/src/camoufox/async_api.py @@ -106,44 +106,53 @@ async def AsyncNewBrowser( else: virtual_display = None - # Options the caller built leave its contexts to the caller (host_identity). - canvas_noise: Optional[bool] = False - if not from_options: - canvas_noise = kwargs.get('canvas_noise') - # Opt-in; see the note in sync_api.NewBrowser. - kwargs.setdefault('pin_cpu_cores', False) - from_options = await asyncio.get_event_loop().run_in_executor( - None, - partial(launch_options, headless=headless, debug=debug, **kwargs), - ) + # Everything after the display exists can fail (bad proxy, missing binary, + # unsatisfiable constraints) before sync/async_attach_vd wires kill() into + # close, which used to strand one Xvfb and its /tmp/.X lock per failed + # launch. + try: + # Options the caller built leave its contexts to the caller (host_identity). + canvas_noise: Optional[bool] = False + if not from_options: + canvas_noise = kwargs.get('canvas_noise') + # Opt-in; see the note in sync_api.NewBrowser. + kwargs.setdefault('pin_cpu_cores', False) + from_options = await asyncio.get_event_loop().run_in_executor( + None, + partial(launch_options, headless=headless, debug=debug, **kwargs), + ) - # Playwright's default viewport deadlocks Juggler when the window is spoofed - # to a different size (daijro/camoufox#666), so default to no_viewport. - no_viewport_default = spoofs_window_dimensions(from_options) + # Playwright's default viewport deadlocks Juggler when the window is spoofed + # to a different size (daijro/camoufox#666), so default to no_viewport. + no_viewport_default = spoofs_window_dimensions(from_options) - # Pin the driver (and so the browser it is about to spawn) to as many - # cores as the identity reports, so measurable parallelism matches - # navigator.hardwareConcurrency; the driver gets its cores back afterwards. - from . import cpu_affinity - from .utils import driver_pid, pinned_core_count + # Pin the driver (and so the browser it is about to spawn) to as many + # cores as the identity reports, so measurable parallelism matches + # navigator.hardwareConcurrency; the driver gets its cores back afterwards. + from . import cpu_affinity + from .utils import driver_pid, pinned_core_count - pin_to = pinned_core_count(from_options) - pid = driver_pid(playwright) if pin_to else None - if not pid: - return await _launch( - playwright, from_options, persistent_context, no_viewport_default, virtual_display, canvas_noise - ) - # The browser inherits the driver's mask at spawn, so two concurrent launches - # on one driver must not interleave pin/restore: the second pin would land on - # the first browser, and the first restore would leave the driver pinned. - async with _pin_lock(pid): - previous = cpu_affinity.pin(pid, pin_to) - try: + pin_to = pinned_core_count(from_options) + pid = driver_pid(playwright) if pin_to else None + if not pid: return await _launch( playwright, from_options, persistent_context, no_viewport_default, virtual_display, canvas_noise ) - finally: - cpu_affinity.restore(pid, previous) + # The browser inherits the driver's mask at spawn, so two concurrent launches + # on one driver must not interleave pin/restore: the second pin would land on + # the first browser, and the first restore would leave the driver pinned. + async with _pin_lock(pid): + previous = cpu_affinity.pin(pid, pin_to) + try: + return await _launch( + playwright, from_options, persistent_context, no_viewport_default, virtual_display, canvas_noise + ) + finally: + cpu_affinity.restore(pid, previous) + except BaseException: + if virtual_display: + virtual_display.kill() + raise _PIN_LOCKS: Dict[int, asyncio.Lock] = {} diff --git a/python/src/camoufox/sync_api.py b/python/src/camoufox/sync_api.py index af2ba46b..b3101589 100644 --- a/python/src/camoufox/sync_api.py +++ b/python/src/camoufox/sync_api.py @@ -108,47 +108,56 @@ def NewBrowser( else: virtual_display = None - # Options the caller built leave its contexts to the caller (host_identity). - canvas_noise: Optional[bool] = False - if not from_options: - canvas_noise = kwargs.get('canvas_noise') - # Opt-in (2026-09-17). Pinning keeps the identity's core count by - # constraining the browser to that many cores; it costs real CPU, needs - # a launch lock, and does nothing on macOS. What it defends against is a - # page timing N parallel workers, which is expensive and noisy on a busy - # machine. Off, the host's own snapped count is reported, so reported - # and measurable still agree -- the identity just loses that one draw. - kwargs.setdefault('pin_cpu_cores', False) - from_options = launch_options(headless=headless, debug=debug, **kwargs) - - # Playwright's default viewport deadlocks Juggler when the window is spoofed - # to a different size (daijro/camoufox#666), so default to no_viewport. - no_viewport_default = spoofs_window_dimensions(from_options) - - # Pin the driver (and so the browser it is about to spawn) to as many - # cores as the identity reports, so measurable parallelism matches - # navigator.hardwareConcurrency; the driver gets its cores back afterwards. - from . import cpu_affinity - from .utils import driver_pid, pinned_core_count - - pin_to = pinned_core_count(from_options) - pid = driver_pid(playwright) if pin_to else None - previous = cpu_affinity.pin(pid, pin_to) if pid else None + # Everything after the display exists can fail (bad proxy, missing binary, + # unsatisfiable constraints) before sync/async_attach_vd wires kill() into + # close, which used to strand one Xvfb and its /tmp/.X lock per failed + # launch. try: - if persistent_context: - context = playwright.firefox.launch_persistent_context( - **persistent_context_options(from_options, no_viewport_default) - ) - return sync_attach_vd(context, virtual_display) + # Options the caller built leave its contexts to the caller (host_identity). + canvas_noise: Optional[bool] = False + if not from_options: + canvas_noise = kwargs.get('canvas_noise') + # Opt-in (2026-09-17). Pinning keeps the identity's core count by + # constraining the browser to that many cores; it costs real CPU, needs + # a launch lock, and does nothing on macOS. What it defends against is a + # page timing N parallel workers, which is expensive and noisy on a busy + # machine. Off, the host's own snapped count is reported, so reported + # and measurable still agree -- the identity just loses that one draw. + kwargs.setdefault('pin_cpu_cores', False) + from_options = launch_options(headless=headless, debug=debug, **kwargs) - identity = host_identity(from_options, canvas_noise) - browser = playwright.firefox.launch(**from_options) - attach_context_defaults(browser, no_viewport_default) - browser._camoufox_host_identity = identity - return sync_attach_vd(browser, virtual_display) - finally: - if pid: - cpu_affinity.restore(pid, previous) + # Playwright's default viewport deadlocks Juggler when the window is spoofed + # to a different size (daijro/camoufox#666), so default to no_viewport. + no_viewport_default = spoofs_window_dimensions(from_options) + + # Pin the driver (and so the browser it is about to spawn) to as many + # cores as the identity reports, so measurable parallelism matches + # navigator.hardwareConcurrency; the driver gets its cores back afterwards. + from . import cpu_affinity + from .utils import driver_pid, pinned_core_count + + pin_to = pinned_core_count(from_options) + pid = driver_pid(playwright) if pin_to else None + previous = cpu_affinity.pin(pid, pin_to) if pid else None + try: + if persistent_context: + context = playwright.firefox.launch_persistent_context( + **persistent_context_options(from_options, no_viewport_default) + ) + return sync_attach_vd(context, virtual_display) + + identity = host_identity(from_options, canvas_noise) + browser = playwright.firefox.launch(**from_options) + attach_context_defaults(browser, no_viewport_default) + browser._camoufox_host_identity = identity + return sync_attach_vd(browser, virtual_display) + finally: + if pid: + cpu_affinity.restore(pid, previous) + except BaseException: + if virtual_display: + virtual_display.kill() + raise def _resolve_proxy_geo(proxy: Dict[str, str]) -> Tuple[str, str]: diff --git a/python/tests/test_launch_failure_cleanup.py b/python/tests/test_launch_failure_cleanup.py new file mode 100644 index 00000000..f2555e02 --- /dev/null +++ b/python/tests/test_launch_failure_cleanup.py @@ -0,0 +1,70 @@ +"""headless="virtual" must not strand its Xvfb when the launch fails. + +Everything after VirtualDisplay() -- launch_options(), the Playwright launch -- +can raise before sync/async_attach_vd wires kill() into close, which used to +leave one Xvfb and its /tmp/.X lock per failed launch. +""" + +import asyncio + +import pytest + +from camoufox import async_api, sync_api + + +class FakeDisplay: + instances = [] + + def __init__(self, debug=None): + self.killed = False + FakeDisplay.instances.append(self) + + def get(self): + return ":99" + + def kill(self): + self.killed = True + + +class Boom(Exception): + pass + + +def _raise(*args, **kwargs): + raise Boom() + + +@pytest.fixture(autouse=True) +def fake_display(monkeypatch): + FakeDisplay.instances.clear() + monkeypatch.setattr(sync_api, "VirtualDisplay", FakeDisplay) + monkeypatch.setattr(async_api, "VirtualDisplay", FakeDisplay) + + +def test_sync_kills_display_when_launch_options_fails(monkeypatch): + monkeypatch.setattr(sync_api, "launch_options", _raise) + with pytest.raises(Boom): + sync_api.NewBrowser(object(), headless="virtual") + assert FakeDisplay.instances[0].killed + + +def test_async_kills_display_when_launch_options_fails(monkeypatch): + monkeypatch.setattr(async_api, "launch_options", _raise) + with pytest.raises(Boom): + asyncio.run(async_api.AsyncNewBrowser(object(), headless="virtual")) + assert FakeDisplay.instances[0].killed + + +def test_sync_kills_display_when_browser_launch_fails(monkeypatch): + monkeypatch.setattr(sync_api, "launch_options", lambda **kw: {}) + monkeypatch.setattr(sync_api, "host_identity", lambda *a: None) + + class Firefox: + launch = staticmethod(_raise) + + class Playwright: + firefox = Firefox() + + with pytest.raises(Boom): + sync_api.NewBrowser(Playwright(), headless="virtual") + assert FakeDisplay.instances[0].killed