10000 bug #60260 [Serializer] Prevent `Cannot traverse an already closed ge… · symfony/symfony@1fb9d17 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1fb9d17

Browse files
committed
bug #60260 [Serializer] Prevent Cannot traverse an already closed generator error by materializing Traversable input (santysisi)
This PR was merged into the 6.4 branch. Discussion ---------- [Serializer] Prevent `Cannot traverse an already closed generator` error by materializing Traversable input | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix #60141 | License | MIT ### ✅ Pull Request description: This PR addresses the issue reported in the linked ticket, specifically the error: `Cannot traverse an already closed generator` The fix involves converting `Traversable` input into an array using `iterator_to_array($data)`, in order to allow multiple traversals of generator-based inputs. At first glance, it might seem that this approach significantly increases memory usage, since all generator values are stored in memory. However, this is not the case. In the current logic, the following [loop](https://github.com/symfony/symfony/blob/6.4/src/Symfony/Component/Serializer/Encoder/CsvEncoder.php#L82-L86) already forces all values from the generator into memory: ```php foreach ($data as &$value) { $flattened = []; $this->flatten($value, $flattened, $keySeparator, '', $escapeFormulas); $value = $flattened; } ``` Therefore, materializing the generator with `iterator_to_array() `does not increase peak memory usage in any meaningful way. As an example, here's the comparison of peak memory usage (measured with [memory_get_peak_usage](https://www.php.net/manual/en/function.memory-get-peak-usage.php)) when processing an array of 1 million integers: * **With the fix**: 90,044,272 bytes * **Without the fix**: 89,936,680 bytes The difference is negligible, confirming that the fix does not introduce a meaningful performance cost in terms of memory. Commits ------- c7206a9 Fix: prevent "Cannot traverse an already closed generator" error by materializing Traversable input
2 parents ebb9e65 + c7206a9 commit 1fb9d17

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/Symfony/Component/Serializer/Encoder/CsvEncoder.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ public function encode(mixed $data, string $format, array $context = []): string
6565
} elseif (empty($data)) {
6666
$data = [[]];
6767
} else {
68+
if ($data instanceof \Traversable) {
69+
// Generators can only be iterated once — convert to array to allow multiple traversals
70+
$data = iterator_to_array($data);
71+
}
6872
// Sequential arrays of arrays are considered as collections
6973
$i = 0;
7074
foreach ($data as $key => $value) {

src/Symfony/Component/Serializer/Tests/Encoder/CsvEncoderTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,4 +710,28 @@ public function testEndOfLinePassedInConstructor()
710710
$encoder = new CsvEncoder([CsvEncoder::END_OF_LINE => "\r\n"]);
711711
$this->assertSame("foo,bar\r\nhello,test\r\n", $encoder->encode($value, 'csv'));
712712
}
713+
714+
/** @dataProvider provideIterable */
715+
public function testIterable(mixed $data)
716+
{
717+
$this->assertEquals(<<<'CSV'
718+
foo,bar
719+
hello,"hey ho"
720+
hi,"let's go"
721+
722+
CSV, $this->encoder->encode($data, 'csv'));
723+
}
724+
725+
public static function provideIterable()
726+
{
727+
$data = [
728+
['foo' => 'hello', 'bar' => 'hey ho'],
729+
['foo' => 'hi', 'bar' => 'let\'s go'],
730+
];
731+
732+
yield 'array' => [$data];
733+
yield 'array iterator' => [new \ArrayIterator($data)];
734+
yield 'iterator aggregate' => [new \IteratorIterator(new \ArrayIterator($data))];
735+
yield 'generator' => [(fn (): \Generator => yield from $data)()];
736+
}
713737
}

0 commit comments

Comments
 (0)
0