Skip to content

[HttpKernel] fix ErrorException in CacheWarmerAggregate #43501

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

Merged

Conversation

Ahummeling
Copy link
Contributor

Q A
Branch? 4.4
Bug fix? yes
New feature? no
Deprecations? no
Tickets
License MIT
Doc PR

I ran into an ErrorException reloading after some changes. I suppose this triggered a cache warmup call. This didn't go as expected, because $previousLogs = unserialize(file_get_contents($this->deprecationLogsFilepath)); set $previousLogs to false instead of the expected array. I think it makes sense to only call array_merge if $previousLogs was successfully instantiated as an array.

My IDE also pointed out that $collectedLogs was possible undefined, so moving its declaration outside of the if block resolved that.

Stacktrace:

ErrorException:
Warning: array_merge(): Expected parameter 1 to be an array, bool given

  at /srv/vendor/symfony/http-kernel/CacheWarmer/CacheWarmerAggregate.php:106
  at Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerAggregate->warmUp('/srv/var/cache/local')
     (/srv/vendor/symfony/http-kernel/Kernel.php:584)
  at Symfony\Component\HttpKernel\Kernel->initializeContainer()
     (/srv/vendor/symfony/http-kernel/Kernel.php:786)
  at Symfony\Component\HttpKernel\Kernel->preBoot()
     (/srv/vendor/symfony/http-kernel/Kernel.php:187)
  at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
     (/srv/public/index.php:44)                

@carsonbot
Copy link

Hey!

I see that this is your first PR. That is great! Welcome!

Symfony has a contribution guide which I suggest you to read.

In short:

  • Always add tests
  • Keep backward compatibility (see https://symfony.com/bc).
  • Bug fixes must be submitted against the lowest maintained branch where they apply (see https://symfony.com/releases)
  • Features and deprecations must be submitted against the 5.4 branch.

Review the GitHub status checks of your pull request and try to solve the reported issues. If some tests are failing, try to see if they are failing because of this change.

When two Symfony core team members approve this change, it will be merged and you will become an official Symfony contributor!
If this PR is merged in a lower version branch, it will be merged up to all maintained branches within a few days.

I am going to sit back now and wait for the reviews.

Cheers!

Carsonbot

@Ahummeling Ahummeling force-pushed the fix-http-kernel-cache-warmer-aggregate branch from 06edb66 to e623192 Compare October 14, 2021 12:00
@nicolas-grekas nicolas-grekas changed the title fix ErrorExcception in CacheWarmerAggregate [HttpKernel ]fix ErrorException in CacheWarmerAggregate Oct 28, 2021
@nicolas-grekas nicolas-grekas changed the title [HttpKernel ]fix ErrorException in CacheWarmerAggregate [HttpKernel] fix ErrorException in CacheWarmerAggregate Oct 28, 2021
if ($collectDeprecations = $this->debug && !\defined('PHPUNIT_COMPOSER_INSTALL')) {
$collectedLogs = [];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be reverted, the IDE is just missing a brain :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fair enough, will take care of that.
Reverting that is:p

@carsonbot carsonbot changed the title [HttpKernel] fix ErrorException in CacheWarmerAggregate fix ErrorException in CacheWarmerAggregate Oct 28, 2021
@carsonbot carsonbot changed the title fix ErrorException in CacheWarmerAggregate [HttpKernel] fix ErrorException in CacheWarmerAggregate Oct 28, 2021
Copy link
Member

@derrabus derrabus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. Is it possible to write a test for that change?

@Ahummeling
Copy link
Contributor Author

Looks good to me. Is it possible to write a test for that change?

Oh I hadn't looked into that actually.
I'll see if there are related tests and see if it's possible to test it. And if so, write a test:)

@Ahummeling
Copy link
Contributor Author

So @derrabus I am running into an issue here attempting to test this. The relevant codeblock is only executed when the constant PHPUNIT_COMPOSER_INSTALL is not defined. I found that I can "undefine" a constant using uopz_undefine, however, I'd have to install the extension in the ci flow and I am not really sure it's worth the hassle.
Maybe I am overthinking this, I am open to suggestions, but as it stands right now, I'm unable to test it.

@nicolas-grekas
Copy link
Member

I'm not sure this can be tested, because I suspect this might happen only in some race conditions.

@Ahummeling Ahummeling force-pushed the fix-http-kernel-cache-warmer-aggregate branch from e623192 to 2eaa19f Compare October 29, 2021 09:47
@Ahummeling
Copy link
Contributor Author

Ahummeling commented Oct 29, 2021

I'm not sure this can be tested, because I suspect this might happen only in some race conditions.

I am quite sure you can replicate it without race conditions by truncating the file to size 0 before warming cache
as unserialize(file_get_contents('empty_file')) returns false. Which causes a type error as it's not an array

@nicolas-grekas
Copy link
Member

Oh, can you figure out a test case then?

@Ahummeling
Copy link
Contributor Author

Yeah give me a few minutes and I'll push my testcase

@Ahummeling
Copy link
Contributor Author

So without the \is_array check in there, running with php8.0, I get the expected error:

There was 1 error:

1) Symfony\Component\HttpKernel\Tests\CacheWarmer\CacheWarmerAggregateTest::testWarmupEmptyFile
TypeError: array_merge(): Argument #1 must be of type array, bool given

With the \is_array check, it runs fine. I am a little stuck looking for a cleaner solution for getting the $collectDeprecations to be true.
Maybe the \defined('PHPUNIT_COMPOSER_INSTALL') could be taken out? I am really not sure about this part

@@ -50,7 +52,7 @@ public function enableOnlyOptionalWarmers()
*/
public function warmUp($cacheDir)
{
if ($collectDeprecations = $this->debug && !\defined('PHPUNIT_COMPOSER_INSTALL')) {
if ($collectDeprecations = $this->debug && (!\defined('PHPUNIT_COMPOSER_INSTALL') || file_exists(CacheWarmerAggregateTest::$emptyFile))) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please revert this.
I'm fine if we don't test, compared to adding it :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes!
I commited it to show why I thought I couldn't test it, and hoped that maybe I overlooked something.
I will revert the last commit and it should be good to go

@Ahummeling Ahummeling force-pushed the fix-http-kernel-cache-warmer-aggregate branch from fadafdc to 2eaa19f Compare October 29, 2021 19:06
@fabpot
Copy link
Member

fabpot commented Oct 30, 2021

Thank you @Ahummeling.

@fabpot fabpot merged commit 415592a into symfony:4.4 Oct 30, 2021
This was referenced Nov 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants