Skip to content

Commit 82a9635

Browse files
committed
Reworded the explanation about when a lock is released
1 parent 65b0822 commit 82a9635

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

components/filesystem/lock_handler.rst

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,23 @@ You can pass an optional blocking argument as the first argument to the
6262
``lock()`` method, which defaults to ``false``. If this is set to ``true``, your
6363
PHP code will wait indefinitely until the lock is released by another process.
6464

65-
The resource is automatically released by PHP at the end of the script. In
66-
addition, you can invoke the
67-
:method:`Symfony\\Component\\Filesystem\\LockHandler::release` method to release
68-
the lock explicitly. Once it's released, any other process can lock the
69-
resource.
65+
Beware that the resource lock is automatically released as soon as PHP applies the
66+
garbage-collection process to the ``LockHandler`` object. This means that if you
67+
refactor the first example showed in this article as follows:
68+
69+
.. code-block:: php
70+
71+
use Symfony\Component\Filesystem\LockHandler;
72+
73+
if (!(new LockHandler('hello.lock'))->lock()) {
74+
// the resource "hello" is already locked by another process
75+
76+
return 0;
77+
}
78+
79+
Now the code won't work as expected, because PHP's garbage collection mechanism
80+
removes the reference to the ``LockHandler`` object and thus, the lock is released
81+
just after it's been created.
82+
83+
Another alternative way to release the lock explicitly when needed is to use the
84+
:method:`Symfony\\Component\\Filesystem\\LockHandler::release` method.

0 commit comments

Comments
 (0)