Skip to content

Leak: an exception leaving a foreach over an IteratorAggregate never releases the iterator getIterator() returned #103

Closed

Description

@blacktrs

Summary

Leak: an exception leaving a foreach over an IteratorAggregate never releases the iterator getIterator() returned.

Repro

<?php
final class M { public function __construct(public string $data) {} }
final class C implements \IteratorAggregate {
    public int $n = 0;
    public function receive(): M { if ($this->n >= 2) { throw new \RuntimeException('x'); } $this->n++; return new M(str_repeat('m', 2000) . $this->n); }
    public function getIterator(): \Generator { while (true) { yield $this->receive(); } }
}
function run1(C $ws): int { $t = 0; foreach ($ws as $m) { $t += strlen($m->data); } return $t; }
function turn(): int { $c = new C(); try { return run1($c); } catch (\RuntimeException $e) { return 1; } }
$t = 0;
for ($i = 0; $i < 2000; $i++) { $t += turn(); }
$m0 = memory_get_peak_usage();
for ($i = 0; $i < 40000; $i++) { $t += turn(); }
$mb = (memory_get_peak_usage() - $m0) >> 20;
fwrite(STDERR, "grew {$mb} MB\n");
echo $mb < 8 ? "flat\n" : "LEAK\n";

Expected (php)

flat

Actual (manticore e3c213cf, Darwin arm64)

LEAK
(stderr: grew 83 MB)

Tracking

Known-bug repro: tests/aot/repro/leak/foreach_aggregate_iterator_exception.php — bash tests/aot/xfail.sh -k foreach_aggregate_iterator_exception 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 6, 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