mirror of
https://github.com/daijro/camoufox.git
synced 2026-11-19 00:07:28 +00:00
Stop the virtual display when a Python launch fails
With headless="virtual", NewBrowser and AsyncNewBrowser start Xvfb first, and only sync/async_attach_vd ties its kill() to closing the browser. Anything that raised in between -- launch_options() on a bad proxy or unsatisfiable constraints, the Playwright launch on a missing binary -- left one Xvfb and its /tmp/.X<n> lock behind per failed launch. Both now kill the display on any exception before re-raising, as the TypeScript launcher already does. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
18604faea2
commit
0618f75ecb
@@ -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<n> 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] = {}
|
||||
|
||||
@@ -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<n> 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]:
|
||||
|
||||
Reference in New Issue
Block a user