Skip to content

Fix pickling for scheduled events by serializing weak-referenced callbacks safely#3205

Merged
EwoutH merged 2 commits intomesa:mainfrom
EwoutH:simulation_event_pickling
Jan 25, 2026
Merged

Fix pickling for scheduled events by serializing weak-referenced callbacks safely#3205
EwoutH merged 2 commits intomesa:mainfrom
EwoutH:simulation_event_pickling

Conversation

@EwoutH
Copy link
Copy Markdown
Member

@EwoutH EwoutH commented Jan 24, 2026

Small cherry-pick from #3204, to keep that PR atomic.

This PR adds __getstate__/__setstate__ to the event object in mesa.experimental.devs.eventlist to make it pickle-friendly when the callback is stored as a weak reference. During pickling we materialize the weak reference into a temporary strong reference (_fn_strong) and omit the weakref itself, and during unpickling we restore the callback by recreating the appropriate weak reference (WeakMethod for bound methods, ref for functions) or leaving it as None when absent.

@EwoutH EwoutH requested a review from quaquel January 24, 2026 19:47
@EwoutH EwoutH added the maintenance Release notes label label Jan 24, 2026
@github-actions
Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -0.9% [-1.4%, -0.4%] 🔵 -0.8% [-1.0%, -0.7%]
BoltzmannWealth large 🔵 +3.5% [-6.0%, +12.8%] 🔵 -3.0% [-6.9%, +0.7%]
Schelling small 🔵 -0.4% [-0.8%, +0.1%] 🔵 +0.5% [+0.3%, +0.7%]
Schelling large 🔵 +3.9% [-2.4%, +10.2%] 🔵 +4.7% [+2.1%, +7.3%]
WolfSheep small 🔵 +4.6% [+1.7%, +7.5%] 🔵 +0.6% [+0.2%, +1.0%]
WolfSheep large 🔵 +8.1% [-7.9%, +24.4%] 🔴 +8.8% [+6.8%, +10.9%]
BoidFlockers small 🔴 +4.7% [+4.3%, +5.1%] 🔵 -1.3% [-1.5%, -1.0%]
BoidFlockers large 🔴 +5.8% [+5.1%, +6.5%] 🔵 -0.4% [-0.8%, -0.2%]

@EwoutH EwoutH force-pushed the simulation_event_pickling branch from ea7fd49 to b0378a6 Compare January 25, 2026 08:50
@EwoutH
Copy link
Copy Markdown
Member Author

EwoutH commented Jan 25, 2026

@quaquel rebased and added tests.

@EwoutH EwoutH merged commit 5895a66 into mesa:main Jan 25, 2026
13 of 14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maintenance Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants