Skip to content

Commit 9558b87

Browse files
authored
Merge pull request #63 from reactphp-parallel/3.x-only-close-existing-runtimes
[3.x] Only close existing runtimes
2 parents 08b038c + cabece8 commit 9558b87

File tree

1 file changed

+25
-23
lines changed

1 file changed

+25
-23
lines changed

src/Infinite.php

+25-23
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818

1919
use function array_key_exists;
2020
use function array_pop;
21-
use function assert;
2221
use function count;
23-
use function is_int;
2422
use function Safe\hrtime;
2523
use function spl_object_id;
2624

@@ -108,8 +106,8 @@ public function close(): bool
108106

109107
$this->closed = TRUE_;
110108

111-
foreach ($this->runtimes as $hash => $runtime) {
112-
$this->closeRuntime($hash);
109+
foreach ($this->runtimes as $id => $runtime) {
110+
$this->closeRuntime($id);
113111
}
114112

115113
return TRUE_;
@@ -157,21 +155,20 @@ public function releaseGroup(GroupInterface $group): void
157155

158156
private function getIdleRuntime(): Runtime
159157
{
160-
$hash = array_pop($this->idleRuntimes);
161-
assert(is_int($hash));
158+
$id = array_pop($this->idleRuntimes);
162159

163-
if (array_key_exists($hash, $this->ttlTimers)) {
164-
Loop::cancelTimer($this->ttlTimers[$hash]);
165-
unset($this->ttlTimers[$hash]);
160+
if (array_key_exists($id, $this->ttlTimers)) {
161+
Loop::cancelTimer($this->ttlTimers[$id]);
162+
unset($this->ttlTimers[$id]);
166163
}
167164

168-
return $this->runtimes[$hash];
165+
return $this->runtimes[$id];
169166
}
170167

171168
private function addRuntimeToIdleList(Runtime $runtime): void
172169
{
173-
$hash = spl_object_id($runtime);
174-
$this->idleRuntimes[$hash] = $hash;
170+
$id = spl_object_id($runtime);
171+
$this->idleRuntimes[$id] = $id;
175172
}
176173

177174
private function spawnRuntime(): Runtime
@@ -188,38 +185,43 @@ private function spawnRuntime(): Runtime
188185

189186
private function startTtlTimer(Runtime $runtime): void
190187
{
191-
$hash = spl_object_id($runtime);
188+
$id = spl_object_id($runtime);
192189

193-
$this->ttlTimers[$hash] = Loop::addTimer($this->ttl, function () use ($hash): void {
194-
$this->closeRuntime($hash);
190+
$this->ttlTimers[$id] = Loop::addTimer($this->ttl, function () use ($id): void {
191+
$this->closeRuntime($id);
195192
});
196193
}
197194

198-
private function closeRuntime(int $hash): void
195+
private function closeRuntime(int $id): void
199196
{
200-
$runtime = $this->runtimes[$hash];
197+
if (! array_key_exists($id, $this->runtimes)) {
198+
return;
199+
}
200+
201+
// check if it exists
202+
$runtime = $this->runtimes[$id];
201203
try {
202204
$runtime->close();
203205
} catch (Closed) {
204206
// @ignoreException
205207
}
206208

207-
unset($this->runtimes[$hash]);
209+
unset($this->runtimes[$id]);
208210

209-
if (array_key_exists($hash, $this->idleRuntimes)) {
210-
unset($this->idleRuntimes[$hash]);
211+
if (array_key_exists($id, $this->idleRuntimes)) {
212+
unset($this->idleRuntimes[$id]);
211213
}
212214

213215
if ($this->metrics instanceof Metrics) {
214216
$this->metrics->threads()->gauge(new Label('state', 'idle'))->dcr();
215217
}
216218

217-
if (! array_key_exists($hash, $this->ttlTimers)) {
219+
if (! array_key_exists($id, $this->ttlTimers)) {
218220
return;
219221
}
220222

221-
Loop::cancelTimer($this->ttlTimers[$hash]);
223+
Loop::cancelTimer($this->ttlTimers[$id]);
222224

223-
unset($this->ttlTimers[$hash]);
225+
unset($this->ttlTimers[$id]);
224226
}
225227
}

0 commit comments

Comments
 (0)