Skip to content

feat: actionable error when a memray Tracker is already active (pytest-memray interop)#89

Merged
FBumann merged 1 commit into
mainfrom
feat/memray-nested-guard
Jun 15, 2026
Merged

feat: actionable error when a memray Tracker is already active (pytest-memray interop)#89
FBumann merged 1 commit into
mainfrom
feat/memray-nested-guard

Conversation

@FBumann

@FBumann FBumann commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

What

Part 3 of #82 — the pytest-memray interop guard.

memray allows only one Tracker per process. So if pytest-memray's --memray is profiling a test and a benchmem fixture runs on that same test, benchmem's nested Tracker raises memray's terse:

RuntimeError: No more than one Tracker instance can be active at the same time

_track_once now catches that specific case and re-raises with the likely cause and the fix:

pytest-benchmem couldn't start its memray pass because another memray Tracker is already active — most likely pytest-memray's --memray running on the same test. … Run the two on different tests (or different sessions): pytest-memray for whole-test limits/leaks, pytest-benchmem for the benchmarked action's memory.

Scope / rationale

This is deliberately just a message upgrade — memray already fails loudly and safely, so the guard prevents nothing new; it makes the failure self-explanatory for the interop case. It matches the existing _compute_statistics guard, which already translates an internal-memray error into actionable guidance. We considered shipping only the doc caveat (memray's own error isn't terrible) but opted to keep the friendlier message, in keeping with the house style.

  • Matches on memray's wording ("more than one Tracker"); any other RuntimeError propagates unchanged, and if memray changes the text we fall back to its original error (graceful, low-consequence coupling, pinned by the test).

Tests

test_nested_tracker_raises_actionable_error — opens an outer memray.Tracker (standing in for --memray) and asserts measure_memory(...) raises the actionable RuntimeError. Full test_memray.py: 7 passed. ruff format --diff, ruff check, and mypy src + tests all clean (the exact CI gates).

Docs

README "With pytest-memray" caveat now notes benchmem fails that test with an actionable error rather than a terse memray one.

Closes #82.

🤖 Generated with Claude Code

memray allows only one Tracker per process, so running pytest-memray's --memray
and a benchmem fixture on the same test makes benchmem's nested Tracker raise a
terse "No more than one Tracker instance can be active" RuntimeError. _track_once
now catches that specific case and re-raises with the likely cause (pytest-memray
on the same test) and the fix (run them on different tests/sessions). Any other
RuntimeError propagates unchanged; if memray's wording changes we fall back to its
original error. Mirrors the existing _compute_statistics guard's style.

Closes #82 (part 3; parts 1-2 shipped in #86/#87).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@FBumann FBumann enabled auto-merge (squash) June 15, 2026 11:24
@read-the-docs-community

Copy link
Copy Markdown

Documentation build overview

📚 pytest-benchmem | 🛠️ Build #33144698 | 📁 Comparing 414a0d5 against latest (8230ddb)

  🔍 Preview build  

5 files changed · ± 5 modified

± Modified

@FBumann FBumann merged commit bdd24e9 into main Jun 15, 2026
11 checks passed
@FBumann FBumann deleted the feat/memray-nested-guard branch June 15, 2026 11:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Positioning follow-ups: own the action-scoped guardrail, reach the sweep, interop with pytest-memray

1 participant