Skip to content

Commit 2f1c74d

Browse files
authored
Merge pull request #51920 from nextcloud/newfolder-race-improvements
fix: improve handling of newFolder race condition handling
2 parents d4b3808 + 99364ad commit 2f1c74d

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

lib/private/Files/Node/Folder.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,21 @@ public function newFolder($path) {
126126
$fullPath = $this->getFullPath($path);
127127
$nonExisting = new NonExistingFolder($this->root, $this->view, $fullPath);
128128
$this->sendHooks(['preWrite', 'preCreate'], [$nonExisting]);
129-
if (!$this->view->mkdir($fullPath) && !$this->view->is_dir($fullPath)) {
130-
throw new NotPermittedException('Could not create folder "' . $fullPath . '"');
129+
if (!$this->view->mkdir($fullPath)) {
130+
// maybe another concurrent process created the folder already
131+
if (!$this->view->is_dir($fullPath)) {
132+
throw new NotPermittedException('Could not create folder "' . $fullPath . '"');
133+
} else {
134+
// we need to ensure we don't return before the concurrent request has finished updating the cache
135+
$tries = 5;
136+
while (!$this->view->getFileInfo($fullPath)) {
137+
if ($tries < 1) {
138+
throw new NotPermittedException('Could not create folder "' . $fullPath . '", folder exists but unable to get cache entry');
139+
}
140+
usleep(5 * 1000);
141+
$tries--;
142+
}
143+
}
131144
}
132145
$parent = dirname($fullPath) === $this->getPath() ? $this : null;
133146
$node = new Folder($this->root, $this->view, $fullPath, null, $parent);

0 commit comments

Comments
 (0)