Skip to content

Fix _get_hexmesh mutable cache return and wrong type hint - #3858

Merged
quaquel merged 1 commit into
mesa:mainfrom
yashvisharma1204:fix/hexmesh-cache-immutable-return
Sep 14, 2026
Merged

quaquel merged 1 commit into
mesa:mainfrom
yashvisharma1204:fix/hexmesh-cache-immutable-return

Conversation

@yashvisharma1204

Copy link
Copy Markdown
Contributor

Note

Use this template for bug fixes only. For enhancements/new features, use the feature template and get maintainer approval in an issue/discussion before opening a PR.

Pre-PR Checklist

  • This PR is a bug fix, not a new feature or enhancement.

Summary

_get_hexmesh in mesa/visualization/mpl_space_drawing.py is decorated with @lru_cache but returns a mutable list. Because lru_cache stores the return value by reference, every call with the same (width, height, size) returns the same list object — so a caller mutating it would poison the cache for every other same-sized grid. This PR makes the function return immutable nested tuples and corrects the wrong return type annotation and docstring. No behavior change for existing callers.

Bug / Issue

Fixes #3857

The cached function hands out a shared mutable object. Minimal reproduction:

from mesa.visualization.mpl_space_drawing import _get_hexmesh

a = _get_hexmesh(3, 3)
b = _get_hexmesh(3, 3)
assert a is b            # same cached object

a.append("CORRUPTED")    # a caller mutates its result
c = _get_hexmesh(3, 3)   # fresh lookup, same dimensions
assert "CORRUPTED" in c  # cache is now poisoned

Expected: a cached function must not let callers corrupt the cache. Actual: the returned list is shared and mutable, so any in-place mutation persists across calls.

Two related defects in the same function:

  • Return annotation was list[tuple[float, float]], but it actually returns list[list[tuple[float, float]]] (a list of hexagons, each six vertex tuples).
  • Docstring said "Yields…" for a function that returns.

This is currently latent — both existing call sites only iterate the result (one passes it to PolyCollection, the other builds edges) — so it's a robustness/typing fix, not a user-facing regression.

Implementation

  • Return nested immutable tuples (tuple[tuple[tuple[float, float], ...], ...]) so the cached object cannot be mutated in place.
  • Correct the return type annotation to match the true nested shape (consistent with SpaceDrawer._get_hexmesh in space_drawers.py, which was already correct).
  • Fix the docstring ("Yields…" → "Returns one entry per hexagon…", noting why the result is immutable).

Testing

  • Added regression test test_get_hexmesh_returns_immutable_shared_safe_mesh asserting the mesh shape (12 hexagons × 6 vertices × 2 coords for a 3×4 grid), that repeated calls return the same cached object, and
    that the result cannot be mutated (append → AttributeError, item assignment → TypeError).
  • ruff check passes on both changed files.
  • Existing hex-grid drawing tests still pass, confirming PolyCollection and edge construction work unchanged with tuples.
  • Full non-browser test suite: 804 passed, no regressions.

Additional Notes

@github-actions github-actions Bot added testing Release notes label visualization labels Sep 13, 2026
@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 +0.4% [-0.0%, +0.9%] 🔵 +2.8% [+2.6%, +3.0%]
BoltzmannWealth large 🔵 -1.6% [-2.2%, -1.0%] 🔵 +0.5% [-1.6%, +2.4%]
Schelling small 🔵 +2.0% [+1.3%, +2.6%] 🔵 +1.6% [+0.8%, +2.5%]
Schelling large 🔵 +2.6% [+2.0%, +3.1%] 🔴 +9.6% [+8.5%, +10.7%]
WolfSheep small 🔵 +1.7% [+1.1%, +2.3%] 🔵 +0.5% [-3.1%, +4.3%]
WolfSheep large 🔵 +3.6% [+2.3%, +5.1%] 🔴 +11.5% [+9.0%, +13.8%]
SugarscapeG1mt small 🔵 +1.3% [+0.8%, +1.8%] 🔵 +3.4% [+1.7%, +5.2%]
SugarscapeG1mt large 🔵 +0.5% [-0.7%, +1.7%] 🔵 +1.3% [-0.9%, +3.6%]
BoidFlockers small 🔵 +2.1% [+1.8%, +2.5%] 🔵 -0.6% [-1.1%, -0.2%]
BoidFlockers large 🔵 +2.1% [+1.6%, +2.5%] 🔵 +0.5% [+0.3%, +0.8%]
MultiLevelAllianceModel small 🔴 +8.2% [+7.2%, +9.3%] 🔵 +1.4% [+1.0%, +1.8%]
MultiLevelAllianceModel large 🔵 +1.0% [-0.2%, +2.2%] 🔴 +5.8% [+3.2%, +8.2%]

@quaquel quaquel added the bug Release notes label label Sep 14, 2026
@quaquel
quaquel merged commit e5245c8 into mesa:main Sep 14, 2026
16 of 17 checks passed
EwoutH pushed a commit that referenced this pull request Oct 28, 2026
(cherry picked from commit e5245c8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Release notes label testing Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Mutable shared return value from cached _get_hexmesh in mpl_space_drawing

2 participants