File tree Expand file tree Collapse file tree 1 file changed +20
-5
lines changed Expand file tree Collapse file tree 1 file changed +20
-5
lines changed Original file line number Diff line number Diff 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
6363PHP 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.
You can’t perform that action at this time.
0 commit comments