Skip to content
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

Error in v3.2-rc1 and HTTP cache in production #20560

Closed
nunocodex opened this issue Nov 18, 2016 · 8 comments
Closed

Error in v3.2-rc1 and HTTP cache in production #20560

nunocodex opened this issue Nov 18, 2016 · 8 comments

Comments

@nunocodex
Copy link

I have a small personal project made with the latest dev version but I found a bug with the http cache, this is my sources:

<?php

use Symfony\Component\HttpFoundation\Request;

/** @var \Composer\Autoload\ClassLoader $loader */

# Config autoload
$loader = require __DIR__ . '/../app/autoload.php';
include_once __DIR__ . '/../var/bootstrap.php.cache';

# Cache Loader
if (function_exists('apcu_fetch')) {
    $cache_loader = new \Symfony\Component\ClassLoader\ApcClassLoader(sha1(__DIR__), $loader);
    $cache_loader->register();
    $loader->unregister();
}

# Create kernel instance
$kernel = new MicroKernel('prod', false);
$kernel->loadClassCache();

# Create cache instance
$kernel = new MicroCache($kernel);

# Create request instance
$request = Request::createFromGlobals();

# Setup reverse proxy
Request::enableHttpMethodParameterOverride();
//Request::setTrustedProxies([ '127.0.0.1', $request->server->get('REMOTE_ADDR') ]);
//Request::setTrustedHeaderName(Request::HEADER_FORWARDED, null);

# Give response instance
$response = $kernel->handle($request);
$response->send();

# Bye!
$kernel->terminate($request, $response);

Kernel: https://github.com/nunopress/sf-micro-edition/blob/master/app/MicroKernel.php

If I use $kernel = new MicroCache($kernel); get error:

Cannot declare class Symfony\Component\HttpFoundation\ResponseHeaderBag, because the name is already in use in /var/www/html/var/cache/prod/classes.php on line 3942" while reading response header from upstream

If I enable the AppCache and disable $kernel->loadClassCache(); working but not all 2 in the same time.

For check if is my problem I downgraded to stable version 3.1 and all working fine as expected so I think the problem come with 3.2-rc1 (i not tested with the previous version).

php version:

PHP 7.0.8-0ubuntu0.16.04.3 (cli) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
    with Zend OPcache v7.0.8-0ubuntu0.16.04.3, Copyright (c) 1999-2016, by Zend Technologies

If need more information's ask without problems. Sorry if isn't a good issue post, it's the first time i post one issue for symfony.

@javiereguiluz javiereguiluz changed the title Error in v3.2-rc1 and http cache in production Error in v3.2-rc1 and HTTP cache in production Nov 18, 2016
@nicolas-grekas
Copy link
Member

Can you check where the ResponseHeaderBag class is loaded from the first time? bootstrap.php.cache? Autoloading somewhere (where then?)

@nunocodex
Copy link
Author

nunocodex commented Nov 21, 2016

Hi, after investigate more in depth I see the problem come with the file response, you can found my project here: https://github.com/nunopress/sharex-hosting-uploader

The problem working with export SYMFONY_ENV=prod and use the app.php file when you try to see a files (after cache created) with return $this->file($file, $filename, 'inline'); : https://files.nunopress.com/view/21-11-2016-1479725953.png

I tried too with return new BinaryFileResponse($file, 200); but the problem still.

@nunocodex
Copy link
Author

Ok I think the problem come with composer dump-autoload --optimize because with the latest dev-master without run this command the app working fine in production, but when use this the problem writed here come back. I don't know why on the v3.1.7 (updated now) working fine with composer optimize and with v3.2 not working ...

@rodush
Copy link

rodush commented Dec 2, 2016

I observe similar issue in production (Heroku), when using latest 3.2 (which is released, right?)
Can reproduce it locally as well. Experimented, found out that if I comment out this string
include_once __DIR__ . '/../var/bootstrap.php.cache'; in app.php file, it works fine.
This is strange that issue is not reported yet...
Update:
in my case the error a bit different -

[Fri Dec 02 23:53:10.237860 2016] [:error] [pid 8796] [client 33.33.33.1:60565] PHP Fatal error: Cannot declare interface Symfony\Component\HttpKernel\HttpKernelInterface, because the name is already in use in /var/www/frontier/var/bootstrap.php.cache on line 1309

@eventhorizonpl
Copy link

I've got a similar problem
2016/12/12 17:07:40 [error] 17795#17795: *725 FastCGI sent in stderr: "PHP message: PHP Fatal error: Cannot declare class Symfony\Component\HttpFoundation\Response, be cause the name is already in use in /var/www/prod.test.dk/var/cache/prod/classes.php on line 3777" while reading response header from upstream, client: 89.1 88.72.143, server: test.dk, request: "GET / HTTP/1.0", upstream: "fastcgi://unix:/var/run/php-fpm/php7-fpm.sock:", host: "www.test.dk"

@stof
Copy link
Member

stof commented Dec 12, 2016

@namaless @rodush As a workaround, I suggest you to remove the call to ->loadClassCache. We found out that this class cache was not necessary on PHP 7.
It does not bring any benefit compared to PHP 7 + OPCache + composer optimized autoloader (and can even have a negative impact).

@araines
Copy link

araines commented Dec 14, 2016

@stof out of interest do you know what kind of performance impact that might have running PHP5.6? I've hit this same issue, but unfortunately haven't got a PHP7 compatible system (yet).

@raress96
Copy link

raress96 commented Dec 20, 2016

I have exactly the same problem as the OP.If I downgrade to Symfony 3.1.x it works perfectly without changing the code, so the problem is definitely in Symfony 3.2.x.I upgraded to 3.2.1 but the problem is still there.I am running PHP 7 with OPCache enabled and the default app.php file with the app cache kernel enabled.
I have this error:
PHP Fatal error: Cannot declare class Symfony\\Component\\HttpFoundation\\Response, because the name is already in use in /home/rares.serban/Sites/site.welpe/var/cache/prod/classes.php on line 377

fabpot added a commit that referenced this issue Jan 6, 2017
… (nicolas-grekas)

This PR was merged into the 3.2 branch.

Discussion
----------

[FrameworkBundle] Remove Response* from classes to compile

| Q             | A
| ------------- | ---
| Branch?       | 3.2
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #20560, symfony/demo#411, FriendsOfSymfony/FOSHttpCacheBundle#276
| License       | MIT
| Doc PR        | -

When HttpCache is used, Response is loaded first, then the kernel is booted (on cache miss), which triggers the loading of classes.php. Since 3.2 generates a context free classes.php, the Response class is now included there when it was excluded previously. And boom, "Cannot declare class Symfony\Component\HttpFoundation\Response".

Commits
-------

9ab5982 [FrameworkBundle] Remove Response* from classes to compile
@fabpot fabpot closed this as completed Jan 6, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants