|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #72692 Bad behaviour of AppendIterator with Empty Generator
Submitted: 2016-07-28 05:09 UTC Modified: 2016-07-28 23:59 UTC
Avg. Score:4.4 ± 0.9
Reproduced:6 of 8 (75.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: Assigned:
Status: Open Package: SPL related
PHP Version: 5.6.24 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
New email:
PHP Version: OS:


 [2016-07-28 05:09 UTC]
When giving an new empty generator, AppendIterator will directly throw an Exception "Cannot traverse an already closed generator" even if the given generator is new.

Test script:

function createEmptyGen() { if(false) { yield 1; } }
$gen = createEmptyGen();

$ai = new AppendIterator();

Expected result:
array(0) {

Actual result:
Fatal error: Uncaught Exception: Cannot traverse an already closed generator in /home/pierrick/php-src/github/foo.php:14
Stack trace:
#0 /home/pierrick/php-src/github/foo.php(14): iterator_to_array(Object(Generator))
#1 {main}
  thrown in /home/pierrick/php-src/github/foo.php on line 14


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2016-07-28 23:59 UTC]
Related: Bug #71436
 [2019-09-17 15:03 UTC] f dot bosch at genkgo dot nl
I hit this bug too today, still valid in PHP7.4. See For others who hit this bug: use a closure and yield from as alternative.

function createEmptyGen1() { if(false) { yield 1; } }
function createEmptyGen2() { if(false) { yield 2; } }

$ai = (function () {
    yield from createEmptyGen1();
    yield from createEmptyGen2();

foreach ($ai as $_);
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sat May 15 12:01:23 2021 UTC