Files
archived-camoufox/python/tests/test_launch_failure_cleanup.py
T
coffeegrind123andClaude Opus 5.5 0618f75ecb 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>
2026-10-18 09:53:24 +03:00

71 lines
1.9 KiB
Python

"""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<n> 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