From 1e33c14bb418acadcfac8c603d6fd1daf2b0442f Mon Sep 17 00:00:00 2001 From: wikiboss Date: Sun, 1 Nov 2026 12:53:42 +0800 Subject: [PATCH] =?UTF-8?q?fix(queue):=20=E4=BF=AE=E5=A4=8D=E9=98=9F?= =?UTF-8?q?=E5=88=97=20expire=20=E9=85=8D=E7=BD=AE=E4=B8=BA=200=20?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=E4=BB=BB=E5=8A=A1=E6=89=A7=E8=A1=8C=E6=9C=9F?= =?UTF-8?q?=E9=97=B4=E8=A2=AB=E5=B9=BD=E7=81=B5=E9=87=8D=E5=A4=8D=E6=B6=88?= =?UTF-8?q?=E8=B4=B9=E7=9A=84=E4=B8=A5=E9=87=8D=E7=BC=BA=E9=99=B7=20(TDD)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/extra/queue.php | 2 +- tests/StabilityFixTest.php | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/application/extra/queue.php b/application/extra/queue.php index 1969703..4135cce 100644 --- a/application/extra/queue.php +++ b/application/extra/queue.php @@ -1,7 +1,7 @@ 'Redis', // Redis 驱动 - 'expire' => 0, // 任务的过期时间,默认为60秒; 若要禁用,则设置为 null + 'expire' => 180, // 任务的租期超时时间(秒),必须大于任务最大执行时间(如120s),防止执行中的任务被提前误判超时重投 // 任务的过期时间,默认为60秒; 若要禁用,则设置为 null 'default' => 'default', // 默认的队列名称 'host' => '127.0.0.1', // redis 主机ip 'port' => 6379, // redis 端口 diff --git a/tests/StabilityFixTest.php b/tests/StabilityFixTest.php index 69f0a96..c16469a 100644 --- a/tests/StabilityFixTest.php +++ b/tests/StabilityFixTest.php @@ -16,6 +16,7 @@ class StabilityFixTest self::testNotifyTimeParsing(); self::testAreaTreeAssembly(); self::testUserAddressDefenseLogic(); + self::testQueueConfigSafety(); echo "====================================================\n"; echo "【测试结论】所有稳定性测试均已通过!\n"; @@ -127,6 +128,26 @@ class StabilityFixTest echo "[PASS] 测试 3 通过: 用户收货地址防 IDOR 越权及空指针防御逻辑有效\n"; } + + /** + * 测试 4: 队列配置安全基线测试(杜绝 expire=0 导致的幽灵重复消费) + */ + public static function testQueueConfigSafety() + { + $queueConfigFile = dirname(__DIR__) . '/application/extra/queue.php'; + assert(file_exists($queueConfigFile), "queue.php 配置文件不存在"); + + $queueConfig = include $queueConfigFile; + assert(is_array($queueConfig), "queue.php 返回格式错误"); + + // 当 expire 为 0 时,think-queue 会在每个 tick 把正在执行的任务立即判定为已过期并重新投递,造成并发重复消费 + assert( + $queueConfig['expire'] === null || $queueConfig['expire'] >= 120, + "queue.php 的 expire 配置必须大于等于 120 秒或为 null,当前配置为 {$queueConfig['expire']},存在并发重复消费隐患" + ); + + echo "[PASS] 测试 4 通过: 队列超时与任务租期配置安全合规\n"; + } } if (php_sapi_name() === 'cli' && basename(__FILE__) === basename($_SERVER['SCRIPT_FILENAME'] ?? '')) {