Skip to content

Commit 325354e

Browse files
committed
feature #4903 Reworded the explanation about when a lock is released (javiereguiluz)
This PR was merged into the 2.6 branch. Discussion ---------- Reworded the explanation about when a lock is released | Q | A | ------------- | --- | Doc fix? | yes | New docs? | no | Applies to | 2.6+ | Fixed tickets | #4877 Commits ------- 6e14bac Fixed minor grammar issues e632d49 Fixed the errors spotted by Wouter 89a7320 Added the new documentation inside a "caution" admonition 82a9635 Reworded the explanation about when a lock is released
2 parents 8ad1724 + 6e14bac commit 325354e

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

components/filesystem/lock_handler.rst

+21-5
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,24 @@ 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+
.. caution::
66+
67+
Be aware of the fact that the resource lock is automatically released as soon
68+
as PHP applies the garbage-collection process to the ``LockHandler`` object.
69+
This means that if you refactor the first example shown in this article as
70+
follows::
71+
72+
use Symfony\Component\Filesystem\LockHandler;
73+
74+
if (!(new LockHandler('hello.lock'))->lock()) {
75+
// the resource "hello" is already locked by another process
76+
77+
return 0;
78+
}
79+
80+
Now the code won't work as expected because PHP's garbage collection mechanism
81+
removes the reference to the ``LockHandler`` object and thus, the lock is released
82+
just after it's been created.
83+
84+
Another alternative way to release the lock explicitly when needed is to use the
85+
:method:`Symfony\\Component\\Filesystem\\LockHandler::release` method.

0 commit comments

Comments
 (0)