Skip to content

Commit a3f6457

Browse files
committed
minor symfony#14179 Fix bad example of lock (jderusse)
This PR was submitted for the 4.4 branch but it was merged into the 3.4 branch instead. Discussion ---------- Fix bad example of lock I just realized that example in lock were BAD. If you don't check the return of `$lock->acquire()` you don't know if the lock has been acquired PR against 4.4 because currently maintained, but bug exists since 3.4 Commits ------- 5233c2a Fix bad example of lock
2 parents a53de78 + 5233c2a commit a3f6457

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

components/lock.rst

+9-3
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,9 @@ method, the resource will stay locked until the timeout::
117117
// create an expiring lock that lasts 30 seconds
118118
$lock = $factory->createLock('charts-generation', 30);
119119

120-
$lock->acquire();
120+
if (!$lock->acquire()) {
121+
return;
122+
}
121123
try {
122124
// perform a job during less than 30 seconds
123125
} finally {
@@ -136,7 +138,9 @@ to reset the TTL to its original value::
136138
// ...
137139
$lock = $factory->createLock('charts-generation', 30);
138140

139-
$lock->acquire();
141+
if (!$lock->acquire()) {
142+
return;
143+
}
140144
try {
141145
while (!$finished) {
142146
// perform a small part of the job.
@@ -366,7 +370,9 @@ Using the above methods, a more robust code would be::
366370
// ...
367371
$lock = $factory->createLock('invoice-publication', 30);
368372

369-
$lock->acquire();
373+
if (!$lock->acquire()) {
374+
return;
375+
}
370376
while (!$finished) {
371377
if ($lock->getRemainingLifetime() <= 5) {
372378
if ($lock->isExpired()) {

0 commit comments

Comments
 (0)