fix(sentry): move cross-request static state into coroutine context - #1086
fix(sentry): move cross-request static state into coroutine context#1086huangdijia wants to merge 1 commit into
Conversation
|
Warning Review limit reached
Next review available in: 58 minutes Limit details: You’ve used all 2 included reviews currently available. You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. How can I continue?Wait for the limit to reset, then comment An organization admin can change what happens after included review limits in Billing. How do review limits work?CodeRabbit enforces per-developer PR review limits within each organization. For paid Pro and Pro+ reviews, CodeRabbit uses a developer's included PR review attempts over the past 7 days to set the current hourly allowance. At typical activity levels, the full plan allowance applies. Higher sustained activity can lower the allowance until earlier attempts leave the 7-day window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (5)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
问题
src/sentry中存在两处进程级静态可变状态,在 Swoole 协程并发下会互相覆盖并跨请求残留:Integration::$transaction(私有静态属性)——getTransaction()在setupOnce()注册的事件处理器中被读取,决定事件上的 transaction 名;协程 A 写入后协程 B 覆盖,导致事件携带错误的 transaction,且值在请求结束后仍驻留进程。Constants::$runningInCommand(公共静态属性)——被OnBeforeHandle写入、OnMetricFactoryReady读取,同样为进程级共享,协程/请求间相互污染。修改
将两处静态属性改为基于
Hyperf\Context\Context的协程级存储(随协程生命周期自动回收):Integration.php:删除private static ?string $transaction,新增CONTEXT_TRANSACTION常量;getTransaction()/setTransaction()方法签名与可见性不变,内部改为读写 Context。Constants.php:删除公共静态属性public static bool $runningInCommand,新增CTX_RUNNING_IN_COMMAND常量及静态方法runningInCommand(): bool、setRunningInCommand(bool $running = true): void。OnBeforeHandle.php:Constants::$runningInCommand = true→Constants::setRunningInCommand()。OnMetricFactoryReady.php:! SentryConstants::$runningInCommand→! SentryConstants::runningInCommand()。测试
新增
tests/Sentry/CoroutineScopedStateTest.php(PHPUnit 风格类,经FriendsOfHyperf\Tests\TestCase的RunTestsInCoroutinetrait 在Swoole\Coroutine::run()内执行),共 4 个用例:setTransaction('A')→getTransaction() === 'A',setTransaction(null)→null;runningInCommand()默认false,setRunningInCommand()后为true,可再次关闭;runningInCommand两个协程间隔离(协程 2 未设置时为false)。验证
vendor/bin/pest --group=sentry(sentry 组全部用例,含新增 4 例):通过(47 passed / 83 assertions);php-cs-fixer fix --dry-run --diff改动文件:0 处需修复;git diff --check:无空白错误;phpstan analyse src/sentry:No errors;setRunningInCommand()不存在 / 协程间互相读到对方值),可有效防止回归。