r106162 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r106161‎ | r106162 | r106163 >
Date:01:58, 14 December 2011
Author:aaron
Status:deferred
Tags:
Comment:
* Altered TempFSFile purging to only be deferred for non-cli scripts, avoids memory "leaking". Updated some callers using a new TempFSFile::bind() function to extend the file lifetime.
* Related code comment improvements.
Modified paths:
  • /branches/FileBackend/phase3/includes/filerepo/FileRepo.php (modified) (history)
  • /branches/FileBackend/phase3/includes/filerepo/backend/FileBackend.php (modified) (history)
  • /branches/FileBackend/phase3/includes/filerepo/file/File.php (modified) (history)
  • /branches/FileBackend/phase3/includes/filerepo/file/TempFSFile.php (modified) (history)
  • /branches/FileBackend/phase3/includes/upload/UploadBase.php (modified) (history)

Diff [purge]

Index: branches/FileBackend/phase3/includes/upload/UploadBase.php
@@ -239,8 +239,11 @@
240240 function getRealPath( $srcPath ) {
241241 $repo = RepoGroup::singleton()->getLocalRepo();
242242 if ( $repo->isVirtualUrl( $srcPath ) ) {
 243+ // @TODO: just make uploads work with storage paths
243244 // UploadFromStash loads files via virtuals URLs
244 - return $repo->getLocalCopy( $srcPath )->getPath();
 245+ $tmpFile = $repo->getLocalCopy( $srcPath );
 246+ $tmpFile->bind( $this ); // keep alive with $thumb
 247+ return $tmpFile->getPath();
245248 }
246249 return $srcPath;
247250 }
Index: branches/FileBackend/phase3/includes/filerepo/file/TempFSFile.php
@@ -19,7 +19,8 @@
2020 protected static $instances = array();
2121
2222 /**
23 - * Make a new temporary file on the file system
 23+ * Make a new temporary file on the file system.
 24+ * Temporary files may be purged when the file object falls out of scope.
2425 *
2526 * @param $prefix string
2627 * @param $extension string
@@ -42,7 +43,9 @@
4344 }
4445 }
4546 $tmpFile = new self( $path );
46 - self::$instances[] = $tmpFile; // defer purge till shutdown
 47+ if ( php_sapi_name() != 'cli' ) {
 48+ self::$instances[] = $tmpFile; // defer purge till shutdown
 49+ }
4750 return $tmpFile;
4851 }
4952
@@ -60,17 +63,28 @@
6164 }
6265
6366 /**
64 - * Flag to not clean up after the temporary file
 67+ * Clean up the temporary file only after an object goes out of scope
6568 *
 69+ * @param $object Object
6670 * @return void
6771 */
 72+ public function bind( $object ) {
 73+ if ( is_object( $object ) ) {
 74+ $object->tempFSFileReferences[] = $this;
 75+ }
 76+ }
 77+
 78+ /**
 79+ * Set flag to not clean up after the temporary file
 80+ *
 81+ * @return void
 82+ */
6883 public function preserve() {
6984 $this->canDelete = false;
7085 }
7186
7287 /**
73 - * Cleans up after the temporary file by deleting it.
74 - * This is done on shutdown after PHP kills self::$instances.
 88+ * Cleans up after the temporary file by deleting it
7589 */
7690 function __destruct() {
7791 if ( $this->canDelete ) {
Index: branches/FileBackend/phase3/includes/filerepo/file/File.php
@@ -783,6 +783,7 @@
784784
785785 // Actually render the thumbnail
786786 $thumb = $this->handler->doTransform( $this, $tmpThumbPath, $thumbUrl, $params );
 787+ $tmpFile->bind( $thumb ); // keep alive with $thumb
787788
788789 // Ignore errors if requested
789790 if ( !$thumb ) {
Index: branches/FileBackend/phase3/includes/filerepo/backend/FileBackend.php
@@ -274,10 +274,11 @@
275275
276276 /**
277277 * Returns a file system file, identical to the file at a storage path.
278 - * The file return is either:
 278+ * The file returned is either:
279279 * a) A local copy of the file at a storage path in the backend.
280280 * The temporary copy will have the same extension as the source.
281281 * b) An original of the file at a storage path in the backend.
 282+ * Temporary files may be purged when the file object falls out of scope.
282283 *
283284 * Write operations should *never* be done on this file as some backends
284285 * may do internal tracking or may be instances of FileBackendMultiWrite.
@@ -294,6 +295,7 @@
295296 /**
296297 * Get a local copy on disk of the file at a storage path in the backend.
297298 * The temporary copy will have the same file extension as the source.
 299+ * Temporary files may be purged when the file object falls out of scope.
298300 *
299301 * $params include:
300302 * src : source storage path
Index: branches/FileBackend/phase3/includes/filerepo/FileRepo.php
@@ -1142,10 +1142,10 @@
11431143
11441144 /**
11451145 * Get a local FS copy of a file with a given virtual URL/storage path.
1146 - * Returns null on failure.
 1146+ * Temporary files may be purged when the file object falls out of scope.
11471147 *
11481148 * @param $virtualUrl string
1149 - * @return TempFSFile|null
 1149+ * @return TempFSFile|null Returns null on failure
11501150 */
11511151 public function getLocalCopy( $virtualUrl ) {
11521152 $path = $this->resolveToStoragePath( $virtualUrl );
@@ -1155,10 +1155,10 @@
11561156 /**
11571157 * Get a local FS file with a given virtual URL/storage path.
11581158 * The file is either an original or a copy. It should not be changed.
1159 - * Returns null on failure.
 1159+ * Temporary files may be purged when the file object falls out of scope.
11601160 *
11611161 * @param $virtualUrl string
1162 - * @return FSFile|null
 1162+ * @return FSFile|null Returns null on failure.
11631163 */
11641164 public function getLocalReference( $virtualUrl ) {
11651165 $path = $this->resolveToStoragePath( $virtualUrl );

Follow-up revisions

RevisionCommit summaryAuthorDate
r106236* Replaced getHash() function with getSha1Base36(). This standardizes the has...aaron19:59, 14 December 2011

Status & tagging log