Skip to content

Commit

Permalink
! Fixed... Maybe... Maybe... Finally fixed race conditions in PHP fil…
Browse files Browse the repository at this point in the history
…e caching. Needs testing, but at least I'm no longer able to reproduce the problem. (Subs.php)

@ Imagine you're uploading a modified file to your server, and it has several visitors, and one of them is loading a page at the same time. The cacher will look for a cached version, will find it's outdated, and rebuild it. Now, here's the trick: if this happens DURING the upload, the file won't be complete, but it'll still be used for caching. The trick here, since we don't know the final expected filesize, is to instead check the file date. If it's very recent (0, 1 or 2 seconds old), we'll wait for one more second and check again, until the file is old enough. This isn't perfect, but should handle 99% of all possible race conditions.
  • Loading branch information
Nao committed Feb 26, 2017
1 parent 47060c5 commit 9affd1f
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions core/app/Subs-CachePHP.php
Expand Up @@ -15,6 +15,17 @@
// Build the cached version of our source file
function cache_source_file($source, $dest)
{
// First of all, make sure we're not caching a partially uploaded file. Give it a few seconds to finish.
$tries = 8;
clearstatcache(true, $source);
while (time() - filemtime($source) < 3 && $tries--)
{
sleep(1);
clearstatcache(true, $source);
}
if ($tries < 0)
exit('An error occurred while loading this page; please contact an administrator if this happens repeatedly.');

$dest_locked = substr($dest, 0, -4) . '-' . mt_rand(999, 999999999) . '.php';
apply_plugin_mods($source, $dest_locked);
minify_php($dest_locked);
Expand Down

0 comments on commit 9affd1f

Please sign in to comment.