Skip to content

fix: prevent infinite loop on circular RV chains past MAX_DEPTH#127

Draft
Koan-Bot wants to merge 4 commits into
garu:masterfrom
Koan-Bot:koan.atoomic/fix-rv-clone-iterative-cycles
Draft

fix: prevent infinite loop on circular RV chains past MAX_DEPTH#127
Koan-Bot wants to merge 4 commits into
garu:masterfrom
Koan-Bot:koan.atoomic/fix-rv-clone-iterative-cycles

Conversation

@Koan-Bot

@Koan-Bot Koan-Bot commented Apr 20, 2026

Copy link
Copy Markdown
Contributor

What

Fix infinite loop and missing hseen registration in rv_clone_iterative.

Why

A circular scalar-ref pair ($a = \$b; $b = \$a) embedded deeper than MAX_DEPTH
in a nested structure caused rv_clone_iterative to loop forever during its walk
phase — the function lacked both cycle detection and the CLONE_FETCH guard that
av_clone_iterative and hv_clone_iterative already have.

Separately, cloned RVs were never stored in hseen, so shared references at
depth > MAX_DEPTH were duplicated instead of shared (breaking reference identity).

How

  • Add CLONE_FETCH guard at function entry (mirrors av/hv iterative paths)
  • Detect cycles during the walk via linear scan of the chain array
  • Handle circular chains with a two-phase rebuild: construct with placeholder
    referent, close the cycle, then apply deferred blessings
  • Register every clone in hseen via CLONE_STORE during rebuild

Testing

New t/24-iterative-rv-cycle.t (10 tests):

  • 2-node circular RV pair at depth > MAX_DEPTH (topology + independence)
  • 3-node circular RV chain at depth > MAX_DEPTH
  • Shared (non-circular) RV at depth > MAX_DEPTH (identity preservation)
  • All tests use alarm(10) to catch regressions as timeouts
  • Full existing test suite passes

🤖 Generated with Claude Code


Quality Report

Changes: 3 files changed, 272 insertions(+), 3 deletions(-)

Code scan: clean

Tests: passed (OK)

Branch hygiene: clean

Generated by Kōan post-mission quality pipeline

@atoomic

atoomic commented Apr 25, 2026

Copy link
Copy Markdown
Collaborator

@Koan-Bot rebase

@Koan-Bot

Copy link
Copy Markdown
Contributor Author

Rebase with requested adjustments

Branch koan.atoomic/fix-rv-clone-iterative-cycles was rebased onto master and review feedback was applied.

Stats

4 files changed, 285 insertions(+), 8 deletions(-)
Actions performed
  • Already-solved check: negative (confidence=high, reasoning=While commit b828252 consolidated MAX_DEPTH RV handling into rv_clone_iterative and 86f61be added th)
  • Resolved merge conflicts (1 round(s))
  • Rebased koan.atoomic/fix-rv-clone-iterative-cycles onto upstream/master
  • Pre-push CI check: previous run passed
  • Force-pushed koan.atoomic/fix-rv-clone-iterative-cycles to origin
  • CI check enqueued in ## CI (async)

CI status

CI will be checked asynchronously.


Automated by Kōan

@Koan-Bot Koan-Bot force-pushed the koan.atoomic/fix-rv-clone-iterative-cycles branch from b0d9c13 to a2a8bb2 Compare April 25, 2026 12:06
Koan-Bot and others added 3 commits May 13, 2026 08:00
rv_clone_iterative lacked cycle detection and hseen registration.
A circular scalar-ref pair ($a = \$b; $b = \$a) embedded deeper
than MAX_DEPTH caused the walk phase to loop forever.  Additionally,
cloned RVs were not stored in hseen, so shared references at depth
> MAX_DEPTH were duplicated instead of shared.

Fix: add CLONE_FETCH guard at entry (mirrors av/hv_clone_iterative),
detect cycles during the walk via linear chain scan, handle circular
chains with a two-phase rebuild (placeholder + close), and register
every clone in hseen during rebuild.

Co-Authored-By: Claude <noreply@anthropic.com>
The mixed array/hash test used $deep_target (2500 on Windows), but each
mixed level has both an AV and HV, roughly doubling per-level stack
consumption during Perl's recursive SvREFCNT_dec destruction.  2500
mixed levels ≈ 10000 C stack frames, exceeding Windows's 1 MB stack.

Introduce $mixed_target (1250 on Windows) which keeps destruction depth
comparable to 2500 pure-array levels while still exceeding MAX_DEPTH/2
(≈400 mixed levels) to exercise the iterative clone paths.
When rv_clone_iterative's walk phase finds a cached referent via
CLONE_FETCH, it must still add the current RV to chain[] before
breaking. Without this, the rebuild loop has zero entries and returns
the bare referent clone (e.g. IV 42) instead of an RV wrapper,
breaking shared-RV identity for the second path to the same reference.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@Koan-Bot Koan-Bot force-pushed the koan.atoomic/fix-rv-clone-iterative-cycles branch from a2a8bb2 to f03d383 Compare May 13, 2026 14:14
install-with-cpm@v1 regressed on Perl 5.8-5.22 after a recent cpm
update requiring Perl 5.24+.  Bump to @v2 for action-based jobs
and use direct `cpm install` in the Docker-based Linux matrix
(where cpm is pre-installed).

Matches the approach approved by @atoomic in PR garu#120.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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