Skip to content

Commit ec5c11d

Browse files
committed
Fix UniqueConstraintViolationException while insert into oc_file_locks
* fixes #9305 by not being prone to the race condition in insertIfNotExists * fixes #6899 by not using a query that can result in a deadlock * replaces the insertIfNotExists call with an insert which is wrapped into a try-catch block * followup to #12371 Signed-off-by: Morris Jobke <hey@morrisjobke.de>
1 parent 4426800 commit ec5c11d

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

lib/private/Lock/DBLockingProvider.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
namespace OC\Lock;
2828

29+
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
2930
use OC\DB\QueryBuilder\Literal;
3031
use OCP\AppFramework\Utility\ITimeFactory;
3132
use OCP\DB\QueryBuilder\IQueryBuilder;
@@ -133,7 +134,17 @@ public function __construct(
133134

134135
protected function initLockField(string $path, int $lock = 0): int {
135136
$expire = $this->getExpireTime();
136-
return $this->connection->insertIfNotExist('*PREFIX*file_locks', ['key' => $path, 'lock' => $lock, 'ttl' => $expire], ['key']);
137+
138+
try {
139+
$builder = $this->connection->getQueryBuilder();
140+
return $builder->insert('file_locks')
141+
->setValue('key', $builder->createNamedParameter($path))
142+
->setValue('lock', $builder->createNamedParameter($lock))
143+
->setValue('ttl', $builder->createNamedParameter($expire))
144+
->execute();
145+
} catch(UniqueConstraintViolationException $e) {
146+
return 0;
147+
}
137148
}
138149

139150
/**

0 commit comments

Comments
 (0)