From 86db6ffdae569a94539fa547ccb8dac93002bcd8 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Tue, 25 Oct 2022 11:34:15 +0200 Subject: [PATCH] Retry cache writes if they fail, refs #11076 --- src/Composer/Cache.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Composer/Cache.php b/src/Composer/Cache.php index 8a5ab1a71..4e1fa7429 100644 --- a/src/Composer/Cache.php +++ b/src/Composer/Cache.php @@ -137,6 +137,8 @@ class Cache */ public function write(string $file, string $contents) { + $wasEnabled = $this->enabled === true; + if ($this->isEnabled() && !$this->readOnly) { $file = Preg::replace('{[^'.$this->allowlist.']}i', '-', $file); @@ -146,6 +148,14 @@ class Cache try { return file_put_contents($tempFileName, $contents) !== false && rename($tempFileName, $this->root . $file); } catch (\ErrorException $e) { + // If the write failed despite isEnabled checks passing earlier, rerun the isEnabled checks to + // see if they are still current and recreate the cache dir if needed. Refs https://github.com/composer/composer/issues/11076 + if ($wasEnabled) { + clearstatcache(); + $this->enabled = null; + return $this->write($file, $contents); + } + $this->io->writeError('Failed to write into cache: '.$e->getMessage().'', true, IOInterface::DEBUG); if (Preg::isMatch('{^file_put_contents\(\): Only ([0-9]+) of ([0-9]+) bytes written}', $e->getMessage(), $m)) { // Remove partial file.