Skip to content

Fix race around condition in space_renderer#3065

Closed
codebreaker32 wants to merge 3 commits intomesa:mainfrom
codebreaker32:bug/space_renderer
Closed

Fix race around condition in space_renderer#3065
codebreaker32 wants to merge 3 commits intomesa:mainfrom
codebreaker32:bug/space_renderer

Conversation

@codebreaker32
Copy link
Copy Markdown
Collaborator

Summary

There is a race condition in SpaceRenderer._map_coordinates when rendering NetworkGrid environments. Currently, the method uses contextlib.suppress(IndexError) to handle cases where an agent's node index exceeds the bounds of the cached layout positions (pos).

The current logic is "all-or-nothing." If the simulation adds a new node/agent (e.g., node 100) before the visualization layout refreshes (which only knows nodes 0-99), the IndexError triggers. The suppress block catches this error but aborts the entire coordinate mapping assignment.

Bug / Issue

Fix #3064

Implementation

# Handle race condition 

# 1. Initialize result with NaNs (hidden from plot)
mapped_locs = np.full((len(x), 2), np.nan)

# 2. Identify valid indices that exist in the current layout
if len(pos) > 0:
    valid_mask = (x >= 0) & (x < len(pos))

    # 3. Map only valid agents
    if np.any(valid_mask):
        mapped_locs[valid_mask] = pos[x[valid_mask]]

mapped_arguments["loc"] = mapped_locs

Testing

Benchmarking with 5,000 agents shows a negligible overhead of ~0.13ms per frame vs the old unsafe method.
If you're fixing the visualisation, add before/after screenshots.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Jan 4, 2026

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 +1.3% [+0.5%, +2.1%] 🔵 +1.2% [+1.0%, +1.3%]
BoltzmannWealth large 🔵 +0.3% [-0.3%, +1.0%] 🔵 -0.7% [-4.3%, +2.8%]
Schelling small 🔵 +0.1% [-0.8%, +1.1%] 🔵 +0.2% [-0.4%, +0.7%]
Schelling large 🔵 -1.0% [-1.4%, -0.5%] 🔵 +0.0% [-0.9%, +0.9%]
WolfSheep small 🔵 +1.4% [+1.2%, +1.7%] 🔵 -0.6% [-0.7%, -0.4%]
WolfSheep large 🔵 +1.5% [-3.6%, +7.5%] 🔵 -1.8% [-3.7%, +0.0%]
BoidFlockers small 🔵 +0.5% [+0.1%, +1.0%] 🔵 +1.3% [+1.1%, +1.5%]
BoidFlockers large 🔵 +1.1% [+0.5%, +1.7%] 🔵 +0.6% [+0.3%, +0.8%]

@codebreaker32
Copy link
Copy Markdown
Collaborator Author

Please review @EwoutH @quaquel

@quaquel quaquel added the bug Release notes label label Jan 6, 2026
Copy link
Copy Markdown
Member

@tpike3 tpike3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me...

I will give @Sahil-Chhoker, @EwoutH or @quaquel a few days to review if they want, but I am happy with this solution.

@Sahil-Chhoker
Copy link
Copy Markdown
Collaborator

I'm currently reviewing PR #3045 by @codebyNJ and that feels like the proper solution to this problem, can't say anything now but will let know when the review is over.

@quaquel
Copy link
Copy Markdown
Member

quaquel commented Jan 17, 2026

Can you take a look at the conflicts in space renderer and ensure they are correctly resolved? Once that is done, I'll give a quick last look before merging it.

@codebreaker32
Copy link
Copy Markdown
Collaborator Author

This bug is resolved in #3045 through collaborative effort(see here. Thus closing this PR

@codebreaker32 codebreaker32 deleted the bug/space_renderer branch February 17, 2026 16:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Race condition in SpaceRenderer._map_coordinates for NetworkGrid

4 participants