From 9750150c2e2841af6f142b7e9282d82e188b9e69 Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Tue, 24 Sep 2013 13:55:29 -0700 Subject: [PATCH] Reduce chance for parser cache race conditions * Move cache time definition to happen before parse so it is less likely to override the cache with a stale value and a high cache timestamp. bug: 46014 Change-Id: I021d27885ac260d89232ff11ec3cccf976e44f0f --- includes/WikiPage.php | 6 +++++- includes/parser/ParserCache.php | 14 +++++++------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/includes/WikiPage.php b/includes/WikiPage.php index bfa9956e0c..8ea8e354ad 100644 --- a/includes/WikiPage.php +++ b/includes/WikiPage.php @@ -3510,6 +3510,9 @@ class PoolWorkArticleView extends PoolCounterWork { return false; } + // Reduce effects of race conditions for slow parses (bug 46014) + $cacheTime = wfTimestampNow(); + $time = - microtime( true ); $this->parserOutput = $content->getParserOutput( $this->page->getTitle(), $this->revid, $this->parserOptions ); $time += microtime( true ); @@ -3521,7 +3524,8 @@ class PoolWorkArticleView extends PoolCounterWork { } if ( $this->cacheable && $this->parserOutput->isCacheable() ) { - ParserCache::singleton()->save( $this->parserOutput, $this->page, $this->parserOptions ); + ParserCache::singleton()->save( + $this->parserOutput, $this->page, $this->parserOptions, $cacheTime ); } // Make sure file cache is not used on uncacheable content. diff --git a/includes/parser/ParserCache.php b/includes/parser/ParserCache.php index 7c5eeb4a8f..7053f134c6 100644 --- a/includes/parser/ParserCache.php +++ b/includes/parser/ParserCache.php @@ -223,19 +223,19 @@ class ParserCache { * @param $parserOutput ParserOutput * @param $article Article * @param $popts ParserOptions + * @param $cacheTime Time when the cache was generated */ - public function save( $parserOutput, $article, $popts ) { + public function save( $parserOutput, $article, $popts, $cacheTime = null ) { $expire = $parserOutput->getCacheExpiry(); - if ( $expire > 0 ) { - $now = wfTimestampNow(); + $cacheTime = $cacheTime ?: wfTimestampNow(); $optionsKey = new CacheTime; $optionsKey->mUsedOptions = $parserOutput->getUsedOptions(); $optionsKey->updateCacheExpiry( $expire ); - $optionsKey->setCacheTime( $now ); - $parserOutput->setCacheTime( $now ); + $optionsKey->setCacheTime( $cacheTime ); + $parserOutput->setCacheTime( $cacheTime ); $optionsKey->setContainsOldMagic( $parserOutput->containsOldMagic() ); @@ -245,8 +245,8 @@ class ParserCache { // Save the timestamp so that we don't have to load the revision row on view $parserOutput->setTimestamp( $article->getTimestamp() ); - $parserOutput->mText .= "\n\n"; - wfDebug( "Saved in parser cache with key $parserOutputKey and timestamp $now\n" ); + $parserOutput->mText .= "\n\n"; + wfDebug( "Saved in parser cache with key $parserOutputKey and timestamp $cacheTime\n" ); // Save the parser output $this->mMemc->set( $parserOutputKey, $parserOutput, $expire ); -- 2.20.1