From: Tim Düsterhus Date: Tue, 30 Sep 2014 01:00:51 +0000 (+0200) Subject: Fix AtomicWriter race condition on Windows X-Git-Tag: 2.1.0_Alpha_1~290 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=c5216e3582cfcc2a8cc3aee9d3eba76f30aecb5b;p=GitHub%2FWoltLab%2FWCF.git Fix AtomicWriter race condition on Windows --- 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 + } + } } /**