From 938024c6f6b0f8ba276a3615acfeb0bbb90c25a2 Mon Sep 17 00:00:00 2001 From: Donncha O Caoimh <5656673+donnchawp@users.noreply.github.com> Date: Tue, 26 Nov 2024 13:00:30 +0000 Subject: [PATCH 1/3] Check that there is cached content to display in browser --- projects/plugins/super-cache/wp-cache-phase2.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/projects/plugins/super-cache/wp-cache-phase2.php b/projects/plugins/super-cache/wp-cache-phase2.php index 392d25bf258d0..5df344a759793 100644 --- a/projects/plugins/super-cache/wp-cache-phase2.php +++ b/projects/plugins/super-cache/wp-cache-phase2.php @@ -301,6 +301,16 @@ function wp_cache_serve_cache_file() { } header( 'Last-Modified: ' . $local_mod_time ); } + + /* + * On the rare occasion that the cache file is empty, maybe due to a race condition + * between the file check and the file_get_contents call, we'll just regenerate the cache. + */ + if ( false === $cachefiledata ) { + wp_cache_debug( 'The cached file could not be read. Must generate a new one.' ); + return false; + } + echo $cachefiledata; exit(); } else { From 792bf03c7eeac2d431a75412aef9fb09f52ae841 Mon Sep 17 00:00:00 2001 From: Donncha O Caoimh <5656673+donnchawp@users.noreply.github.com> Date: Tue, 26 Nov 2024 13:02:33 +0000 Subject: [PATCH 2/3] changelog --- .../changelog/update-super-cache-cache-file-race-condition | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 projects/plugins/super-cache/changelog/update-super-cache-cache-file-race-condition diff --git a/projects/plugins/super-cache/changelog/update-super-cache-cache-file-race-condition b/projects/plugins/super-cache/changelog/update-super-cache-cache-file-race-condition new file mode 100644 index 0000000000000..142a0ba3b9648 --- /dev/null +++ b/projects/plugins/super-cache/changelog/update-super-cache-cache-file-race-condition @@ -0,0 +1,4 @@ +Significance: patch +Type: fixed + +Caching: make sure there is cache content to serve, even if the cache file was found From 9749f8e8bfa3a8ef670c7761b5b592ed38e19aca Mon Sep 17 00:00:00 2001 From: Donncha O Caoimh <5656673+donnchawp@users.noreply.github.com> Date: Tue, 26 Nov 2024 13:28:11 +0000 Subject: [PATCH 3/3] Check for cache contents earlier --- .../plugins/super-cache/wp-cache-phase2.php | 39 +++++++++++++------ 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/projects/plugins/super-cache/wp-cache-phase2.php b/projects/plugins/super-cache/wp-cache-phase2.php index 5df344a759793..44287b4635f1e 100644 --- a/projects/plugins/super-cache/wp-cache-phase2.php +++ b/projects/plugins/super-cache/wp-cache-phase2.php @@ -227,18 +227,44 @@ function wp_cache_serve_cache_file() { if ( $wp_cache_gzip_encoding ) { if ( file_exists( $file . '.gz' ) ) { $cachefiledata = file_get_contents( $file . '.gz' ); + + if ( false === $cachefiledata ) { + wp_cache_debug( 'The cached gzip file could not be read. Must generate a new one.' ); + return false; + } + wp_cache_debug( "Fetched gzip static page data from supercache file using PHP. File: $file.gz" ); } else { - $cachefiledata = gzencode( file_get_contents( $file ), 6, FORCE_GZIP ); + $cachefiledata = file_get_contents( $file ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents + + if ( false === $cachefiledata ) { + wp_cache_debug( 'The cached file could not be read. Must generate a new one.' ); + return false; + } + + $cachefiledata = gzencode( $cachefiledata, 6, FORCE_GZIP ); wp_cache_debug( "Fetched static page data from supercache file using PHP and gzipped it. File: $file" ); } } else { $cachefiledata = file_get_contents( $file ); + + if ( false === $cachefiledata ) { + wp_cache_debug( 'The cached file could not be read. Must generate a new one.' ); + return false; + } + wp_cache_debug( "Fetched static page data from supercache file using PHP. File: $file" ); } } else { // get dynamic data from filtered file - $cachefiledata = do_cacheaction( 'wpsc_cachedata', file_get_contents( $file ) ); + $cachefiledata = file_get_contents( $file ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents + + if ( false === $cachefiledata ) { + wp_cache_debug( 'The cached file could not be read. Must generate a new one.' ); + return false; + } + + $cachefiledata = do_cacheaction( 'wpsc_cachedata', $cachefiledata ); if ( $wp_cache_gzip_encoding ) { $cachefiledata = gzencode( $cachefiledata, 6, FORCE_GZIP ); wp_cache_debug( "Fetched dynamic page data from supercache file using PHP and gzipped it. File: $file" ); @@ -302,15 +328,6 @@ function wp_cache_serve_cache_file() { header( 'Last-Modified: ' . $local_mod_time ); } - /* - * On the rare occasion that the cache file is empty, maybe due to a race condition - * between the file check and the file_get_contents call, we'll just regenerate the cache. - */ - if ( false === $cachefiledata ) { - wp_cache_debug( 'The cached file could not be read. Must generate a new one.' ); - return false; - } - echo $cachefiledata; exit(); } else {