bugsnag / bugsnag-laravel

BugSnag notifier for the Laravel PHP framework. Monitor and report Laravel errors.
https://docs.bugsnag.com/platforms/php/laravel/
MIT License
876 stars 129 forks source link

[OomBootstrapper] BindingResolutionException: Target class [bugsnag] does not exist. #527

Open wimski opened 1 year ago

wimski commented 1 year ago

I'm trying to use the OomBootstrapper, but I get a BindingResolutionException.

Environment

Steps

  1. Install package.
    composer require bugsnag/bugsnag-laravel
  2. Add the provider in app.config.

    // config/app.php
    [
    //...
    
    'providers' => [
    
        /*
         * Laravel Framework Service Providers...
         */
        //...
    
        /*
         * Package Service Providers...
         */
        Bugsnag\BugsnagLaravel\BugsnagServiceProvider::class,
    
        /*
         * Application Service Providers...
         */
        //...
    ],
    ];
  3. Add the OomBootstrapper in both kernel classes as per the documentation.
    // app/Console/Kernel.php
    // app/Http/Kernel.php
    /**
    * @return array<int, string>
    */
    protected function bootstrappers(): array
    {
    return array_merge(
        [\Bugsnag\BugsnagLaravel\OomBootstrapper::class],
        parent::bootstrappers(),
    );
    }

Stack trace

Details ``` Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 266240 bytes) in /var/www/html/vendor/bugsnag/bugsnag-laravel/src/OomBootstrapper.php on line 35 Fatal error: Uncaught ReflectionException: Class "bugsnag" does not exist in /var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php:889 Stack trace: #0 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php(889): ReflectionClass->__construct('bugsnag') #1 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php(770): Illuminate\Container\Container->build('bugsnag') #2 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(935): Illuminate\Container\Container->resolve('bugsnag', Array, true) #3 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php(706): Illuminate\Foundation\Application->resolve('bugsnag', Array) #4 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(920): Illuminate\Container\Container->make('bugsnag', Array) #5 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php(120): Illuminate\Foundation\Application->make('bugsnag', Array) #6 /var/www/html/vendor/bugsnag/bugsnag-laravel/src/OomBootstrapper.php(53): app('bugsnag') #7 [internal function]: Bugsnag\BugsnagLaravel\OomBootstrapper->Bugsnag\BugsnagLaravel\{closure}() #8 {main} Next Illuminate\Contracts\Container\BindingResolutionException: Target class [bugsnag] does not exist. in /var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php:891 Stack trace: #0 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php(770): Illuminate\Container\Container->build('bugsnag') #1 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(935): Illuminate\Container\Container->resolve('bugsnag', Array, true) #2 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php(706): Illuminate\Foundation\Application->resolve('bugsnag', Array) #3 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(920): Illuminate\Container\Container->make('bugsnag', Array) #4 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php(120): Illuminate\Foundation\Application->make('bugsnag', Array) #5 /var/www/html/vendor/bugsnag/bugsnag-laravel/src/OomBootstrapper.php(53): app('bugsnag') #6 [internal function]: Bugsnag\BugsnagLaravel\OomBootstrapper->Bugsnag\BugsnagLaravel\{closure}() #7 {main} thrown in /var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php on line 891 ```
wimski commented 1 year ago

This seems to be a memory issue with my specific application context. My bad :grimacing:

On second thought, here is some more information.

The issue occurs when running my PHPUnit test suite. When the OomBootstrapper is setup, memory keeps increasing during the running of the test suite until it hits the limit. If I remove the whole bootstrappers setup, the memory increase is gone and the test suite runs fine.

wimski commented 1 year ago

I have found a solution. The memory leak seems to stem from register_shutdown_function() handler being called over and over again. This little tricks makes my memory issue go away:

class OomBootstrapper
{
    protected static bool $isRegistered = false;

    public function bootstrap()
    {
        if (self::$isRegistered) {
            return;
        }

        $this->reservedMemory = str_repeat(' ', 1024 * 256);

        register_shutdown_function(function () use ($app) {
            // ...
        });

        self::$isRegistered = true;
    }
}
johnkiely1 commented 1 year ago

Hi @wimski, Thanks for raising. Glad you've found a solution for now. We're going to look into this to see what we can do to fix on our side.

sirshaun commented 2 months ago

any updates on this issue?

clr182 commented 2 months ago

Hi @sirshaun

Unfortunately no updates on this yet. It is still on our backlog and we'll be sure to share more on this thread when we have an update.

In the meantime, have you tried using the workaround suggested by @wimski ?

wimski commented 2 months ago

This is the workaround as an extended class which I use in my projects.

<?php

declare(strict_types=1);

namespace App\Extensions\Bugsnag\BugsnagLaravel;

use Bugsnag\BugsnagLaravel\OomBootstrapper as BugsnagOomBootstrapper;

/**
 * @see https://github.com/bugsnag/bugsnag-laravel/issues/527#issuecomment-1463844176
 */
class OomBootstrapper extends BugsnagOomBootstrapper
{
    protected static bool $isRegistered = false;

    public function bootstrap(): void
    {
        if (self::$isRegistered) {
            return;
        }

        parent::bootstrap();

        self::$isRegistered = true;
    }
}
sirshaun commented 2 months ago

thank you @clr182 for getting back to me. and, thank you @wimski, i'll try your workaround