Skip to content

fix: FixedCell setter atomically adds agent before updating reference (fixes #3411)#3415

Merged
quaquel merged 5 commits intomesa:mainfrom
Rishav23av:main
Mar 2, 2026
Merged

fix: FixedCell setter atomically adds agent before updating reference (fixes #3411)#3415
quaquel merged 5 commits intomesa:mainfrom
Rishav23av:main

Conversation

@Rishav23av
Copy link
Copy Markdown
Contributor

Problem

FixedAgent kept a dangling reference to a cell that rejected it due to capacity.
After a failed cell.add_agent(), agent.cell was set but the cell had no record of the agent.

Fix

Swap the order in FixedCell.cell.setter — call cell.add_agent(self) FIRST,
then update self._mesa_cell only if it succeeds. This is the same atomic
pattern already used in HasCell (PR #3374).

Fixes #3411

self._mesa_cell = cell

def cell(self, cell: Cell | None) -> None:
if self._mesa_cell is not None and cell is not self._mesa_cell:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This still allows reassigning the same cell. I would just block any assignment after self._mesa_cell is no longer None.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

simplified it just blocks any assignment once _mesa_cell is set

@quaquel
Copy link
Copy Markdown
Member

quaquel commented Mar 2, 2026

There is an indentation error. Can you fix that? As a general rule, I suggest running all tests locally before committing changes.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Mar 2, 2026

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -3.5% [-4.0%, -2.8%] 🔵 -3.4% [-3.9%, -2.8%]
BoltzmannWealth large 🔵 -3.2% [-3.9%, -2.6%] 🔵 +2.0% [+0.1%, +3.5%]
Schelling small 🔵 -1.1% [-1.5%, -0.7%] 🔵 +0.5% [+0.3%, +0.7%]
Schelling large 🔵 -0.2% [-0.9%, +0.4%] 🔵 +3.8% [+2.4%, +5.4%]
WolfSheep small 🔵 +0.0% [-0.3%, +0.4%] 🔵 -1.0% [-1.1%, -0.8%]
WolfSheep large 🔵 +0.6% [-0.1%, +1.2%] 🔵 +1.6% [+0.2%, +2.9%]
BoidFlockers small 🔵 +0.2% [-0.2%, +0.5%] 🔵 +0.6% [+0.4%, +0.8%]
BoidFlockers large 🔵 -0.3% [-0.5%, +0.0%] 🔵 +0.4% [+0.1%, +0.6%]

@Rishav23av
Copy link
Copy Markdown
Contributor Author

Sorry about that Fixed the indentation and ran tests locally before pushing this time

@Rishav23av
Copy link
Copy Markdown
Contributor Author

@quaquel all done

@quaquel quaquel merged commit 4907ed3 into mesa:main Mar 2, 2026
12 checks passed
@EwoutH EwoutH added the bug Release notes label label Mar 13, 2026
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.

[BUG] FixedAgent keeps a reference to a cell that may reject it

3 participants