Skip to content

Commit d9d94d7

Browse files
committed
Keep lock until end of PHP process #10614
The lock was effectively useless, because it was released before leaving the method, making it likely to send emails twice.
1 parent 84d5223 commit d9d94d7

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/Service/Mailer.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@
1919
*/
2020
class Mailer
2121
{
22+
/**
23+
* DO NOT REMOVE THIS PROPERTY !
24+
*
25+
* When it is garbage collected, the lock will be released.
26+
* And the lock must only be released at the end of PHP process,
27+
* never at the end of the method.
28+
*
29+
* @var false|resource
30+
*/
31+
private $lock;
32+
2233
public function __construct(
2334
private readonly EntityManager $entityManager,
2435
private readonly MessageRepository $messageRepository,
@@ -119,12 +130,12 @@ private function acquireLock(): void
119130
{
120131
$lockFile = 'data/tmp/mailer.lock';
121132
touch($lockFile);
122-
$lock = fopen($lockFile, 'r+b');
123-
if ($lock === false) {
133+
$this->lock = fopen($lockFile, 'r+b');
134+
if ($this->lock === false) {
124135
throw new Exception('Could not read lock file. This is not normal and might be a permission issue');
125136
}
126137

127-
if (!flock($lock, LOCK_EX | LOCK_NB)) {
138+
if (!flock($this->lock, LOCK_EX | LOCK_NB)) {
128139
$message = LogRepository::MAILER_LOCKED;
129140
_log()->info($message);
130141

0 commit comments

Comments
 (0)