bugsnag/bugsnag-laravel

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

wimski opened this issue · 7 comments

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

Environment

  • Bugsnag Laravel version: 2.26
    • Bugsnag version: 3.29.0
  • PHP version: 8.2.3
  • Composer version: 2.5.4
  • Laravel version: 10.0.0

Steps

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

    'providers' => [

        /*
         * Laravel Framework Service Providers...
         */
        //...

        /*
         * Package Service Providers...
         */
        Bugsnag\BugsnagLaravel\BugsnagServiceProvider::class,

        /*
         * Application Service Providers...
         */
        //...
    ],
];
  1. 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

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

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.

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;
    }
}

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.

any updates on this issue?

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 ?

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;
    }
}

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