Skip to content

Conversation

@huangdijia
Copy link
Contributor

@huangdijia huangdijia commented Dec 18, 2025

Summary

  • Replace manual timer cleanup with Timer's built-in closure parameter support
  • Removes need for separate coroutines monitoring WORKER_EXIT events
  • Simplifies timer handling logic across all metrics listeners
  • Improves resource management and reduces code complexity

Test plan

  • Verify metrics collection continues to work properly
  • Test graceful shutdown behavior
  • Ensure no memory leaks from timer cleanup
  • Run existing test suite for sentry component

Summary by CodeRabbit

  • 改进
    • 优化了指标采集的关闭与清理流程:采用信号驱动的定时器停止机制,实现更平滑的服务终止与更可靠的资源释放。
    • 精简了后台清理路径,减少显式外部协程等待/清理,降低潜在竞争与泄漏风险。
  • 新增
    • 在周期性上报中增加了定时器相关指标的统计,提升监控可观测性。

✏️ Tip: You can customize this high-level summary in your review settings.

Replace manual timer cleanup with Timer's built-in closure parameter support.
This removes the need for separate coroutines to monitor WORKER_EXIT events
and simplifies the timer handling logic across all metrics listeners.

Changes:
- Use Timer's closure parameter to handle cleanup gracefully
- Remove manual coroutine creation and CoordinatorManager usage
- Improve overall resource management and reduce complexity

Affected listeners:
- OnBeforeHandle
- OnCoroutineServerStart
- OnMetricFactoryReady
- OnWorkerStart
- PoolWatcher
- QueueWatcher
Copilot AI review requested due to automatic review settings December 18, 2025 03:11
@coderabbitai
Copy link

coderabbitai bot commented Dec 18, 2025

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.

演练

该 PR 在六个指标监听器中重构了定时器关闭逻辑:将显式的协程等待与清理(通过 CoordinatorManager / Coroutine 等)移除,改为在定时器回调中接受 $isClosing 参数,当为真时返回 Timer::STOP 以终止定时器,并删除相关未使用的协调器导入与外部清理协程。

变更

内聚体 / 文件 变更摘要
定时器回调重构
src/sentry/src/Metrics/Listener/OnBeforeHandle.php, src/sentry/src/Metrics/Listener/OnCoroutineServerStart.php, src/sentry/src/Metrics/Listener/OnMetricFactoryReady.php, src/sentry/src/Metrics/Listener/OnWorkerStart.php, src/sentry/src/Metrics/Listener/PoolWatcher.php, src/sentry/src/Metrics/Listener/QueueWatcher.php
将定时器回调签名改为接受 $isClosing = false;在关闭时返回 Timer::STOP 以停止定时器;在回调内短路关闭路径;移除外部 Coroutine::create 等等待 WORKER_EXIT 的清理协程;删除未使用的协调器/协程导入(如 Hyperf\Coordinator\ConstantsHyperf\Coordinator\CoordinatorManagerHyperf\Engine\Coroutine);个别文件中保留或新增了少量指标收集调用(如 Timer::stats()

评估代码审查工作量

🎯 3 (中等) | ⏱️ ~25 分钟

  • 关注点:
    • 验证六处监听器中 $isClosing 参数处理与返回 Timer::STOP 的一致性与正确性
    • 检查移除的外部清理协程是否在所有运行路径下不会导致定时器泄漏或竞态
    • 确认新增或保留的指标调用(例如 Timer::stats())与现有指标采集/上报逻辑兼容

可能相关的 PR

建议审查者

  • xuanyanwow
  • zds-s

诗歌

🐰✨ 定时器轻摇在田间,
回调悄悄听见告别言;
关停信号温柔挥手,
协程下岗花自眠,
指标安稳入梦眠 🥕

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: refactoring timer cleanup logic in metrics listeners by replacing manual cleanup with Timer's built-in closure support.
Docstring Coverage ✅ Passed Docstring coverage is 83.33% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch refactor/sentry-metrics-timer-cleanup

📜 Recent review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5528231 and 749c75d.

📒 Files selected for processing (1)
  • src/sentry/src/Metrics/Listener/OnBeforeHandle.php (2 hunks)
🧰 Additional context used
📓 Path-based instructions (4)
**/*.php

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

**/*.php: All PHP files must include declare(strict_types=1) at the top
Use PSR-12 coding standards for PHP code formatting
Use 4-space indentation with short array syntax in PHP code
Use .php-cs-fixer.php configuration for PHP code formatting

All PHP files must include a strict_types declaration at the top

**/*.php: Follow project PHP coding standard enforced by php-cs-fixer with PSR-12 style, 4-space indentation, and short array syntax
Maintain type coverage by updating or adding tests when public APIs change; ensure composer test:types stays green before pushing

Files:

  • src/sentry/src/Metrics/Listener/OnBeforeHandle.php
src/**/src/**/*.php

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

Use namespace pattern FriendsOfHyperf\{ComponentName} for all component classes

Files:

  • src/sentry/src/Metrics/Listener/OnBeforeHandle.php
src/*/src/**/*.php

📄 CodeRabbit inference engine (CLAUDE.md)

src/*/src/**/*.php: Namespace convention for components must follow FriendsOfHyperf{ComponentName}
All code must be coroutine-safe and avoid global state without proper context management, blocking I/O operations, and non-coroutine-safe third-party libraries without wrappers
Use Hyperf's Context API for request-scoped data instead of global state
Follows PSR-12 coding standards and use PHP-CS-Fixer for automatic formatting
Use PHPStan at maximum level for static analysis
Ensure component namespace doesn't conflict with existing Hyperf components or other packages in the ecosystem
Integrate deeply with Hyperf's Dependency Injection container for service registration
Leverage Hyperf's AOP (Aspect-Oriented Programming) for cross-cutting concerns via aspects defined in ConfigProvider
Use Hyperf's Event System to register listeners for framework events in components
All code must support coroutine-based concurrency using Swoole/Swow compatibility

Files:

  • src/sentry/src/Metrics/Listener/OnBeforeHandle.php
src/**/*.php

📄 CodeRabbit inference engine (AGENTS.md)

Each component lives in its own subdirectory with PSR-4 namespaces matching FriendsOfHyperf\*

Files:

  • src/sentry/src/Metrics/Listener/OnBeforeHandle.php
🧬 Code graph analysis (1)
src/sentry/src/Metrics/Listener/OnBeforeHandle.php (3)
src/sentry/src/Function.php (2)
  • feature (25-28)
  • metrics (30-33)
src/sentry/src/Feature.php (1)
  • getMetricsInterval (57-66)
src/sentry/src/Metrics/Timer.php (1)
  • Timer (18-62)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (12)
  • GitHub Check: Test on PHP 8.1 with Swoole 6.0.2
  • GitHub Check: Test on PHP 8.1 with Swoole 5.1.8
  • GitHub Check: Test on PHP 8.1 with Swoole 6.0.2
  • GitHub Check: Test on PHP 8.3 with Swoole 6.0.2
  • GitHub Check: Test on PHP 8.1 with Swoole 5.1.8
  • GitHub Check: Test on PHP 8.2 with Swoole 6.1.3
  • GitHub Check: Test on PHP 8.3 with Swoole 6.1.3
  • GitHub Check: Test on PHP 8.2 with Swoole 5.1.8
  • GitHub Check: Test on PHP 8.1 with Swoole 6.1.3
  • GitHub Check: Test on PHP 8.2 with Swoole 6.0.2
  • GitHub Check: Test on PHP 8.3 with Swoole 5.1.8
  • GitHub Check: Seer Code Review
🔇 Additional comments (3)
src/sentry/src/Metrics/Listener/OnBeforeHandle.php (3)

24-24: defer 导入已添加,解决了之前的问题。

此前的审查指出缺少 defer 函数的导入,现已正确添加。


96-113: 指标收集逻辑正确。

使用 defer 确保在设置所有指标后再刷新,gc、rusage 和内存指标的收集逻辑完整合理。


91-96: Hyperf Timer API 支持已确认

已验证 Hyperf\Coordinator\Timer 完全支持此用法模式:

  • Timer::STOP 常量已定义(值为 'stop'
  • tick() 方法确实向回调传递 $isClosing 参数
  • 返回 Timer::STOP 会正确停止定时器循环

代码在此处的实现是正确的,符合 Hyperf Timer 的设计意图。

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 PHPStan (2.1.33)

At least one path must be specified to analyse.


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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Comment on lines +90 to +93
$this->timer->tick($this->feature->getMetricsInterval(), function ($isClosing = false) use ($metrics, $event) {
if ($isClosing) {
return Timer::STOP;
}

This comment was marked as outdated.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
src/sentry/src/Metrics/Listener/OnCoroutineServerStart.php (1)

58-62: 可选观察:$this->running 标志永不重置。

$this->running 标志在第 62 行设置为 true,但在定时器停止后永远不会重置为 false。对于 MainCoroutineServerStart 事件来说,这可能是有意为之的,因为该事件在服务器生命周期中应该只触发一次。

但是,如果定时器因某种原因停止(通过返回 Timer::STOP),并且 process() 方法再次被调用,定时器将不会重新启动。如果这是预期行为,那么当前实现是正确的。

📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5f713a2 and 5528231.

📒 Files selected for processing (6)
  • src/sentry/src/Metrics/Listener/OnBeforeHandle.php (1 hunks)
  • src/sentry/src/Metrics/Listener/OnCoroutineServerStart.php (1 hunks)
  • src/sentry/src/Metrics/Listener/OnMetricFactoryReady.php (1 hunks)
  • src/sentry/src/Metrics/Listener/OnWorkerStart.php (1 hunks)
  • src/sentry/src/Metrics/Listener/PoolWatcher.php (1 hunks)
  • src/sentry/src/Metrics/Listener/QueueWatcher.php (1 hunks)
🧰 Additional context used
📓 Path-based instructions (4)
**/*.php

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

**/*.php: All PHP files must include declare(strict_types=1) at the top
Use PSR-12 coding standards for PHP code formatting
Use 4-space indentation with short array syntax in PHP code
Use .php-cs-fixer.php configuration for PHP code formatting

All PHP files must include a strict_types declaration at the top

**/*.php: Follow project PHP coding standard enforced by php-cs-fixer with PSR-12 style, 4-space indentation, and short array syntax
Maintain type coverage by updating or adding tests when public APIs change; ensure composer test:types stays green before pushing

Files:

  • src/sentry/src/Metrics/Listener/OnWorkerStart.php
  • src/sentry/src/Metrics/Listener/OnBeforeHandle.php
  • src/sentry/src/Metrics/Listener/QueueWatcher.php
  • src/sentry/src/Metrics/Listener/OnMetricFactoryReady.php
  • src/sentry/src/Metrics/Listener/OnCoroutineServerStart.php
  • src/sentry/src/Metrics/Listener/PoolWatcher.php
src/**/src/**/*.php

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

Use namespace pattern FriendsOfHyperf\{ComponentName} for all component classes

Files:

  • src/sentry/src/Metrics/Listener/OnWorkerStart.php
  • src/sentry/src/Metrics/Listener/OnBeforeHandle.php
  • src/sentry/src/Metrics/Listener/QueueWatcher.php
  • src/sentry/src/Metrics/Listener/OnMetricFactoryReady.php
  • src/sentry/src/Metrics/Listener/OnCoroutineServerStart.php
  • src/sentry/src/Metrics/Listener/PoolWatcher.php
src/*/src/**/*.php

📄 CodeRabbit inference engine (CLAUDE.md)

src/*/src/**/*.php: Namespace convention for components must follow FriendsOfHyperf{ComponentName}
All code must be coroutine-safe and avoid global state without proper context management, blocking I/O operations, and non-coroutine-safe third-party libraries without wrappers
Use Hyperf's Context API for request-scoped data instead of global state
Follows PSR-12 coding standards and use PHP-CS-Fixer for automatic formatting
Use PHPStan at maximum level for static analysis
Ensure component namespace doesn't conflict with existing Hyperf components or other packages in the ecosystem
Integrate deeply with Hyperf's Dependency Injection container for service registration
Leverage Hyperf's AOP (Aspect-Oriented Programming) for cross-cutting concerns via aspects defined in ConfigProvider
Use Hyperf's Event System to register listeners for framework events in components
All code must support coroutine-based concurrency using Swoole/Swow compatibility

Files:

  • src/sentry/src/Metrics/Listener/OnWorkerStart.php
  • src/sentry/src/Metrics/Listener/OnBeforeHandle.php
  • src/sentry/src/Metrics/Listener/QueueWatcher.php
  • src/sentry/src/Metrics/Listener/OnMetricFactoryReady.php
  • src/sentry/src/Metrics/Listener/OnCoroutineServerStart.php
  • src/sentry/src/Metrics/Listener/PoolWatcher.php
src/**/*.php

📄 CodeRabbit inference engine (AGENTS.md)

Each component lives in its own subdirectory with PSR-4 namespaces matching FriendsOfHyperf\*

Files:

  • src/sentry/src/Metrics/Listener/OnWorkerStart.php
  • src/sentry/src/Metrics/Listener/OnBeforeHandle.php
  • src/sentry/src/Metrics/Listener/QueueWatcher.php
  • src/sentry/src/Metrics/Listener/OnMetricFactoryReady.php
  • src/sentry/src/Metrics/Listener/OnCoroutineServerStart.php
  • src/sentry/src/Metrics/Listener/PoolWatcher.php
🧠 Learnings (1)
📚 Learning: 2025-12-12T05:33:43.947Z
Learnt from: CR
Repo: friendsofhyperf/components PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-12-12T05:33:43.947Z
Learning: Applies to src/*/src/**/*.php : Use Hyperf's Event System to register listeners for framework events in components

Applied to files:

  • src/sentry/src/Metrics/Listener/QueueWatcher.php
  • src/sentry/src/Metrics/Listener/OnCoroutineServerStart.php
🧬 Code graph analysis (5)
src/sentry/src/Metrics/Listener/OnBeforeHandle.php (3)
src/sentry/src/Function.php (2)
  • feature (25-28)
  • metrics (30-33)
src/sentry/src/Feature.php (1)
  • getMetricsInterval (57-66)
src/sentry/src/Metrics/Timer.php (1)
  • Timer (18-62)
src/sentry/src/Metrics/Listener/QueueWatcher.php (3)
src/sentry/src/Function.php (1)
  • feature (25-28)
src/sentry/src/Feature.php (1)
  • getMetricsInterval (57-66)
src/sentry/src/Metrics/Timer.php (1)
  • Timer (18-62)
src/sentry/src/Metrics/Listener/OnMetricFactoryReady.php (3)
src/sentry/src/Function.php (2)
  • feature (25-28)
  • metrics (30-33)
src/sentry/src/Feature.php (1)
  • getMetricsInterval (57-66)
src/sentry/src/Metrics/Timer.php (1)
  • Timer (18-62)
src/sentry/src/Metrics/Listener/OnCoroutineServerStart.php (3)
src/sentry/src/Function.php (2)
  • feature (25-28)
  • metrics (30-33)
src/sentry/src/Feature.php (1)
  • getMetricsInterval (57-66)
src/sentry/src/Metrics/Timer.php (1)
  • Timer (18-62)
src/sentry/src/Metrics/Listener/PoolWatcher.php (3)
src/sentry/src/Function.php (1)
  • feature (25-28)
src/sentry/src/Feature.php (1)
  • getMetricsInterval (57-66)
src/sentry/src/Metrics/Timer.php (1)
  • Timer (18-62)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (20)
  • GitHub Check: Test on PHP 8.1 with Swoole 6.1.3
  • GitHub Check: Test on PHP 8.2 with Swoole 6.1.3
  • GitHub Check: Test on PHP 8.1 with Swoole 5.1.8
  • GitHub Check: Test on PHP 8.2 with Swoole 5.1.8
  • GitHub Check: Test on PHP 8.2 with Swoole 6.0.2
  • GitHub Check: Test on PHP 8.1 with Swoole 6.0.2
  • GitHub Check: Test on PHP 8.3 with Swoole 6.1.3
  • GitHub Check: Test on PHP 8.3 with Swoole 5.1.8
  • GitHub Check: Test on PHP 8.3 with Swoole 6.0.2
  • GitHub Check: Test on PHP 8.1 with Swoole 5.1.8
  • GitHub Check: Test on PHP 8.3 with Swoole 6.0.2
  • GitHub Check: Test on PHP 8.1 with Swoole 6.0.2
  • GitHub Check: Test on PHP 8.1 with Swoole 6.1.3
  • GitHub Check: Test on PHP 8.2 with Swoole 6.0.2
  • GitHub Check: Test on PHP 8.3 with Swoole 5.1.8
  • GitHub Check: Test on PHP 8.3 with Swoole 6.1.3
  • GitHub Check: Test on PHP 8.2 with Swoole 6.1.3
  • GitHub Check: Test on PHP 8.2 with Swoole 5.1.8
  • GitHub Check: Agent
  • GitHub Check: Seer Code Review
🔇 Additional comments (5)
src/sentry/src/Metrics/Listener/QueueWatcher.php (1)

55-58: 定时器关闭模式实现正确。

与其他指标监听器保持一致的 $isClosing 参数处理模式,简化了资源清理逻辑。基于 learnings,该文件正确使用了 Hyperf 的事件系统来注册监听器。

src/sentry/src/Metrics/Listener/OnWorkerStart.php (1)

90-93: 实现正确,与整体重构保持一致。

定时器回调使用 $isClosing 参数进行优雅关闭,移除了之前基于协程的清理逻辑,代码更加简洁。

src/sentry/src/Metrics/Listener/PoolWatcher.php (1)

65-72: 抽象类中的定时器模式实现正确。

watch() 方法中的定时器回调正确使用了 $isClosing 参数,与其他监听器保持一致。这使得所有继承此抽象类的具体连接池监控器都能受益于简化的清理机制。

src/sentry/src/Metrics/Listener/OnMetricFactoryReady.php (1)

99-107: 重构方案清晰合理。

使用 $isClosing 参数和 Timer::STOP 返回值来处理定时器关闭是 Hyperf Coordinator Timer 的标准模式,相比之前基于协程监听 WORKER_EXIT 事件的方式更加简洁。Timer::STOP 常量在 Hyperf 的协调器组件中明确定义,已在多个监听器中验证使用。

代码充分利用 defer() 确保协程安全的资源清理,并通过 Hyperf 的 Context API 避免全局状态。第 107 行新增的 Timer::stats() 指标采集是一个有价值的补充,可以监控定时器的运行状态。

src/sentry/src/Metrics/Listener/OnCoroutineServerStart.php (1)

100-104: Timer 类的 $isClosing 参数模式已验证为正确支持。

Hyperf 的 Timer::tick() 方法完全支持将 $isClosing 参数传递给回调闭包。该实现在 Hyperf\Coordinator\Timer 类中明确定义(第 76 行),当 $isClosing 为 true 时,返回 Timer::STOP 会正确停止计时器。这个模式在整个项目中一致使用,包括 PoolWatcher.phpOnWorkerStart.phpOnMetricFactoryReady.php 等多个监听器类中。代码实现无误。

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR attempts to refactor timer cleanup logic across Sentry metrics listeners by replacing the manual cleanup pattern (using CoordinatorManager and explicit timer->clear() calls) with a presumed built-in closure parameter feature. However, this refactoring appears to be based on a misunderstanding of how Hyperf's Coordinator\Timer::tick() method works.

Key changes:

  • Adds $isClosing = false parameter to all timer tick callbacks
  • Removes CoordinatorManager::until(Constants::WORKER_EXIT) wait pattern
  • Removes explicit timer->clear($timerId) calls
  • Removes unused imports for Constants, CoordinatorManager, and Coroutine (except where Coroutine is still used for Coroutine::stats())

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
src/sentry/src/Metrics/Listener/QueueWatcher.php Refactored timer cleanup to use $isClosing parameter pattern, removed coordinator-based cleanup
src/sentry/src/Metrics/Listener/PoolWatcher.php Refactored timer cleanup to use $isClosing parameter pattern, removed coordinator-based cleanup
src/sentry/src/Metrics/Listener/OnWorkerStart.php Refactored timer cleanup to use $isClosing parameter pattern, removed coordinator-based cleanup
src/sentry/src/Metrics/Listener/OnMetricFactoryReady.php Refactored timer cleanup to use $isClosing parameter pattern, removed coordinator-based cleanup
src/sentry/src/Metrics/Listener/OnCoroutineServerStart.php Refactored timer cleanup to use $isClosing parameter pattern, removed coordinator-based cleanup
src/sentry/src/Metrics/Listener/OnBeforeHandle.php Refactored timer cleanup to use $isClosing parameter pattern, removed coordinator-based cleanup

Critical Issue Identified: The refactoring assumes that Hyperf\Coordinator\Timer::tick() automatically passes an $isClosing parameter to callbacks during shutdown. This doesn't appear to be a documented or implemented feature of Hyperf's Timer API. Other components in this repository (trigger, confd) use different patterns without this parameter. The original CoordinatorManager-based cleanup pattern is the established Hyperf approach for ensuring proper timer cleanup during worker shutdown.

Comment on lines +55 to +58
$this->timer->tick($this->feature->getMetricsInterval(), function ($isClosing = false) {
if ($isClosing) {
return Timer::STOP;
}
Copy link

Copilot AI Dec 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This refactoring assumes that Timer::tick() passes an $isClosing parameter to the callback during shutdown, but this doesn't appear to be how Hyperf's Coordinator\Timer works. The Timer::tick() method in Hyperf doesn't automatically invoke callbacks with a closing signal.

Looking at other usages in this codebase (trigger component's HealthMonitor, RedisServerMutex), they return Timer::STOP based on runtime conditions they check themselves, not on a parameter passed by the Timer.

The original pattern using CoordinatorManager::until(Constants::WORKER_EXIT) to wait for worker exit and then calling timer->clear() is the established Hyperf pattern for cleanup. Without this, the timers may not be properly cleaned up during shutdown, potentially causing memory leaks or preventing graceful shutdowns.

Copilot uses AI. Check for mistakes.
Comment on lines +65 to +72
$this->timer->tick($this->feature->getMetricsInterval(), function ($isClosing = false) use (
$pool,
$workerId,
$poolName
) {
if ($isClosing) {
return Timer::STOP;
}
Copy link

Copilot AI Dec 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This refactoring assumes that Timer::tick() passes an $isClosing parameter to the callback during shutdown, but this doesn't appear to be how Hyperf's Coordinator\Timer works. The Timer::tick() method in Hyperf doesn't automatically invoke callbacks with a closing signal.

The original pattern using CoordinatorManager::until(Constants::WORKER_EXIT) to wait for worker exit and then calling timer->clear() is the established Hyperf pattern for cleanup. Without this, the timers may not be properly cleaned up during shutdown, potentially causing memory leaks or preventing graceful shutdowns.

Copilot uses AI. Check for mistakes.
Comment on lines +90 to +93
$this->timer->tick($this->feature->getMetricsInterval(), function ($isClosing = false) use ($metrics, $event) {
if ($isClosing) {
return Timer::STOP;
}
Copy link

Copilot AI Dec 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This refactoring assumes that Timer::tick() passes an $isClosing parameter to the callback during shutdown, but this doesn't appear to be how Hyperf's Coordinator\Timer works. The Timer::tick() method in Hyperf doesn't automatically invoke callbacks with a closing signal.

The original pattern using CoordinatorManager::until(Constants::WORKER_EXIT) to wait for worker exit and then calling timer->clear() is the established Hyperf pattern for cleanup. Without this, the timers may not be properly cleaned up during shutdown, potentially causing memory leaks or preventing graceful shutdowns.

Copilot uses AI. Check for mistakes.
Comment on lines +99 to +102
$this->timer->tick($this->feature->getMetricsInterval(), function ($isClosing = false) use ($metrics, $serverStatsFactory, $workerId) {
if ($isClosing) {
return Timer::STOP;
}
Copy link

Copilot AI Dec 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This refactoring assumes that Timer::tick() passes an $isClosing parameter to the callback during shutdown, but this doesn't appear to be how Hyperf's Coordinator\Timer works. The Timer::tick() method in Hyperf doesn't automatically invoke callbacks with a closing signal.

The original pattern using CoordinatorManager::until(Constants::WORKER_EXIT) to wait for worker exit and then calling timer->clear() is the established Hyperf pattern for cleanup. Without this, the timers may not be properly cleaned up during shutdown, potentially causing memory leaks or preventing graceful shutdowns.

Copilot uses AI. Check for mistakes.
Comment on lines +100 to +103
$this->timer->tick($this->feature->getMetricsInterval(), function ($isClosing = false) use ($metrics) {
if ($isClosing) {
return Timer::STOP;
}
Copy link

Copilot AI Dec 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This refactoring assumes that Timer::tick() passes an $isClosing parameter to the callback during shutdown, but this doesn't appear to be how Hyperf's Coordinator\Timer works. The Timer::tick() method in Hyperf doesn't automatically invoke callbacks with a closing signal.

The original pattern using CoordinatorManager::until(Constants::WORKER_EXIT) to wait for worker exit and then calling timer->clear() is the established Hyperf pattern for cleanup. Without this, the timers may not be properly cleaned up during shutdown, potentially causing memory leaks or preventing graceful shutdowns.

Copilot uses AI. Check for mistakes.
@huangdijia huangdijia merged commit ec76f5b into main Dec 18, 2025
23 checks passed
@huangdijia huangdijia deleted the refactor/sentry-metrics-timer-cleanup branch December 18, 2025 05:15
huangdijia added a commit that referenced this pull request Dec 18, 2025
* refactor(sentry): improve timer cleanup in metrics listeners

Replace manual timer cleanup with Timer's built-in closure parameter support.
This removes the need for separate coroutines to monitor WORKER_EXIT events
and simplifies the timer handling logic across all metrics listeners.

Changes:
- Use Timer's closure parameter to handle cleanup gracefully
- Remove manual coroutine creation and CoordinatorManager usage
- Improve overall resource management and reduce complexity

Affected listeners:
- OnBeforeHandle
- OnCoroutineServerStart
- OnMetricFactoryReady
- OnWorkerStart
- PoolWatcher
- QueueWatcher

* refactor(metrics): add defer function import in OnBeforeHandle listener

---------

Co-authored-by: Deeka Wong <8337659+huangdijia@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants