Skip to content

Commit 994948b

Browse files
#47 fix edge case where the lock is acquired during the deadline
1 parent c3ee9d7 commit 994948b

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

classes/util/Loop.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class Loop
3939
/**
4040
* @var bool True while code execution is repeating.
4141
*/
42-
private $looping;
42+
private $looping = false;
4343

4444
/**
4545
* Sets the timeout. The default is 3 seconds.
@@ -57,7 +57,6 @@ public function __construct(int $timeout = 3)
5757
}
5858

5959
$this->timeout = $timeout;
60-
$this->looping = false;
6160
}
6261

6362
/**
@@ -98,7 +97,10 @@ public function execute(callable $code)
9897
for ($i = 0; $this->looping && microtime(true) < $deadline; ++$i) {
9998
$result = $code();
10099
if (!$this->looping) { // @phpstan-ignore-line
101-
break;
100+
/*
101+
* The $code callback has called $this->end() and the lock has been acquired.
102+
*/
103+
return $result;
102104
}
103105

104106
// Calculate max time remaining, don't sleep any longer than that.
@@ -120,10 +122,6 @@ public function execute(callable $code)
120122
usleep($usecToSleep);
121123
}
122124

123-
if (microtime(true) >= $deadline) {
124-
throw TimeoutException::create($this->timeout);
125-
}
126-
127-
return $result;
125+
throw TimeoutException::create($this->timeout);
128126
}
129127
}

0 commit comments

Comments
 (0)