Skip to content

Commit

Permalink
Merge pull request #42052 from nextcloud/fix/carddav/create-sab-concu…
Browse files Browse the repository at this point in the history
…rrently
  • Loading branch information
skjnldsv authored Nov 6, 2024
2 parents 3c42906 + 0bb68ab commit 373ad6c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
1 change: 1 addition & 0 deletions apps/dav/lib/CardDAV/CardDavBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ public function updateAddressBook($addressBookId, \Sabre\DAV\PropPatch $propPatc
* @param array $properties
* @return int
* @throws BadRequest
* @throws Exception
*/
public function createAddressBook($principalUri, $url, array $properties) {
if (strlen($url) > 255) {
Expand Down
33 changes: 26 additions & 7 deletions apps/dav/lib/CardDAV/SyncService.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use OCP\AppFramework\Db\TTransactional;
use OCP\AppFramework\Http;
use OCP\DB\Exception;
use OCP\Http\Client\IClient;
use OCP\Http\Client\IClientService;
use OCP\IConfig;
Expand Down Expand Up @@ -89,15 +90,33 @@ public function syncRemoteAddressBook(string $url, string $userName, string $add
* @throws \Sabre\DAV\Exception\BadRequest
*/
public function ensureSystemAddressBookExists(string $principal, string $uri, array $properties): ?array {
return $this->atomic(function () use ($principal, $uri, $properties) {
$book = $this->backend->getAddressBooksByUri($principal, $uri);
if (!is_null($book)) {
return $book;
try {
return $this->atomic(function () use ($principal, $uri, $properties) {
$book = $this->backend->getAddressBooksByUri($principal, $uri);
if (!is_null($book)) {
return $book;
}
$this->backend->createAddressBook($principal, $uri, $properties);

return $this->backend->getAddressBooksByUri($principal, $uri);
}, $this->dbConnection);
} catch (Exception $e) {
// READ COMMITTED doesn't prevent a nonrepeatable read above, so
// two processes might create an address book here. Ignore our
// failure and continue loading the entry written by the other process
if ($e->getReason() !== Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
throw $e;
}
$this->backend->createAddressBook($principal, $uri, $properties);

return $this->backend->getAddressBooksByUri($principal, $uri);
}, $this->dbConnection);
// If this fails we might have hit a replication node that does not
// have the row written in the other process.
// TODO: find an elegant way to handle this
$ab = $this->backend->getAddressBooksByUri($principal, $uri);
if ($ab === null) {
throw new Exception('Could not create system address book', $e->getCode(), $e);
}
return $ab;
}
}

private function prepareUri(string $host, string $path): string {
Expand Down

0 comments on commit 373ad6c

Please sign in to comment.