Skip to content

Fix network restore loop#1815

Draft
Utkarsha2811 wants to merge 4 commits into
brian-team:masterfrom
Utkarsha2811:fix-network-restore-loop
Draft

Fix network restore loop#1815
Utkarsha2811 wants to merge 4 commits into
brian-team:masterfrom
Utkarsha2811:fix-network-restore-loop

Conversation

@Utkarsha2811

Copy link
Copy Markdown
Contributor

Fixes #1814

This PR fixes two bugs that happen when restoring Brian2 networks in a loop. First, I added a gc.collect() call inside start_scope() to fix the KeyError that crashes Network.restore(). This forces Python to clean up old objects from the previous loop iteration, which stops the internal registry from keeping their names and incorrectly bumping the name counters (e.g. G_run_regularly_clock_1). Second, I fixed the spurious "unused object" warning by updating Network.add() to set obj._network immediately. This ensures that objects are correctly marked as part of the network right away, even if the user is only restoring a state and hasn't explicitly called Network.run().

@Utkarsha2811 Utkarsha2811 marked this pull request as draft April 24, 2026 09:24
@Utkarsha2811 Utkarsha2811 force-pushed the fix-network-restore-loop branch from 6b4d6e8 to de1a8ce Compare April 24, 2026 17:43
@mstimberg

Copy link
Copy Markdown
Member

Hi @Utkarsha2811, are you still interested in/have time for working on this?

@Utkarsha2811

Copy link
Copy Markdown
Contributor Author

Hi @mstimberg,

Yes, I’m still interested. I got busy with university for a while, but I have time now and I’m planning to work on this today. I’ll update the PR as I make progress.

@mstimberg

Copy link
Copy Markdown
Member

Hi @Utkarsha2811, great to hear you are still interested in working on this. Could you please install/run the pre-commit hook we use to make sure code follows our style guideline: https://brian2.readthedocs.io/en/stable/developer/guidelines/style.html#code-style
This is the first step of our test suite on GitHub, so if it fails the actual tests will never run.

@Utkarsha2811

Copy link
Copy Markdown
Contributor Author

Hi @mstimberg , I have installed pre-commit locally and run the formatting checks using ruff. The code has been formatted to match the style guidelines and I've pushed the updates to the branch. The CI checks should now pass. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug Report: Network.restore() fails with KeyError when multiple models are created and restored in a loop; spurious "unused object" warnings

2 participants