Skip to content

[VarExporter][FrameworkBundle][7.0] Error when using a lazy AbstractController #52753

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
keulinho opened this issue Nov 27, 2023 · 5 comments
Closed

Comments

@keulinho
Copy link
Contributor

keulinho commented Nov 27, 2023

Symfony version(s) affected

7.0

Description

For 7.0 the protected $container property was typed as ContainerInterface, this leads to an error when using it together with the LazyGhost as it tries to access the property before it is initialized.

How to reproduce

Extend the AbstractController, configure it in the container as lazy and with the setContainer method

Possible Solution

Catch the exception, and set null as fallback:
Before:

        $previous = $this->container ?? null;

After:

        try {
            $previous = $this->container;
        } catch (\Error) {
            $previous = null;
        }

I tried to use isset instead of null coalesce, but that leads to memory issues, so i guess catching the exception would be best

Additional Context

See stack trace:

Error: Cannot access uninitialized non-nullable property Symfony\Bundle\FrameworkBundle\Controller\AbstractController::$container by reference
/builds/shopware/6/product/platform/vendor/symfony/var-exporter/LazyGhostTrait.php:133
/builds/shopware/6/product/platform/vendor/symfony/framework-bundle/Controller/AbstractController.php:63
/builds/shopware/6/product/platform/var/cache/test_h8458ab63ca9647a85b0d42ef842db976/ContainerMg28vrx/Shopware_Core_KernelTestDebugContainer.php:2851
/builds/shopware/6/product/platform/var/cache/test_h8458ab63ca9647a85b0d42ef842db976/ContainerMg28vrx/Shopware_Core_KernelTestDebugContainer.php:2846
/builds/shopware/6/product/platform/vendor/symfony/var-exporter/Internal/LazyObjectState.php:61
/builds/shopware/6/product/platform/vendor/symfony/var-exporter/LazyGhostTrait.php:206
/builds/shopware/6/product/platform/vendor/symfony/framework-bundle/Controller/AbstractController.php:63
/builds/shopware/6/product/platform/vendor/symfony/framework-bundle/Controller/ControllerResolver.php:28
/builds/shopware/6/product/platform/vendor/symfony/http-kernel/Controller/ControllerResolver.php:66
@nicolas-grekas
Copy link
Member

Can you please provide a small reproducer?
This might be caused by php/php-src#12695
Is your controller lazy? That shouldn't be needed I think (but that doesn't invalidate the issue)

@nicolas-grekas
Copy link
Member

Can you please try replacing the current code by this and let us know if this fixes it?

$previous = isset($this->container) ? $this->container : null;

@keulinho
Copy link
Contributor Author

Is your controller lazy? That shouldn't be needed I think (but that doesn't invalidate the issue)

Yes that is the fix on our side

$previous = isset($this->container) ? $this->container : null;

Yes that works as well, the memory issues i got when i first tried this solution are caused by another issue, that is independent.

@nicolas-grekas
Copy link
Member

Also, why is your controller registered as a lazy service?

@nicolas-grekas
Copy link
Member

A more low-level fix should be #52762.
Can you please give it a try (after reverting the previous change)?

nicolas-grekas added a commit that referenced this issue Nov 28, 2023
…ts, fixing nullsafe-related behavior (nicolas-grekas)

This PR was merged into the 6.3 branch.

Discussion
----------

[VarExporter] Work around php/php-src#12695 for lazy objects, fixing nullsafe-related behavior

| Q             | A
| ------------- | ---
| Branch?       | 6.3
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | #52753
| License       | MIT

This PR fixes another issue discovered with `@arnaud`-lb while working on lazy-objects.
The root of the issue is described in php/php-src#12695 and this PR implements a fix based on a suggestion by `@iluuu1994`

Only ghost objects are affected; proxy objects are OK.

Commits
-------

b051700 [VarExporter] Work around php/php-src#12695 for lazy objects, fixing nullsafe-related behavior
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

4 participants