From c5216e3582cfcc2a8cc3aee9d3eba76f30aecb5b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tim=20D=C3=BCsterhus?= Date: Tue, 30 Sep 2014 03:00:51 +0200 Subject: [PATCH] Fix AtomicWriter race condition on Windows --- .../files/lib/system/io/AtomicWriter.class.php | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/wcfsetup/install/files/lib/system/io/AtomicWriter.class.php b/wcfsetup/install/files/lib/system/io/AtomicWriter.class.php index 9fecdac719..66a83bae4f 100644 --- a/wcfsetup/install/files/lib/system/io/AtomicWriter.class.php +++ b/wcfsetup/install/files/lib/system/io/AtomicWriter.class.php @@ -49,7 +49,7 @@ class AtomicWriter extends File { } catch (SystemException $e) { // allow at most 5 failures - if (++$i == 5) { + if (++$i === 5) { throw $e; } } @@ -90,7 +90,21 @@ class AtomicWriter extends File { flock($this->resource, LOCK_UN); fclose($this->resource); - rename($this->filename, $this->targetFilename); + $i = 0; + while (true) { + try { + rename($this->filename, $this->targetFilename); + break; + } + catch (SystemException $e) { + // rename may fail on Windows with a high number + // of concurrent requests + // retry up to 5 times with a random sleep + if (++$i === 5) throw $e; + + usleep(mt_rand(0, .1e6)); // 0 to .1 seconds + } + } } /** -- 2.20.1