Skip to content

Commit 6da369a

Browse files
committed
feat: enhance coroutine context handling in CoroutineAspect
1 parent 0eda3c5 commit 6da369a

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/Aspect/CoroutineAspect.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ protected function handleCreate(ProceedingJoinPoint $proceedingJoinPoint): void
5353

5454
$proceedingJoinPoint->arguments['keys']['callable'] = function () use ($callable, $cid) {
5555
// Restore the Context in the new Coroutine.
56-
Context::copy($cid, self::CONTEXT_KEYS);
56+
foreach (self::CONTEXT_KEYS as $key) {
57+
Context::getOrSet($key, fn () => Context::get($key, coroutineId: $cid));
58+
}
5759

5860
// Defer the flushing of events until the coroutine completes.
5961
Co::defer(fn () => Integration::flushEvents());

src/Tracing/Aspect/CoroutineAspect.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,13 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint)
5353

5454
$callingOnFunction = CoroutineBacktraceHelper::foundCallingOnFunction();
5555

56+
if (! $callingOnFunction) {
57+
return $proceedingJoinPoint->process();
58+
}
59+
5660
return trace(
5761
function (Scope $scope) use ($proceedingJoinPoint, $callingOnFunction) {
58-
if ($callingOnFunction && $span = $scope->getSpan()) {
62+
if ($span = $scope->getSpan()) {
5963
$cid = Co::id();
6064
$callable = $proceedingJoinPoint->arguments['keys']['callable'];
6165

@@ -64,7 +68,9 @@ function (Scope $scope) use ($proceedingJoinPoint, $callingOnFunction) {
6468
SentrySdk::init(); // Ensure Sentry is initialized in the new coroutine.
6569

6670
// Restore the Context in the new Coroutine.
67-
Context::copy($cid, self::CONTEXT_KEYS);
71+
foreach (self::CONTEXT_KEYS as $key) {
72+
Context::getOrSet($key, fn () => Context::get($key, coroutineId: $cid));
73+
}
6874

6975
// Start a new transaction for the coroutine preparation phase.
7076
$transaction = startTransaction(

0 commit comments

Comments
 (0)