[critical] The toIterable() method causes an unexpected behaviour (segfault ...) #7579

Closed
opened 2026-01-22 15:53:45 +01:00 by admin · 8 comments
Owner

Originally created by @acantepie on GitHub (Dec 15, 2025).

Doctrine version : 3.5.3
Php version : 8.4.15

I’m running into an incomprehensible problem with Doctrine’s QueryBuilder toIterable() function.
I can’t catch exceptions anymore, or I get segfaults after using toIterable().

Example of code (in symfony command) :

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        try {
            $this->foo();
        } catch (\Throwable $exception) {
            $output->writeln('>>>>>> EXCEPTION CAUGHT <<<<<');
        }

        return self::SUCCESS;
    }

    private function foo(): void
    {
        $qb = $this->em->createQueryBuilder();
        $qb->select('e');
        $qb->from(Product::class, 'e');

        $clients = $qb->getQuery()->toIterable();
        
        foreach ($clients as $client) {
            throw new \InvalidArgumentException('EXCEPTION');
        }
    }
}

The exception "EXCEPTION" is not caught by the try-catch block in execute function. I don't get this problem if exception is earlier on code or if i used getResult() method instead.

In the original script, I get a segfault (624 segmentation fault (core dumped)) after throwing the exception.

Originally created by @acantepie on GitHub (Dec 15, 2025). Doctrine version : 3.5.3 Php version : 8.4.15 I’m running into an incomprehensible problem with Doctrine’s QueryBuilder `toIterable()` function. I can’t catch exceptions anymore, or I get segfaults after using toIterable(). Example of code (in symfony command) : ```php protected function execute(InputInterface $input, OutputInterface $output): int { try { $this->foo(); } catch (\Throwable $exception) { $output->writeln('>>>>>> EXCEPTION CAUGHT <<<<<'); } return self::SUCCESS; } private function foo(): void { $qb = $this->em->createQueryBuilder(); $qb->select('e'); $qb->from(Product::class, 'e'); $clients = $qb->getQuery()->toIterable(); foreach ($clients as $client) { throw new \InvalidArgumentException('EXCEPTION'); } } } ``` The exception "EXCEPTION" is not caught by the try-catch block in execute function. I don't get this problem if exception is earlier on code or if i used getResult() method instead. In the original script, I get a segfault (`624 segmentation fault (core dumped)`) after throwing the exception.
admin closed this issue 2026-01-22 15:53:46 +01:00
Author
Owner

@greg0ire commented on GitHub (Dec 15, 2025):

The exception "UNCATCHABLE EXCEPTION" is uncatchable.

What are you referring to? The code you included does not contain this.

@greg0ire commented on GitHub (Dec 15, 2025): > The exception "UNCATCHABLE EXCEPTION" is uncatchable. What are you referring to? The code you included does not contain this.
Author
Owner

@acantepie commented on GitHub (Dec 15, 2025):

@greg0ire I have edited my post, i mean that the exception new \InvalidArgumentException('EXCEPTION'); is not caught in the execute function by the try-catch block.

I have created a small app in symfony to reproduce this issue : https://github.com/acantepie/issue-php8-4-15

@acantepie commented on GitHub (Dec 15, 2025): @greg0ire I have edited my post, i mean that the exception `new \InvalidArgumentException('EXCEPTION');` is not caught in the `execute` function by the try-catch block. I have created a small app in symfony to reproduce this issue : https://github.com/acantepie/issue-php8-4-15
Author
Owner

@greg0ire commented on GitHub (Dec 15, 2025):

If you get a segmentation fault, then your issue is with PHP, not with Doctrine I'm afraid.

@greg0ire commented on GitHub (Dec 15, 2025): If you get a segmentation fault, then your issue is with PHP, not with Doctrine I'm afraid.
Author
Owner

@greg0ire commented on GitHub (Dec 15, 2025):

Anyway, I've tried your reproducer, and I don't get a segfault 🤔
I've also tried adding a finally block, it's not executed either. Maybe the issue is with the exception handler.

@greg0ire commented on GitHub (Dec 15, 2025): Anyway, I've tried your reproducer, and I don't get a segfault 🤔 I've also tried adding a `finally` block, it's not executed either. Maybe the issue is with the exception handler.
Author
Owner

@greg0ire commented on GitHub (Dec 15, 2025):

I've tried reproducing it with a simple generator, to no avail. So it could be related to Doctrine and PHP.

@greg0ire commented on GitHub (Dec 15, 2025): I've tried reproducing it with a simple generator, to no avail. So it could be related to Doctrine _and_ PHP.
Author
Owner

@greg0ire commented on GitHub (Dec 15, 2025):

I've managed to reproduce the issue with PHPUnit (no Symfony involved): https://github.com/doctrine/orm/pull/12324

@greg0ire commented on GitHub (Dec 15, 2025): I've managed to reproduce the issue with PHPUnit (no Symfony involved): https://github.com/doctrine/orm/pull/12324
Author
Owner

@greg0ire commented on GitHub (Dec 15, 2025):

I've asked my LLM to look into this, and it found this is in fact a PHP bug. Here is the minimal reproducer it came up with:

#!/usr/bin/env php
<?php

function gen(): Generator
{
    try {
        yield 1;
    } finally {
    }
}

function process(): void
{
    $g = gen();
    foreach ($g as $_) {
        throw new Exception('ERROR');
    }
}

try {
    process();
} catch (Exception $e) {
    echo "Caught\n";
}

I've tried simplifying it but couldn't. It seems to affect PHP 8.4.15 but not PHP 8.4.14, so it's a regression.

@greg0ire commented on GitHub (Dec 15, 2025): I've asked my LLM to look into this, and it found this is in fact a PHP bug. Here is the minimal reproducer it came up with: ```php #!/usr/bin/env php <?php function gen(): Generator { try { yield 1; } finally { } } function process(): void { $g = gen(); foreach ($g as $_) { throw new Exception('ERROR'); } } try { process(); } catch (Exception $e) { echo "Caught\n"; } ``` I've tried simplifying it but couldn't. It seems to affect PHP 8.4.15 but not PHP 8.4.14, so it's a regression.
Author
Owner

@greg0ire commented on GitHub (Dec 15, 2025):

Reported as https://github.com/php/php-src/issues/20714

@greg0ire commented on GitHub (Dec 15, 2025): Reported as https://github.com/php/php-src/issues/20714
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#7579