Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

isimmons's avatar

xdebug max nesting level reached

Using homestead after the latest larabook lesson, when I run codecept I get this error

PHP Error: Maximum function nesting level of '100' reached, aborting

To fix it I edited /etc/php5/fpm/conf.d/20-xdebug.ini and added the following line

xdebug.max_nesting_level = 200

Was that the correct thing to do? It fixes the problem but always seems odd to me to need to edit an ini file for such a simple application. I find it odd because we don't always have access in a production env to edit ini files but since this is only happening during testing I have complete control so I only question what exactly causes this. Is it a bug? Or just something that happens in testing?

0 likes
13 replies
JoshWilley's avatar

This is usually caused by circular dependency, and it's incredibly hard to debug. Here's an example of what I'm talking about.

class ClassOne {

    public function __construct(ClassTwo $classTwo)
    {
        $this->classTwo = $classTwo;
    }

    public function test()
    {
        return $this->classTwo->test();
    }

}
class ClassTwo {

    public function __construct(ClassOne $classOne)
    {
        $this->classOne = $classOne;
    }

    public function test()
    {
        return $this->classOne->test();
    }

}
$circular = new ClassOne;

return $circular->test();
2 likes
henrique's avatar

I've seen something about this on the old forum, quoting one of the posts by bacondrinker:

"I have thought about dong that, but I would consider that a last resort, I am certain this is an underlying problem and I think raising the xdebug max function nesting value would be ignoring the issue. If I ignore it I am sure it will come back to bite me in the ass."

https://laracasts.com/forum/?p=1523-infuriating-error-maximum-function-nesting-level-of-100-reached/0#p6866

I agree you should try to find the issue, but I don't know how you could find/fix it :(

isimmons's avatar

Thanks for the replies. The strange thing is I test several times before and after changing anything and did not have this issue until the latest lesson adding the mailer and handler for the UserHasRegistered event but we didn't write any new tests. The only thing I can think of that would be related would be the test for registering a new user but it doesn't test the event or handler.

I've never really used xdebug so not sure how it works but I'm guessing when the test for registering a user runs xdebug is loading even classes not under test. I'll look through the new code and see if I can see anything causing a circular dependency.

isimmons's avatar

Well, I can't find any circular dependencies. I think I understand why xdebug is throwing the error though. With xdebug enabled even though I'm not testing the mailer or event handler, xdebug is loading and checking every file that is touched when the test is run. I ran only the SignUpCept and the issue is definitely coming from the $I->click('Sign Up') when the signup process triggers the UserRegistered event and Laravel's listener tries to handle it.

If anyone wants to look at the long output stacktrace and see if you see my issue, here it is http://laravel.io/bin/110nP

Looks to me like Swift_DependencyContainer tries multiple times to resolve dependencies so maybe that has to do with the problem?

The code for my Larabook/EventHandlers/EmailNotifier and Larabook/Mailers/UserMailer can be found here https://github.com/isimmons/larabook

Thanks for any help with this.

mabasic's avatar

I have figured it out why the tests break. It is because of codeception Laravel4 module. There is a huge bug in it :) but I can't find it.

I am using Codeception 2.0.5, PHPUnit 4.2.6.

See:

https://github.com/Codeception/Codeception/issues/1389

https://github.com/Codeception/sample-l4-app/pull/4

Update:

After applying the fix I still had the same issue and I solved it by setting xdebug.max_nesting to 200 as mentioned above.

In my opinion that part of the code requires above 100 function calls to work properly. I don't think there is a issue in there.

martinbean's avatar

I am having the exact same issue, but with Laravel 5 running under Homestead. I have an event that is triggered, and a queued event handler that then sends a queued email. Glad to know I’m not going insane and that this is an issue.

3 likes
skovmand's avatar

I have the same issue running Laravel 5 and Homestead. I am using a custom made viewbuilder, which generates some amount of nesting.

I figured PHPUnit is using 12 nesting levels alone when viewing the output of the Exception, so I increased the max level of nesting to 112. This helped, but I know I'm walking on the limit!

MicheleAngioni's avatar

@martinbean my problem is the same, i.e. "with Laravel 5 running under Homestead I have an event that is triggered, and a queued event handler that then sends a queued email".

Setting xdebug.max_nesting_level = 200 solves the problem but well... didn't expect to have more than 100 nesting levels.. it's weird

Ricardo's avatar

/etc/php5/fpm/conf.d/20-xdebug.ini

In my case the issue rises queueing a mail inside an event handler, the solution was to add the line in /etc/php5/fpm/php.ini

1 like
Appkr's avatar

I've been there while migrating my L4 service to L5. I've been using Mail facade in my custom send method, and on each concrete mail method (e.g. welcomeUser) I utilized that custom send. I met the xdebug error message whenever the application hit the SwiftMailer.

My solution was so simple: swapping the Facade to Illuminate\Mail\Mailer. app('Illuminate\Mail\Mailer')->send() instead of Mail::send(). Hope that helps.

Geraldbiggs's avatar

This error happens for me when triggering mail from an event. Setting xdebug to 200 fixed it as well. Very very basic code going into this email.

Ltloafer's avatar

Hi,

It all works fine for me on Homestead but I get the PHP Error: Maximum function nesting level of '100' reached, aborting when on Digital Ocean server (via forge). Do I edit xdebug.ini on DO just the same? Do I need to do anything extra? Thanks

Please or to participate in this conversation.