Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

chore(devex): run pytest boot and collection with GC disabled (#62707)

Julian Bezcd58c02e51bc
diff --git a/.github/workflows/ci-backend.yml b/.github/workflows/ci-backend.yml
index a1b3352f47c1..64cecaa37064 100644
--- a/.github/workflows/ci-backend.yml
+++ b/.github/workflows/ci-backend.yml
@@ -155,6 +155,7 @@ jobs:
- requirements-dev.txt
- mypy.ini
- pytest.ini
+ - conftest.py # Root conftest loads for every pytest run
- .test_durations # Used for pytest-split sharding
# generates posthog/schema.py
- frontend/src/queries/schema.json
@@ -203,6 +204,7 @@ jobs:
- requirements-dev.txt
- mypy.ini
- pytest.ini
+ - conftest.py # Root conftest loads for every pytest run
- .test_durations
- frontend/src/products.json
- 'products/*/manifest.tsx'
diff --git a/.github/workflows/ci-dagster.yml b/.github/workflows/ci-dagster.yml
index 47bf1ccddfc1..1ea54ca8ee18 100644
--- a/.github/workflows/ci-dagster.yml
+++ b/.github/workflows/ci-dagster.yml
@@ -123,6 +123,7 @@ jobs:
- 'posthog/cloud_utils.py'
- 'posthog/ducklake/**'
# Test infrastructure used by dagster tests
+ - conftest.py
- 'posthog/conftest.py'
- 'posthog/test/**'
# --- ee modules that DAGs import ---
diff --git a/.github/workflows/ci-python.yml b/.github/workflows/ci-python.yml
index 5dfe29bf0531..9783306ddead 100644
--- a/.github/workflows/ci-python.yml
+++ b/.github/workflows/ci-python.yml
@@ -45,6 +45,7 @@ jobs:
python:
- 'pyproject.toml'
- 'uv.lock'
+ - conftest.py # Root conftest loads for every pytest run
- 'ee/**/*.py'
- 'posthog/**/*'
- 'products/**/*.py'
diff --git a/conftest.py b/conftest.py
new file mode 100644
index 000000000000..2cfa52e12086
--- /dev/null
+++ b/conftest.py
@@ -0,0 +1,49 @@
+import gc
+import warnings
+
+import pytest
+
+# Test-session boot — plugin imports and importing every collected test module —
+# allocates almost exclusively permanent objects, so automatic cyclic GC during that
+# phase only adds pauses (seconds on a full-tree CI shard collection). Run the boot
+# with GC off, then freeze the survivors into the permanent generation so the
+# collector never rescans them during the test phase. Tests themselves run with GC
+# enabled as usual. (django.setup() runs in pytest-django's load_initial_conftests,
+# before conftest files load, so it stays outside the window.)
+gc.disable()
+
+
+def _end_gc_boot_window() -> None:
+ if gc.isenabled():
+ return
+ # Deliberately no gc.collect() before the freeze: sweeping the boot garbage
+ # (including items deselected by pytest-split sharding — measured at ~7k cyclic
+ # objects for a 5-way shard of posthog/api/test) costs ~0.5s per invocation but
+ # reclaims only ~1MB, so the garbage gets frozen along with the survivors.
+ gc.freeze()
+ gc.enable()
+ # gc.get_referrers() cannot see referrers in the frozen permanent generation,
+ # which turns hypothesis's register_random() liveness check into a false positive
+ # for Randoms registered after the freeze (e.g. trio's module-level instance,
+ # registered when hypothesis is first imported). Refcounts are unaffected, so the
+ # ReferenceError path for real misuse still works; only the warning is spurious.
+ warnings.filterwarnings("ignore", message=r"It looks like `register_random` was passed")
+
+
+def pytest_collection_finish() -> None:
+ _end_gc_boot_window()
+
+
[email protected](tryfirst=True)
+def pytest_runtestloop() -> None:
+ # Safety net for processes that never run a local collection (e.g. the
+ # pytest-xdist controller): end the window before the test loop starts.
+ _end_gc_boot_window()
+
+
+def pytest_unconfigure() -> None:
+ # Frozen objects skip the final cyclic collections of interpreter shutdown, so their
+ # finalizers run in the late teardown phase where extension modules may already be
+ # gone — observed as exit code 139 (SIGSEGV) on the Temporal CI shards. Restore the
+ # default heap state so shutdown behaves exactly as without the boot window.
+ gc.unfreeze()
diff --git a/pyproject.toml b/pyproject.toml
index 74b54a836bf6..ba71a728b56f 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -434,7 +434,9 @@ warn_unreachable = true
strict_equality = true
ignore_missing_imports = true
exclude_gitignore = true
-exclude = ["^(bin/.*|tools/.*|products/[^/]+/skills/[^/]+/scripts/.*|gunicorn\\.config\\.py|manage\\.py)$"]
+# conftest.py: the root pytest bootstrap collides with services/stripe-mock/tests/conftest.py
+# (both resolve to top-level module "conftest")
+exclude = ["^(bin/.*|tools/.*|products/[^/]+/skills/[^/]+/scripts/.*|gunicorn\\.config\\.py|manage\\.py|conftest\\.py)$"]
[[tool.mypy.overrides]]
module = "bin.*"