Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- An example started from the test suite draws with matplotlib's Agg backend on any machine. The
test run set no `MPLBACKEND`, so on a machine with a display matplotlib picked TkAgg, and
`06_mnist_tutorial` and the MixUp/CutMix tutorial aborted on `Tcl_AsyncDelete: async handler
deleted by the wrong thread` in some runs; CI, with no display, never met it. `[tool.pytest_env]`
now sets `MPLBACKEND = "Agg"` unless the caller set one, as substrax, calibrax, receptifex and
seismifex do, and `run_python` hands it to every example's process. With a display, both
examples passed three runs of three.
- The inherited-CUDA guard's test (`tests/pipeline/test_host_worker_setup.py::`
`TestWorkerSetUpPositiveControls`) checks what it states. Its script set `jax_platforms` to the
CPU at module level, which every spawned worker re-runs, so no worker saw the inherited
Expand Down
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,10 @@ testpaths = ["tests"]
# D: prefix = set as default only (don't override existing env vars)
JAX_ENABLE_X64 = {skip_if_set = true, value = "0"}
JAX_SKIP_CUDA_CONSTRAINTS_CHECK = {skip_if_set = true, value = "1"}
# Agg draws off screen, as substrax, calibrax, receptifex and seismifex set it. With a display and
# no backend named, matplotlib picks an interactive one (TkAgg), and an example started from a test
# inherits it through run_python and aborts when a worker thread tears a Tk object down.
MPLBACKEND = {skip_if_set = true, value = "Agg"}
# Ray 2.53's uv runtime_env hook detects uv-managed venvs and tries to
# recreate them for workers — but the new venv lacks ray itself, causing
# workers to crash silently and from_numpy()/iter_batches() to hang.
Expand Down
29 changes: 29 additions & 0 deletions tests/examples/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import platform
import re
import subprocess
import tomllib
from pathlib import Path

import pytest
Expand Down Expand Up @@ -152,6 +153,34 @@ def test_a_run_that_succeeded_is_never_unavailable(self) -> None:
assert unavailable_reason(0, "429 Client Error: retried and succeeded") is None


class TestExampleBackend:
"""An example started from a test draws off screen, whatever display the machine has."""

def test_an_example_process_gets_the_test_runs_matplotlib_backend(self, tmp_path: Path) -> None:
"""The child inherits the test process's ``MPLBACKEND``, never the desktop's default.

With a display (``DISPLAY`` set) and no ``MPLBACKEND``, matplotlib picks an interactive
backend such as TkAgg, whose objects are torn down from a worker thread and abort the
example (``Tcl_AsyncDelete: async handler deleted by the wrong thread``). pytest-env sets
``MPLBACKEND`` for the test run, unless the caller set one, and ``run_python`` hands the
child this process's environment.
"""
script = tmp_path / "backend.py"
script.write_text("import os\nprint(os.environ.get('MPLBACKEND'))\n")

result = run_python(script, timeout=60, env={"DISPLAY": ":0"}).check()

assert os.environ.get("MPLBACKEND") is not None
assert result.stdout.strip() == os.environ["MPLBACKEND"]

def test_the_test_run_defaults_to_agg(self) -> None:
"""Agg, matplotlib's non-interactive backend, is the default a caller can override."""
with open(REPO_ROOT / "pyproject.toml", "rb") as file:
pytest_env = tomllib.load(file)["tool"]["pytest_env"]

assert pytest_env["MPLBACKEND"] == {"skip_if_set": True, "value": "Agg"}


@pytest.mark.slow
class TestExampleExecution:
"""Tests that execute example files.
Expand Down
Loading