Skip to content

foreach over an IteratorAggregate whose getIterator() is a generator leaks the generator per loop. #268

Closed

Description

@blacktrs

Summary

foreach over an IteratorAggregate whose getIterator() is a generator leaks the generator per loop.

Repro

<?php
final class RG implements IteratorAggregate {
    public function __construct(private \Closure $gen) {}
    public function getIterator(): \Traversable { $g = $this->gen; yield from $g(); }
}
function walk(RG $r): int { $n = 0; foreach ($r as $s) { $n += $s; } return $n; }
$rg = new RG(function () { yield 1; yield 2; });
for ($i = 0; $i < 20000; $i++) { walk($rg); }
$b = memory_get_usage();
for ($i = 0; $i < 200000; $i++) { walk($rg); }
echo (memory_get_usage() - $b) < 4000000 ? "flat" : "leak", "\n";

Expected (php)

flat

Actual (manticore 24ab81a0, Darwin arm64)

leak

Tracking

Known-bug repro: tests/aot/repro/leak/aggregate_generator_get_iterator_leaks.php — bash tests/aot/xfail.sh -k aggregate_generator_get_iterator_leaks reports XFAIL while this is open and XPASS once it is fixed; then move the repro into tests/aot/cases/ and close this issue.

Activity

  1. added
    bug
    leak
    on Oct 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugleak

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions