Skip to content

Fatal error: Uncaught Error: Maximum call stack size of 8306688 bytes (zend.max_allowed_stack_size - zend.reserved_stack_size) reached. Infinite recursion? in 8.3.27 and 8.4.14 #20338

@alexander-schranz

Description

@alexander-schranz

Description

I'm fine if you close this issue as we found a way to workaround by implement serialization in another way. Just want to report the issue here if it was unexpected change and / other stumble over it they can get inspiration by our fix here.


https://3v4l.org/40nda (Run in all PHP versions and you see 8.3.27 and 8.4.14 failing now)

The following code (ClassMetadata has IndexMetadata which reference back to its parent class):

<?php

/* Bridge between PHP versions by jms/metadata */
trait SerializationHelper
{
    /**
     * @deprecated Use serializeToArray
     *
     * @return string
     */
    public function serialize()
    {
        return serialize($this->serializeToArray());
    }

    /**
     * @deprecated Use unserializeFromArray
     *
     * @param string $str
     *
     * @return void
     */
    public function unserialize($str)
    {
        $this->unserializeFromArray(unserialize($str));
    }

    public function __serialize(): array
    {
        return [$this->serialize()];
    }

    public function __unserialize(array $data): void
    {
        $this->unserialize($data[0]);
    }
}

class ClassMetadata implements \Serializable {
    
    use SerializationHelper;
    
    /**
     * @var array
     */
    private $indexMetadatas = [];


    public function addIndexMetadata($contextName, IndexMetadata $indexMetadata)
    {
        $indexMetadata->setClassMetadata($this);
        $this->indexMetadatas[$contextName] = $indexMetadata;
    }

    public function serializeToArray(): array
    {
        return [$this->indexMetadatas,];
    }

    public function unserializeFromArray(array $data): void
    {
        list($indexMetadata) = $data;
        $this->indexMetadatas = $indexMetadata;
    }
}


class IndexMetadata
{
    /**
     * @var ClassMetadata
     */
    private $classMetadata;

    public function getClassMetadata()
    {
        return $this->classMetadata;
    }

    public function setClassMetadata(ClassMetadata $classMetadata)
    {
        $this->classMetadata = $classMetadata;
    }
}

$classMetadata = new ClassMetadata();
$indexMetadata = new IndexMetadata();

$classMetadata->addIndexMetadata('test', $indexMetadata);

var_dump(serialize($classMetadata));

Resulted in this output:

Fatal error: Uncaught Error: Maximum call stack size of 8306688 bytes (zend.max_allowed_stack_size - zend.reserved_stack_size) reached. Infinite recursion? in /in/40nda:12
Stack trace:

But I expected this output instead:

string(175) "O:13:"ClassMetadata":1:{i:0;s:137:"a:1:{i:0;a:1:{s:4:"test";O:13:"IndexMetadata":1:{s:28:"IndexMetadataclassMetadata";O:13:"ClassMetadata":1:{i:0;s:12:"a:1:{i:0;N;}";}}}}";}"

PHP Version

PHP 8.3.27 (cli) (built: Oct 21 2025 14:53:41) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.27, Copyright (c) Zend Technologies
    with Xdebug v3.3.2, Copyright (c) 2002-2024, by Derick Rethans
    with Zend OPcache v8.3.27, Copyright (c), by Zend Technologies

Operating System

MacOS:15.5, Ubuntu:latest, ...

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions