Skip to content

Commit d7b8991

Browse files
Do not perform deletion operations during traversal (#941)
* Do not perform deletion operations during traversal (all keys can be taken out for deletion after traversal) Do not perform deletion operations during traversal (all keys can be taken out for deletion after traversal) * fix bug * fix bug * fix bug * fix bug * Update EnsureRequestsDontExceedMaxExecutionTime.php --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 919a4ae commit d7b8991

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

src/Swoole/Actions/EnsureRequestsDontExceedMaxExecutionTime.php

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,33 @@ public function __construct(
2323
*/
2424
public function __invoke()
2525
{
26+
$rows = [];
27+
2628
foreach ($this->timerTable as $workerId => $row) {
2729
if ((time() - $row['time']) > $this->maxExecutionTime) {
28-
$this->timerTable->del($workerId);
30+
$rows[$workerId] = $row;
31+
}
32+
}
2933

30-
if ($this->server instanceof Server && ! $this->server->exists($row['fd'])) {
31-
continue;
32-
}
34+
foreach ($rows as $workerId => $row) {
35+
if ($this->timerTable->get($workerId, 'fd') !== $row['fd']) {
36+
continue;
37+
}
38+
39+
$this->timerTable->del($workerId);
40+
41+
if ($this->server instanceof Server && ! $this->server->exists($row['fd'])) {
42+
continue;
43+
}
3344

34-
$this->extension->dispatchProcessSignal($row['worker_pid'], SIGKILL);
45+
$this->extension->dispatchProcessSignal($row['worker_pid'], SIGKILL);
3546

36-
if ($this->server instanceof Server) {
37-
$response = Response::create($this->server, $row['fd']);
47+
if ($this->server instanceof Server) {
48+
$response = Response::create($this->server, $row['fd']);
3849

39-
if ($response) {
40-
$response->status(408);
41-
$response->end();
42-
}
50+
if ($response) {
51+
$response->status(408);
52+
$response->end();
4353
}
4454
}
4555
}

tests/EnsureRequestsDontExceedMaxExecutionTimeTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public function test_process_is_killed_if_current_request_exceeds_max_execution_
1717
$table['fake-worker-id'] = [
1818
'worker_pid' => 111,
1919
'time' => time() - 60,
20+
'fd' => 1,
2021
];
2122

2223
$action = new EnsureRequestsDontExceedMaxExecutionTime(
@@ -39,4 +40,9 @@ public function del($workerId)
3940
{
4041
$this->deleted[] = $workerId;
4142
}
43+
44+
public function get($workerId, $field = null)
45+
{
46+
return 1;
47+
}
4248
}

0 commit comments

Comments
 (0)