From 0e11b9f386d8bb22f63da5b466d6008dc78a0c56 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Mon, 29 Sep 2025 10:04:32 +0800 Subject: [PATCH 1/4] refactor(sentry): replace try-finally with defer pattern in CoroutineAspect Replace the try-finally block with defer functions for span finishing in the CoroutineAspect class. This improves code clarity and ensures proper cleanup of Sentry spans and transactions when coroutines are created. Changes: - Move span finishing logic to defer function at coroutine creation time - Remove try-finally block that was wrapping proceedingJoinPoint.process() - Ensure parent span and transaction are properly finished using defer - Maintain same cleanup behavior with cleaner code structure --- src/sentry/src/Tracing/Aspect/CoroutineAspect.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/sentry/src/Tracing/Aspect/CoroutineAspect.php b/src/sentry/src/Tracing/Aspect/CoroutineAspect.php index 8af31a9a5..a56fb5644 100644 --- a/src/sentry/src/Tracing/Aspect/CoroutineAspect.php +++ b/src/sentry/src/Tracing/Aspect/CoroutineAspect.php @@ -75,6 +75,13 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) ); SentrySdk::getCurrentHub()->setSpan($parent); + // Finish the span when the coroutine is created. + defer(function () use ($parent, $transaction) { + $parent->finish(); + SentrySdk::getCurrentHub()->setSpan($transaction); + $transaction->finish(); + }); + $cid = Co::id(); $keys = $this->keys; $callable = $proceedingJoinPoint->arguments['keys']['callable']; @@ -125,11 +132,6 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) } }; - try { - return $proceedingJoinPoint->process(); - } finally { - $parent->finish(); - SentrySdk::getCurrentHub()->setSpan($transaction); - } + return $proceedingJoinPoint->process(); } } From d4f57b047773cc6857abea5b3b7aa12e2000a95b Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Mon, 29 Sep 2025 11:50:07 +0800 Subject: [PATCH 2/4] refactor(sentry): simplify span finishing logic in CoroutineAspect --- src/sentry/src/Tracing/Aspect/CoroutineAspect.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/sentry/src/Tracing/Aspect/CoroutineAspect.php b/src/sentry/src/Tracing/Aspect/CoroutineAspect.php index a56fb5644..ff5564b6e 100644 --- a/src/sentry/src/Tracing/Aspect/CoroutineAspect.php +++ b/src/sentry/src/Tracing/Aspect/CoroutineAspect.php @@ -76,10 +76,8 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) SentrySdk::getCurrentHub()->setSpan($parent); // Finish the span when the coroutine is created. - defer(function () use ($parent, $transaction) { + defer(function () use ($parent) { $parent->finish(); - SentrySdk::getCurrentHub()->setSpan($transaction); - $transaction->finish(); }); $cid = Co::id(); From 3cebe133d6c743a35a6c77416c5a6fd037fff579 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Mon, 29 Sep 2025 11:50:49 +0800 Subject: [PATCH 3/4] refactor(sentry): increase sleep duration to reduce busy-waiting in worker watcher --- src/sentry/src/Transport/CoHttpTransport.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sentry/src/Transport/CoHttpTransport.php b/src/sentry/src/Transport/CoHttpTransport.php index c0134a423..0c26c4f0a 100644 --- a/src/sentry/src/Transport/CoHttpTransport.php +++ b/src/sentry/src/Transport/CoHttpTransport.php @@ -121,7 +121,7 @@ protected function loop(): void $this->workerWatcher ??= Coroutine::create(function () { if (CoordinatorManager::until(Constants::WORKER_EXIT)->yield()) { // sleep before setting workerExited to prevent busy-waiting - msleep(1); + msleep(100); $this->workerExited = true; From 76881d6589cfbfa48e0545cbfb592ea9749b8f1f Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Mon, 29 Sep 2025 11:52:51 +0800 Subject: [PATCH 4/4] refactor(sentry): move span finishing logic to finally block in CoroutineAspect --- src/sentry/src/Tracing/Aspect/CoroutineAspect.php | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/sentry/src/Tracing/Aspect/CoroutineAspect.php b/src/sentry/src/Tracing/Aspect/CoroutineAspect.php index ff5564b6e..0f296df9f 100644 --- a/src/sentry/src/Tracing/Aspect/CoroutineAspect.php +++ b/src/sentry/src/Tracing/Aspect/CoroutineAspect.php @@ -75,11 +75,6 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) ); SentrySdk::getCurrentHub()->setSpan($parent); - // Finish the span when the coroutine is created. - defer(function () use ($parent) { - $parent->finish(); - }); - $cid = Co::id(); $keys = $this->keys; $callable = $proceedingJoinPoint->arguments['keys']['callable']; @@ -130,6 +125,10 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) } }; - return $proceedingJoinPoint->process(); + try { + return $proceedingJoinPoint->process(); + } finally { + $parent->finish(); + } } }