Skip to content

Comments

Fix infinite recursion in .copyEnv() on circular environment references#987

Merged
mattfidler merged 2 commits into985-renamefrom
copilot/sub-pr-986
Feb 20, 2026
Merged

Fix infinite recursion in .copyEnv() on circular environment references#987
mattfidler merged 2 commits into985-renamefrom
copilot/sub-pr-986

Conversation

Copy link
Contributor

Copilot AI commented Feb 20, 2026

.copyEnv() had no protection against circular environment references, causing infinite recursion and stack overflow when an environment directly or indirectly referenced itself.

Changes

  • .copyEnv(): Added a visited hash environment parameter (defaulting to a fresh env) that maps each environment's address to its already-created copy. Before recursing, checks visited for a cached copy and returns it immediately if found. Stores the new (empty) copy in visited before populating it — critical for self-referential cases (e$self <- e).
  • .copyUi(): Creates a single shared visited map and passes it to all .copyEnv() calls so shared environment references across UI items are tracked uniformly.
# Previously caused infinite recursion / stack overflow:
e <- new.env(parent = emptyenv())
e$self <- e        # direct self-reference
copy <- .copyEnv(e) # now returns safely

# Indirect cycles also handled:
a <- new.env(parent = emptyenv()); b <- new.env(parent = emptyenv())
a$other <- b; b$other <- a
copy_a <- .copyEnv(a) # no infinite recursion

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Co-authored-by: mattfidler <514778+mattfidler@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix deep copy issue in copy UI Fix infinite recursion in .copyEnv() on circular environment references Feb 20, 2026
Copilot AI requested a review from mattfidler February 20, 2026 19:41
@mattfidler mattfidler marked this pull request as ready for review February 20, 2026 19:43
@mattfidler mattfidler merged commit 87bfd96 into 985-rename Feb 20, 2026
5 of 7 checks passed
@mattfidler mattfidler deleted the copilot/sub-pr-986 branch February 20, 2026 21:43
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.

2 participants