Fix network restore loop#1815
Conversation
6b4d6e8 to
de1a8ce
Compare
|
Hi @Utkarsha2811, are you still interested in/have time for working on this? |
|
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. |
|
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 |
|
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! |
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().