• Hi,

    I should note that I am not experiencing any actual errors or issues on the site itself and the plugin appears to be working fine. However, I’ve noticed the following type of warning occurring on a regular basis. My guess is that maybe this happens when the cache expired and the file doesn’t exist anymore, and it doesn’t cause any actual problem. Aside from cluttering up my php error log, of course. Any reason I should be concerned, or, if not, could this be handled better in a future update?

    PHP Warning: file_get_contents(/wp-content/cache/autoptimize/autoptimize_c03019ef76e0a86a4c6f7b43a608518a.php.none): failed to open stream: No such file or directory in /wp-content/plugins/autoptimize/classes/autoptimizeCache.php on line 37

Viewing 15 replies - 1 through 15 (of 17 total)
  • Plugin Author Optimizing Matters

    (@optimizingmatters)

    Might be a race condition of when not writing the autoptimized files as static files where they are in the process of being written. Maybe try to see if the error goes away by ticking the “save as static files”-option?

    frank

    Thread Starter jpeek

    (@jpeek)

    That appears to have resolved the issue, thank you.

    Thread Starter jpeek

    (@jpeek)

    Actually, no, I spoke too soon ??

    Same error, though this time on line 59, it appears.

    • This reply was modified 7 years, 1 month ago by jpeek.
    Plugin Author Optimizing Matters

    (@optimizingmatters)

    bummer .. no idea what the problem might be I’m afraid @jpeek .. you’re not writing to an NFS- or similar remote filesystem are you? because that would increase chances of race-conditions significantly?

    Thread Starter jpeek

    (@jpeek)

    Nah, just a CentOS 7.4 system. No worries. Do you think this causes any actual issues to my visitors? I’ve not yet seen any problems myself.

    Plugin Author Optimizing Matters

    (@optimizingmatters)

    well .. what is weird is that on line 59 there is no file_get_contents, but a file_put_contents? so can you copy/ paste the exact error?

    orlov0562

    (@orlov0562)

    Hi there,

    At the beginning want to say thank you to author for great plugin!

    At the second want to sorry that write in this topic, but I think no need to make new one cuz I have the same problem.

    I have also faced with same problem (warning on line 59) on vps and fixed it with next code, that suppress warnings and attempt 10 tries to write to the file with random sleep from 50 to 250 ms if first try failed. Looks like errors have gone.

    this ( autoptimizeCache.php on line 59 ):

    file_put_contents($this->cachedir.$this->filename, $code, LOCK_EX);

    replace with this

    
    $errorlevel = error_reporting();
    error_reporting($errorlevel & ~E_WARNING);
    for ($try=0; $try<10; $try++) {
        if (file_put_contents($this->cachedir.$this->filename, $code, LOCK_EX)) {
            break;
        } else {
            usleep(rand(50, 250));
        }
    }
    error_reporting($errorlevel);
    

    You can also just ignore this warning in next way (without any sleep)

    
    $errorlevel = error_reporting();
    error_reporting($errorlevel & ~E_WARNING);
    file_put_contents($this->cachedir.$this->filename, $code, LOCK_EX);
    error_reporting($errorlevel);
    

    in this case the code just ignore warning that generated by the file_put_contents and keep logs clean

    the another, but not recomended way is to put “@” at the beginning of this function:
    @file_put_contents($this->cachedir.$this->filename, $code, LOCK_EX);

    And the best way, is to replace file_put_contents with class method that will use flock to make sure that code can set lock before made any operations. Something like this:

    
    protected static function write_file($filepath, $content) {
    
        if (!is_writable($filepath)) thrown new Exception('File '.$filepath.' is not writable');
    
        $fp = @fopen($filepath, 'w');
        if (!is_resource($fp)) thrown new Exception('Can\'t open file '.$filepath);
    
        $lock = flock($fp, LOCK_EX)
        if (!$lock) {
            fclose($fp);
            thrown new Exception('Can\'t lock file '.$filepath);
        }
    
        if (fwrite($fp, $content) === FALSE) {
            thrown new Exception('Can\'t write to file '.$filepath);
        }
    
        flock($fp, LOCK_UN);
        fclose($fp);
    
        return true;
    
    }
    

    and then in code

    
    try {
         self::write_file($filepath, $content);
    } catch(Exception $ex) {
        \\ oops something going wrong
        \\ make decision what to do
    }
    

    P.S. This is information for the plugin author. If you user then think twice if you decide to apply this changes manually to the plugin code. At least it is bad idea, cuz all changes will be automatically overwritten after plugin updated.

    Plugin Author Optimizing Matters

    (@optimizingmatters)

    @orlov0562 can you also try removing the LOCK_EX from file_put_contents to see if this fixes the warnings?

    I _think_ the reason is that 2 processes are trying to write to the file at _almost_ the same time, but LOCK_EX prevents that. not entirely sure what will happen if they both are allowed to write, but I assume the last process to do so will be the one writing the file.

    a bit hackish maybe, but well .. ??

    Hello @optimizingmatters,

    Thank you for your reply. I have tested solution with removing LOCK_EX from file_put_contents on line 59 in file
    /wp-content/plugins/autoptimize/classes/autoptimizeCache.php
    and yes, it fixed the problem – the errors doesn’t appears anymore in httpd log and looks like it doesn’t affect to the site stability.

    The only one thing that bothers me, that I understand that this solution removes the logs errors, but two processes can write into file at the same time and that can lead to errors in data.

    The another problem is that I have a few WP sites, so will be very cool if you append some solution from the box to avoid this error. This will help to avoid manual edits in plugin files after plugin updates come. It can be setting “Don’t use cache file exclusive lock” into admin panel or something like this.

    JFYI, first time I faced with this error on slow VPS, so I thought that the problem is coming from there, but a week ago I have made new installation on empty dedicated server with a lot of free CPU and RAM and I have got same error right after google bot come to index three pages of the site.

    Thank you for great plugin and have a nice day!

    I’m also getting this warning across multiple WordPress sites so I would also like to add my voice to the request for a solution with the next update so I don’t have to keep editing the plugin. Cheers and thanks for a great plugin.

    Plugin Author Optimizing Matters

    (@optimizingmatters)

    this should be fixed in Autopitimize 2.4, which is in beta now @dubsy. Would be great if you could test to confirm if it works! ??

    I have seen this error a lot in my logs.

    I know above it was said that it did not seem to affect things, but I suddenly had no sales one day and I checked and I saw this error and saw nothing wrong on my site. Then when i was not logged in as admin and i visited the site, there were no styles loaded at all on my cart and checkout pages.

    Logged in and they were back.

    Now today I have not had sales for the last 30 hours (I average 8 orders a day) check my logs and see tons of this same error again. Is it the cause? I do not know. Things appear right on the site, but last time arround, I was just not able to see the problem all the time because it was only on some pages and for certain users.

    Plugin Author Optimizing Matters

    (@optimizingmatters)

    can you share your URL @corelevel so I can take a peak?

    Sorry for hijacking this thread, it seems that I am also having problems with the PHP file not found, taking my site about 2 extra seconds to load. Also, I am having issues with certains page turning up blank.

    Plugin Author Optimizing Matters

    (@optimizingmatters)

    probably unrelated, can you open a new topic in that case @fouzynadjieb and make sure to share your URL?

Viewing 15 replies - 1 through 15 (of 17 total)
  • The topic ‘Failed to Open Stream’ is closed to new replies.