php.net |  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
Votes:5
Avg. Score:4.2 ± 1.0
Reproduced:4 of 5 (80.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: pierrick@php.net Assigned:
Status: Open Package: SPL related
PHP Version: 5.6.24 OS:
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2016-07-28 05:09 UTC] pierrick@php.net
Description:
------------
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:
---------------
<?php

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

$ai = new AppendIterator();
$ai->append($gen);
var_dump(iterator_to_array($gen));


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


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-07-28 23:59 UTC] nikic@php.net
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 https://3v4l.org/R4AH3. 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-2019 The PHP Group
All rights reserved.
Last updated: Mon Oct 21 12:01:26 2019 UTC