Skip to content

Fix deepcopy RecursionError on World objects - #359

Open
toruseo with Copilot wants to merge 4 commits into
mainfrom
copilot/fix-failure-of-run-notebook-demos-extra-test
Open

Fix deepcopy RecursionError on World objects#359
toruseo with Copilot wants to merge 4 commits into
mainfrom
copilot/fix-failure-of-run-notebook-demos-extra-test

Conversation

Copilot AI commented Aug 28, 2026

Copy link
Copy Markdown
Contributor

The run-notebook-demos-extra CI test fails with RecursionError: maximum recursion depth exceeded when demo_notebook_03en_pytorch calls copy.deepcopy(env.W) to save the best World state during RL training. The deeply nested object graph (World ↔ Node ↔ Link ↔ Vehicle chains) exceeds Python's default recursion limit of 1000 during recursive deepcopy traversal. The failure started from a dependency version bump (not a code change) that pushed the required depth past the limit.

  • Add World.__deepcopy__ that temporarily raises sys.setrecursionlimit proportional to the number of simulation objects before delegating to standard deepcopy logic, restoring the original limit in finally
# Previously fails on large networks with default recursion limit
import copy
W2 = copy.deepcopy(W)  # RecursionError

# Now works — __deepcopy__ scales the limit to match the object graph
W2 = copy.deepcopy(W)  # OK, even with sys.setrecursionlimit(300)

Copilot AI linked an issue Aug 28, 2026 that may be closed by this pull request
Copilot AI and others added 2 commits August 28, 2026 07:18
…ethod

The default recursive copy.deepcopy can exceed the interpreter's
recursion limit on large networks because the object graph
(World - Node - Link - Vehicle chains) is very deep. The
__deepcopy__ method temporarily raises sys.setrecursionlimit
proportional to the number of simulation objects before performing
the standard deepcopy logic.

Closes #358

Co-authored-by: toruseo <34780089+toruseo@users.noreply.github.com>
Co-authored-by: toruseo <34780089+toruseo@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix failure of run-notebook-demos-extra test due to recursion error Fix deepcopy RecursionError on World objects Aug 28, 2026
Copilot AI requested a review from toruseo August 28, 2026 07:20
@toruseo
toruseo marked this pull request as ready for review August 28, 2026 07:21
Refactor deepcopy method for World objects to include a safety cap on recursion limit and provide warnings for potential RecursionErrors.
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.

failure of run-notebook-demos-extra test

2 participants