View previous topic :: View next topic |
Author |
Message |
robz Smarty Rookie
Joined: 22 Jul 2011 Posts: 9
|
Posted: Mon Mar 13, 2017 2:00 pm Post subject: Blank pages in Smarty cache |
|
|
Hi there,
I'm having a difficult to reproduce problem when pulling a file from cache. Sometimes my cache file is completely empty. My site has a header, main page and a footer. These get cached separately. Most of the times (say in 99% of the time) everything works fine, but sometimes my users only get to see the header and footer (and not the main page). The only way to fix this is by forcing a cache refresh (or to wait until the cache get refreshed because the lifetime is over). This problem started after a sever upgrade. We went from Apache with libapache2-mod-php5 5.3.3 to Nginx with php-fpm 5.6.30. I tried to find the error in the php and nginx logfiles, but couldn't find anything. In production I use Smarty 3.1.13, but also with the latest Smarty version (3.1.30) this problem persists.
This is the content of an (empty) cache file. In this case: 27025^25dd14254952e422fdd8c8f32cd491a695c37fe6.news_detail.tpl.php
Please note: I changed my paths to [[PATH]].
Code: |
<?php /*%%SmartyHeaderCode:1088937757548857314a21d3-58661479%%*/if(!defined('SMARTY_DIR')) exit('no direct access allowed');
$_valid = $_smarty_tpl->decodeProperties(array (
'file_dependency' =>
array (
'25dd14254952e422fdd8c8f32cd491a695c37fe6' =>
array (
0 => '[[PATH]]live/templates/_main/news_detail.tpl',
1 => 1487853967,
2 => 'file',
),
'dfca31bce1b006e1ef7dbc7f33949a78b352070b' =>
array (
0 => '[[PATH]]/live/templates/social/social_like_buttons.tpl',
1 => 1481021598,
2 => 'file',
),
'893816c687700aeb3544164f0a9436330bc8b423' =>
array (
0 => '[[PATH]]/live/templates/reaction/main_reactions.tpl',
1 => 1463577594,
2 => 'file',
),
'75755ae67d5f670889a4c9350f51c677a4a7dd8b' =>
array (
0 => '[[PATH]]/live/templates/news/sub_news_related.tpl',
1 => 1463577224,
2 => 'file',
),
'51865338427efc2bf8845a496cc223f290d852f2' =>
array (
0 => '[[PATH]]/live/templates/news/sub_news_article.tpl',
1 => 1484601037,
2 => 'file',
),
),
'nocache_hash' => '1088937757548857314a21d3-58661479',
'cache_lifetime' => 600,
'version' => 'Smarty-3.1.13',
'unifunc' => 'content_58c5f644785ba8_61797366',
'has_nocache_code' => false,
),true); /*/%%SmartyHeaderCode%%*/?>
<?php if ($_valid && !is_callable('content_58c5f644785ba8_61797366')) {function content_58c5f644785ba8_61797366($_smarty_tpl) {?><?php }} ?>
|
As you can see the function content_58c5f644785ba8_61797366($_smarty_tpl) is completely empty. In other files the templates html is displayed here. So sometimes, something goes wrong when Smarty tries to save the template to cache. An empty file is cached. Needless to say: there is enough diskspace and all the permissions on directories are set correctly. Most of the times everything works perfectly! There are thousands of correct cache-files in my cache/ directory. Every night I have a cronjob which clears all the cache.
I hope somebody can help me. Thanks in advance! |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Mon Mar 13, 2017 6:09 pm Post subject: |
|
|
Sounds like you have a template that only output a single variable. Which happens to be empty at the moment of this cache_id creation.
I strongly suspect an issue with your site code, not Smarty.
Alternatively, a template file fails to read or its compiled version is empty for given compile_id. |
|
Back to top |
|
robz Smarty Rookie
Joined: 22 Jul 2011 Posts: 9
|
Posted: Mon Mar 13, 2017 9:34 pm Post subject: |
|
|
Thanks for your reply. I greatly appreciate the support.
I do not have templates that output a single variable. My templates have a lot of html and different variables which get assigned.
My php code is like this: On a page request, I check if there is a cached template. If not, I collect information from the database and parse this. If there is a cache file, I just call $Smarty->display(). First for the header file and then for the main file. As shown sometimes this cached file is empty...
Is it possible that a template file sometimes (say once in a thousand requests) fails to read? Sounds bizarre to me.
I don't know what you mean by 'Alternatively, a template file fails to read or its compiled version is empty for given compile_id.'. How do I check this? And what could be the cause?
AnrDaemon wrote: | Sounds like you have a template that only output a single variable. Which happens to be empty at the moment of this cache_id creation.
I strongly suspect an issue with your site code, not Smarty.
Alternatively, a template file fails to read or its compiled version is empty for given compile_id. |
|
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Tue Mar 14, 2017 4:16 pm Post subject: |
|
|
All I can say is that you should dive into your logs. |
|
Back to top |
|
robz Smarty Rookie
Joined: 22 Jul 2011 Posts: 9
|
Posted: Sun Apr 23, 2017 2:44 pm Post subject: |
|
|
AnrDaemon wrote: | All I can say is that you should dive into your logs. |
I know a little more about my problem, because I looked at my Access Logs. I can't find any errors related to this caching problem.
When a page is requested with a HEAD request (so not a GET) and the requested document is not cached, Smarty caching fails. It caches an empty document (as I showed in my first post). When I request the same document with GET everything works as it should.
Does anybody have any clue what happens and what could be the solution? |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Sun Apr 23, 2017 7:31 pm Post subject: |
|
|
robz wrote: | When a page is requested with a HEAD request (so not a GET) and the requested document is not cached | This is your application logic, Smarty by itself do not make such decisions.
I just tested some of my own projects I know do not make any special treatment of HEAD requests, and all cache files were formed correctly regardless of request method. |
|
Back to top |
|
robz Smarty Rookie
Joined: 22 Jul 2011 Posts: 9
|
Posted: Sun Apr 23, 2017 7:49 pm Post subject: |
|
|
Thanks again for the reply!
I forgot to mention one (maybe important) thing. My php file displays three tpl's which are cached separately. The first one (header.tpl) is displayed (and cached) correctly, the second and third one (main_content.tpl and footer.tpl) are empty (when requested with a HEAD request).
Did you use multiple .tpls in your test AnrDaemon? Maybe this changes things. Thanks!
I am working with my server admin to find out what happens differently with the HEAD request, but we haven't found it yet. In my application logic (PHP) I don't do anything different when a page is requested by GET or HEAD. Until very recently I didn't even know about HEAD requests...
As mentioned previously we migrated from Apache to Nginx and this is when the problems started, so my best guess is it has something to do with the server configuration, but I don't know where to look...
AnrDaemon wrote: | robz wrote: | When a page is requested with a HEAD request (so not a GET) and the requested document is not cached | This is your application logic, Smarty by itself do not make such decisions.
I just tested some of my own projects I know do not make any special treatment of HEAD requests, and all cache files were formed correctly regardless of request method. |
|
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Sun Apr 23, 2017 8:38 pm Post subject: |
|
|
In one case it is 12 cache files from six different templates, of which 3 are on different caching times.
But I cleared cache in all cases before running any tests, and checked via server access log that requests made were indeed HEAD.
I'm using both Apache(with PHP-handler) and nginx(with PHP-FPM) and neither show any issues. |
|
Back to top |
|
robz Smarty Rookie
Joined: 22 Jul 2011 Posts: 9
|
Posted: Wed Apr 26, 2017 1:19 pm Post subject: Problem found! |
|
|
Well, I found what was causing my problems with empty cache pages. The Smarty engine uses php output buffers to store (and output/cache) the parsed templates. Problems arise when the output buffer is full. With a GET request the buffer just gets sent to the browser, but with a HEAD request there is nowhere to send it to. And this causes problems with the next output buffer (i.e. the next (sub)template).
I found these people with similar issues with HEAD requests and PHP output buffers:
https://bugs.php.net/bug.php?id=64152
https://www.reddit.com/r/PHPhelp/comments/5chuus/does_php_disable_or_shrink_usage_of_output/
The output buffer on our server is 4kb. When we changed it to 128kb our problem disappeared. Even with a HEAD request everything got cached the way it should be. 128kb is of course a bit large for a production server, so I my final solution was to make some changes in the Smarty engine to prevent empty pages from being cached.
I'm curious if you find the same 'bug' with sufficiently large template files (and a HEAD request), AnrDaemon. N.B. the cached templates are not empty files. They are as posted in my first post. |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Wed Apr 26, 2017 7:32 pm Post subject: |
|
|
Intersting turn of events, I must say.
I have 4k buffer too, and up to 70kb cache files with PHP-FPM 5.6.30 behind nginx.
Can you raise a ticket on GitHub with your analysis?
I wonder if it is possible to trigger this issue in testing environment. |
|
Back to top |
|
robz Smarty Rookie
Joined: 22 Jul 2011 Posts: 9
|
Posted: Mon May 01, 2017 4:50 pm Post subject: |
|
|
Ok. Will do. |
|
Back to top |
|
robz Smarty Rookie
Joined: 22 Jul 2011 Posts: 9
|
|
Back to top |
|
|