tests: a closed context is checked for the processes of its sites, not for every content process

test_closed_contexts_with_a_never_stopping_service_worker_leave_no_process was red on the e2e of the 0.50.0
tag ([2, 2, 2, 5, 5], 5 <= 2). Measured on firefox-62 (Linux), the test's own scenario 10 times with every
process named: no "Isolated Web Co" left after any close; the 3 more are "Web Content", the engine's pool of
preallocated processes filling up behind the closed contexts (5 runs of 10, new pids each time). Not a leak:
the count read the refill.

The helper of the bench moves to tests/_processes.py and both tests use it. The context test counts the
processes that host a site: at least one while the page is open (or the count proves nothing), none a few
seconds after `close` answers (the engine waits for them inside close and kills the ones that stay). Off
Linux it skips, as the bench's two counting tests do: no other system names the process.

Unit tests (tests/unit/test_processes_counts_sites.py) from the measured processes, the closed-context case
included. e2e on firefox-62 (candidate #6, Linux): the context test 10 of 10; run_e2e.py on the two files and
the unit test 18 passed. The old counter, same scenario: 5 of 10 runs would fail.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
feder-crandClaude Opus 5.5 committed 2026-11-11 13:40:29 +01:00
1 parent 5402da4d80
commit ff2123ef58
4 files changed
+99 -80

No files matched your search

+55
View File
@@ -0,0 +1,55 @@
"""Which of the engine's processes host a site: what the e2e tests that count processes read.
A content process is not a site's by being new. The engine keeps preallocated content processes
(`dom.ipc.processPrelaunch.fission.number`, 3), hands one to a site when a site needs a process, and starts
another to refill the pool whenever it gets to it, so a pid that appears between two reads is often that refill.
On Linux the process says which it is: `ContentChild::SetProcessName` names it by its remote type with
`prctl(PR_SET_NAME)` (ipc/glue/ProcessUtils_linux.cpp), "Isolated Web Co" once it hosts a site of strategy 1
(`webIsolated=<site>`), "Web Content" while it is preallocated. Windows names no process
(ProcessUtils_none.cpp): a test that counts sites skips there and says so.
Measured on firefox-62 (Linux): a cross-site frame renames one preallocated process and a new "Web Content"
refills the pool; a same-site frame changes neither; a closed context leaves no "Isolated Web Co", while the
pool of "Web Content" fills up behind it in about half the runs.
"""
from __future__ import annotations
import os
import sys
import pytest
#: The name a content process of strategy 1 takes once it hosts a site (PR_SET_NAME keeps 15 characters of
#: "Isolated Web Content"). A preallocated one is "Web Content" until a site is given to it.
SITE_PROCESS_NAME = "Isolated Web Co"
def site_pids(processes, engine_dir=None):
"""The pids, among `processes` (psutil-like), of the content processes that host a site; with `engine_dir`,
only those of the engine in that directory."""
import psutil
pids = set()
for proc in processes:
try:
if engine_dir is not None and not os.path.normcase(proc.exe()).startswith(os.path.normcase(engine_dir)):
continue
name, cmd = proc.name(), proc.cmdline()
except psutil.Error:
continue
if "-contentproc" in cmd and cmd and cmd[-1] == "tab" and name == SITE_PROCESS_NAME:
pids.add(proc.pid)
return pids
def site_processes_below(root_pid, engine_dir=None):
"""The pids of the processes below `root_pid` that host a site. Off Linux the test skips: no name says it."""
import psutil
if not sys.platform.startswith("linux"):
pytest.skip("only Linux names a content process by the site it hosts; %s names none" % sys.platform)
try:
children = psutil.Process(root_pid).children(recursive=True)
except psutil.Error:
return set()
return site_pids(children, engine_dir)
@@ -7,10 +7,13 @@ four such contexts left four more processes (8 web content processes at the end
BrowserContext now waits a few seconds for the processes of its container once its pages are gone, and kills the
ones that stay.
The test closes five such contexts in a row and counts the web content processes under the engine after each.
The first context may add ONE process that is not the leak (the engine starts a spare content process when the
first page takes one, and Linux keeps it), so the count after the fifth must not be above the count after the
SECOND: a flat line from there on (on the old engine every context adds one: [4, 5, 6, 7, 8]).
The test closes five such contexts in a row. While each page is open it counts the processes that host a site
(at least one: the page's, or the count would prove nothing), and once `close` has answered it waits a few
seconds for them to be gone: none may be left (on the old engine every context leaves its own, one more each
time). Only the processes that host a site are counted (tests/_processes.py): the engine refills its pool of
preallocated content processes behind a closed context, in about half the runs, and counting every content
process read that refill as a leak ([2, 2, 2, 5, 5] on the e2e of the 0.50.0 commit, three "Web Content" and no
site left). Off Linux the test skips: no other system names the process.
The engine that does it is not published when this file is written: `engine_at_least` makes the test wait for
it, and on an older engine it is skipped, never passed.
@@ -27,6 +30,7 @@ import pytest
pytest.importorskip("websockets")
from websockets.asyncio.client import connect # noqa: E402
from _processes import site_processes_below # noqa: E402
from _servers import LocalServer # noqa: E402
pytestmark = pytest.mark.e2e
@@ -59,24 +63,6 @@ def _serve():
return server, "http://127.0.0.1:%d" % server.server_address[1]
def _web_processes(root_pid):
import psutil
found = set()
try:
children = psutil.Process(root_pid).children(recursive=True)
except psutil.Error:
return found
for process in children:
try:
command = " ".join(process.cmdline())
except psutil.Error:
continue
if "-contentproc" in command and command.rstrip().endswith(" tab"):
found.add(process.pid)
return found
class _Client:
"""The few calls of Playwright's protocol this test needs, over the served WebSocket."""
@@ -114,8 +100,11 @@ def test_closed_contexts_with_a_never_stopping_service_worker_leave_no_process(f
launched = launch_server(host="127.0.0.1", port=0, headless=True, virtual_display=False, humanize=False,
seed=4266, binary_path=firefox_binary, launch_timeout_ms=180_000)
def sites():
return site_processes_below(launched.process.pid)
async def drive():
counts = []
opened, left = [], []
async with connect(launched.ws_endpoint, max_size=None) as ws:
client = _Client(ws)
await client.start()
@@ -123,21 +112,18 @@ def test_closed_contexts_with_a_never_stopping_service_worker_leave_no_process(f
context, frame = await client.new_page()
await client.call(frame, "goto", {"url": "%s/?i=%d" % (base, i), "waitUntil": "load", "timeout": 30000})
await asyncio.sleep(1.0) # the worker is registered, its install loop is running
await client.call(context, "close")
deadline = time.time() + 10
settled = None
while time.time() < deadline: # the count after the close, once it stops moving
now = _web_processes(launched.process.pid)
if settled is not None and len(now) == settled:
break
settled = len(now)
await asyncio.sleep(1.0)
counts.append(len(_web_processes(launched.process.pid)))
return counts
opened.append(len(sites()))
await client.call(context, "close") # the engine waits for the context's processes, then kills
deadline = time.time() + 5
while sites() and time.time() < deadline:
await asyncio.sleep(0.25)
left.append(len(sites()))
return opened, left
try:
counts = asyncio.run(drive())
opened, left = asyncio.run(drive())
finally:
launched.close()
server.shutdown()
assert counts[4] <= counts[1], "web content processes after each closed context: %r" % (counts,)
assert all(opened), "a page with no process for its site: the count proves nothing (%r)" % (opened,)
assert left == [0] * 5, "processes of a site left after each closed context: %r" % (left,)
+7 -37
View File
@@ -21,20 +21,15 @@ test says what the product does not do yet and the step that makes it green:
9 named world after the OOP frame step 1a + 2b
10 console of the OOP frame step 1a
The oracle for "another process" is the set of the engine's processes that host a site, below the test: a
cross-site frame adds at least one, a same-site one adds none. A content process is not one of them by being
new: the engine keeps preallocated content processes (`dom.ipc.processPrelaunch.fission.number`), hands one to
a site when a site needs a process, and starts another to refill the pool, whenever it gets to it. A pid that
appears is often that refill, not a site. On Linux the process says which it is: `ContentChild::SetProcessName`
names it by its remote type with `prctl(PR_SET_NAME)` (ipc/glue/ProcessUtils_linux.cpp), "Isolated Web Co" for
a site of strategy 1 (`webIsolated=<site>`), "Web Content" while it is preallocated. Windows names no process
(ProcessUtils_none.cpp), so the two tests that count processes skip there. The set is read once it is quiet.
The oracle for "another process" is the set of the engine's processes that host a site, below the test
(tests/_processes.py says how a site's process is told from a preallocated one): a cross-site frame adds at
least one, a same-site one changes nothing. The set is read once it is quiet. Off Linux the two tests that
count processes skip: no other system names the process.
"""
from __future__ import annotations
import json
import os
import sys
import threading
import time
from http.server import BaseHTTPRequestHandler
@@ -43,6 +38,7 @@ from urllib.parse import parse_qs, urlsplit
import pytest
from _processes import site_processes_below
from _servers import LocalServer
FISSION_ON = {
@@ -131,35 +127,9 @@ def engine_dir(firefox_binary):
return str(Path(firefox_binary).resolve().parent)
#: The name a content process of strategy 1 takes once it hosts a site (15 characters: PR_SET_NAME keeps 15 of
#: "Isolated Web Content"). A preallocated one is "Web Content" until a site is given to it.
SITE_PROCESS_NAME = "Isolated Web Co"
def site_pids(processes, engine_dir):
"""The pids of the processes of the engine, among `processes` (psutil-like), that host a site."""
import psutil
pids = set()
for proc in processes:
try:
if not os.path.normcase(proc.exe()).startswith(os.path.normcase(engine_dir)):
continue
name, cmd = proc.name(), proc.cmdline()
except psutil.Error:
continue
if "-contentproc" in cmd and cmd[-1] == "tab" and name == SITE_PROCESS_NAME:
pids.add(proc.pid)
return pids
def site_processes(engine_dir):
"""The pids of the engine's processes below this test that host a site (see the module's docstring)."""
import psutil
if not sys.platform.startswith("linux"):
pytest.skip("only Linux names a content process by the site it hosts; %s names none" % sys.platform)
return site_pids(psutil.Process(os.getpid()).children(recursive=True), engine_dir)
"""The pids of the engine's processes below this test that host a site (tests/_processes.py)."""
return site_processes_below(os.getpid(), engine_dir)
def quiet(engine_dir, seconds=3.0, limit=30.0):
@@ -1,4 +1,4 @@
"""The Fission bench counts the processes that host a site, not every content process that is new. No browser.
"""The e2e tests that count processes count the ones that host a site, not every new content process. No browser.
The engine keeps preallocated content processes and starts one to refill the pool after it hands one to a site,
whenever it gets to it. The bench counted every `-contentproc ... tab` pid, so a refill that landed between its
@@ -6,10 +6,9 @@ two reads was "a process the same-site frame started" (test_same_site_other_port
e2e of the commit of 0.50.0: `[13443]`). The processes below are the ones measured on firefox-62 (Linux):
names and command lines as /proc gives them.
"""
import _processes
import pytest
import test_fission_strategy1_bench as bench
pytestmark = pytest.mark.unit
ENGINE = "/cache/firefox-62/firefox"
@@ -40,8 +39,8 @@ def _engine_at_plain():
def test_a_preallocated_process_that_arrives_is_not_a_site():
before = bench.site_pids(_engine_at_plain(), DIR)
after = bench.site_pids(_engine_at_plain() + [_Proc(549, "Web Content", "tab")], DIR)
before = _processes.site_pids(_engine_at_plain(), DIR)
after = _processes.site_pids(_engine_at_plain() + [_Proc(549, "Web Content", "tab")], DIR)
assert before == after == {334}
@@ -49,9 +48,18 @@ def test_a_preallocated_process_given_to_a_site_is_a_new_site_process():
"""A cross-site frame: 433 leaves the pool for b.test (and is renamed), 549 refills the pool."""
procs = [p for p in _engine_at_plain() if p.pid != 433]
procs += [_Proc(433, "Isolated Web Co", "tab"), _Proc(549, "Web Content", "tab")]
assert bench.site_pids(procs, DIR) - bench.site_pids(_engine_at_plain(), DIR) == {433}
assert _processes.site_pids(procs, DIR) - _processes.site_pids(_engine_at_plain(), DIR) == {433}
def test_a_process_of_another_firefox_is_not_counted():
other = _Proc(900, "Isolated Web Co", "tab", exe="/usr/lib/firefox/firefox")
assert bench.site_pids(_engine_at_plain() + [other], DIR) == {334}
assert _processes.site_pids(_engine_at_plain() + [other], DIR) == {334}
def test_the_pool_refilled_behind_a_closed_context_is_not_a_process_left():
"""After the fourth closed context of test_a_context_that_ends_leaves_no_process_e2e, as measured: three
preallocated processes and no site. Counting every content process said 5 ([2, 2, 2, 5, 5])."""
left = [_Proc(1219, "WebExtensions", "tab"), _Proc(1382, "Privileged Cont", "tab"),
_Proc(1739, "Web Content", "tab"), _Proc(1744, "Web Content", "tab"), _Proc(1821, "Web Content", "tab")]
assert _processes.site_pids(left) == set()
assert _processes.site_pids(left + [_Proc(1900, "Isolated Web Co", "tab")]) == {1900}