Skip to content

Race condition in SpaceRenderer._map_coordinates for NetworkGrid #3064

@codebreaker32

Description

@codebreaker32

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.

Expected behavior

  • The visualization should be robust against transient race conditions.

  • Valid Agents: Agents with valid node indices should still be mapped and drawn correctly, even if other agents in the same batch are invalid.

  • Invalid Agents: Agents with out-of-bounds indices (waiting for layout updates) should be safely ignored or hidden (e.g., set to NaN) for that single frame, rather than freezing the visualization or rendering garbage data for the entire batch.

Performance: Benchmarking with 5,000 agents shows a negligible overhead of ~0.13ms per frame vs the old unsafe method.

Proposed Logic:

# 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions