From d7b8991270eb57eef83be7de62ba04c1289dd65b Mon Sep 17 00:00:00 2001 From: "hugh.li" Date: Fri, 9 Aug 2024 20:25:04 +0800 Subject: [PATCH] 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 --- ...sureRequestsDontExceedMaxExecutionTime.php | 32 ++++++++++++------- ...RequestsDontExceedMaxExecutionTimeTest.php | 6 ++++ 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/src/Swoole/Actions/EnsureRequestsDontExceedMaxExecutionTime.php b/src/Swoole/Actions/EnsureRequestsDontExceedMaxExecutionTime.php index 787ea9fa2..8437ab362 100644 --- a/src/Swoole/Actions/EnsureRequestsDontExceedMaxExecutionTime.php +++ b/src/Swoole/Actions/EnsureRequestsDontExceedMaxExecutionTime.php @@ -23,23 +23,33 @@ public function __construct( */ public function __invoke() { + $rows = []; + foreach ($this->timerTable as $workerId => $row) { if ((time() - $row['time']) > $this->maxExecutionTime) { - $this->timerTable->del($workerId); + $rows[$workerId] = $row; + } + } - if ($this->server instanceof Server && ! $this->server->exists($row['fd'])) { - continue; - } + foreach ($rows as $workerId => $row) { + if ($this->timerTable->get($workerId, 'fd') !== $row['fd']) { + continue; + } + + $this->timerTable->del($workerId); + + if ($this->server instanceof Server && ! $this->server->exists($row['fd'])) { + continue; + } - $this->extension->dispatchProcessSignal($row['worker_pid'], SIGKILL); + $this->extension->dispatchProcessSignal($row['worker_pid'], SIGKILL); - if ($this->server instanceof Server) { - $response = Response::create($this->server, $row['fd']); + if ($this->server instanceof Server) { + $response = Response::create($this->server, $row['fd']); - if ($response) { - $response->status(408); - $response->end(); - } + if ($response) { + $response->status(408); + $response->end(); } } } diff --git a/tests/EnsureRequestsDontExceedMaxExecutionTimeTest.php b/tests/EnsureRequestsDontExceedMaxExecutionTimeTest.php index a63ee51c7..2b525a4fb 100644 --- a/tests/EnsureRequestsDontExceedMaxExecutionTimeTest.php +++ b/tests/EnsureRequestsDontExceedMaxExecutionTimeTest.php @@ -17,6 +17,7 @@ public function test_process_is_killed_if_current_request_exceeds_max_execution_ $table['fake-worker-id'] = [ 'worker_pid' => 111, 'time' => time() - 60, + 'fd' => 1, ]; $action = new EnsureRequestsDontExceedMaxExecutionTime( @@ -39,4 +40,9 @@ public function del($workerId) { $this->deleted[] = $workerId; } + + public function get($workerId, $field = null) + { + return 1; + } }