Skip to content

Singleton Attribute in DI is not respected (reset at each request in same worker) #1077

@kamil-michalak

Description

@kamil-michalak

Octane Version

2.13.1

Laravel Version

12.38.1

PHP Version

8.4.14

What server type are you using?

Swoole

Server Version

nginx

Database Driver & Version

No response

Description

When registering a singleton via AppServiceProvider, the constructor is triggered only once per worker, as expected. However, when using the attribute-based singleton registration (https://laravel.com/docs/12.x/container#singleton-attribute
), the constructor appears to be triggered once per request scope, behaving more like a scoped service. This may be due to the sandbox being registered earlier.

Steps To Reproduce

Check running this part

namespace App\TestService;

use Illuminate\Container\Attributes\Singleton;

#[Singleton]
class TestService
{
    public function __construct()
    {
        echo("__construct TestService");
        echo(spl_object_id($this));
    }
}

in some controler use

app(\App\TestService\TestService::class);
app(\App\TestService\TestService::class);
app(\App\TestService\TestService::class);

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