Skip to content

Shared service instantiated multiple times #49861

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
julienfalque opened this issue Mar 29, 2023 · 5 comments
Closed

Shared service instantiated multiple times #49861

julienfalque opened this issue Mar 29, 2023 · 5 comments

Comments

@julienfalque
Copy link
Contributor

julienfalque commented Mar 29, 2023

Symfony versions affected

5.4.17 to 5.4.21

Description

With a specific dependency graph with circular references, some services will be instanciated twice, e.g. A' → B → A instead of A → B → A. This happens with symfony/dependency-injection since version 5.4.17 up to latest version to date (5.4.21). Downgrading to version 5.4.16 fixes the issue. This seems to be caused by #48791.

How to reproduce

I'm able to reproduce the issue with this graph of objects:

namespace App;

class ServiceA
{
    public function __construct(
        private readonly ServiceB $serviceB,
        private readonly ServiceC $serviceC,
    ) {
    }
}

class ServiceB
{
    /**
     * @param iterable<ServiceC> $servicesC
     */
    public function __construct(
        private readonly iterable $servicesC,
    ) {
    }
}

class ServiceC
{
    private ServiceA $serviceA;
    private ServiceB $serviceB;

    /**
     * @required
     */
    public function setServiceA(ServiceA $serviceA)
    {
        $this->serviceA = $serviceA;
    }

    /**
     * @required
     */
    public function setServiceB(ServiceB $serviceB)
    {
        $this->serviceB = $serviceB;
    }
}
services:
  _defaults:
    autowire: true
    autoconfigure: true
    public: false

  App\ServiceA:

  App\ServiceB:
    $servicesC: !tagged_iterator foo

  App\ServiceC:
    tags:
      - { name: foo }

Inject ServiceA in e.g. a controller and add a breakpoint or echo something in its constructor to see it's instantiated twice.

@carsonbot
Copy link

Hey, thanks for your report!
There has not been a lot of activity here for a while. Is this bug still relevant? Have you managed to find a workaround?

@carsonbot
Copy link

Hello? This issue is about to be closed if nobody replies.

@julienfalque
Copy link
Contributor Author

As far as I know, the problem still exists.

@carsonbot carsonbot removed the Stalled label Oct 17, 2023
@carsonbot
Copy link

Hey, thanks for your report!
There has not been a lot of activity here for a while. Is this bug still relevant? Have you managed to find a workaround?

@julienfalque
Copy link
Contributor Author

I cannot reproduce the issue with symfony/dependency-injection 7.0.4.

@julienfalque julienfalque closed this as not planned Won't fix, can't repro, duplicate, stale Apr 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants