Skip to content

Commit 5155c55

Browse files
committed
fix(lock): return false when run cannot acquire
- return false from run() when lock acquisition fails - update lock docs and tests for the new return value - list LockManager in the core classes user guide Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
1 parent 553c8ea commit 5155c55

4 files changed

Lines changed: 6 additions & 5 deletions

File tree

system/Lock/Lock.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function run(Closure $callback, int $waitSeconds = 0): mixed
6666
$acquired = $waitSeconds > 0 ? $this->block($waitSeconds) : $this->acquire();
6767

6868
if (! $acquired) {
69-
return null;
69+
return false;
7070
}
7171

7272
try {

tests/system/Lock/LockTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,13 @@ public function testRunReleasesLockAfterCallback(): void
157157
$this->assertTrue($this->locks->create('notifications.send', 60)->acquire());
158158
}
159159

160-
public function testRunReturnsNullWhenLockCannotBeAcquired(): void
160+
public function testRunReturnsFalseWhenLockCannotBeAcquired(): void
161161
{
162162
$first = $this->locks->create('notifications.send', 60);
163163
$second = $this->locks->create('notifications.send', 60);
164164

165165
$this->assertTrue($first->acquire());
166-
$this->assertNull($second->run(static fn (): string => 'sent'));
166+
$this->assertFalse($second->run(static fn (): string => 'sent'));
167167
}
168168

169169
public function testLogicalNamesCanContainReservedCacheCharacters(): void

user_guide_src/source/extending/core_classes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ The following is a list of the core system classes that are invoked every time C
5151
* ``CodeIgniter\HTTP\SiteURIFactory``
5252
* ``CodeIgniter\HTTP\URI``
5353
* ``CodeIgniter\HTTP\UserAgent`` (if launched over HTTP)
54+
* ``CodeIgniter\Lock\LockManager``
5455
* ``CodeIgniter\Log\Logger``
5556
* ``CodeIgniter\Log\Handlers\BaseHandler``
5657
* ``CodeIgniter\Log\Handlers\FileHandler``

user_guide_src/source/libraries/locks.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ in a ``finally`` block.
5757

5858
.. literalinclude:: locks/002.php
5959

60-
If the lock cannot be acquired, ``run()`` returns ``null`` and the callback is
60+
If the lock cannot be acquired, ``run()`` returns ``false`` and the callback is
6161
not called.
6262

6363
Blocking
@@ -143,7 +143,7 @@ Class Reference
143143
144144
:param Closure $callback: The callback to run while the lock is held.
145145
:param int $waitSeconds: Maximum number of seconds to wait.
146-
:returns: The callback result, or ``null`` if the lock was not acquired.
146+
:returns: The callback result, or ``false`` if the lock was not acquired.
147147
:rtype: mixed
148148

149149
.. php:method:: release()

0 commit comments

Comments
 (0)