WP Super Cache and half-written cache files
-
While browsing a supercached page that should have been generated by WP Super Cache, I saw what appeared to be a half-written page — while some of the page was present in the browser, the source was basically just cut off half way through.
A reload fixed it, and it’s possible that it may have been just a broken page download or something. However, looking at the source code, it appears that this could happen due to the way WP Super Cache works.
In wp-cache-phase2.php, it fopen()s the supercache file for writing, then uses fputs() to write the data. However, I don’t believe fputs() is atomic — in other words, it appears to be possible for other clients (apache) reading the same file to see a half-written file while fputs is in progress.
The PHP documentation for fputs() says it’s identical to fwrite(), which has this note:
“If handle was fopen()ed in append mode, fwrite()s are atomic (unless the size of string exceeds the filesystem’s block size, on some platforms, and as long as the file is on a local filesystem).”
This strongly implies that writes are *not* atomic if the file wasn’t opened in append mode, which is the case here.
The fix for this would seem to be to fopen/fputs/fclose a temporary file, then rename() it afterwards. If you’d like a patch, let me know and I’ll be glad to send one.
— Robert L Mathews, Tiger Technologies
- The topic ‘WP Super Cache and half-written cache files’ is closed to new replies.