This PR fixes a critical observability and memory management issue in the hyperf/tracer component when tracing coroutines (CoroutineAspect).
Motivation
Currently, if a coroutine executed via Co::create() throws an unhandled exception or is abruptly terminated, the tracer span is never finished ($child->finish()) and never flushed ($tracer->flush()).
This causes three main issues:
Memory Leaks: Unflushed spans remain in the worker's memory buffer.
Incomplete Traces: Orphaned spans that disappear from tracing UI (Jaeger/Zipkin).
Missing Error Context: Exceptions happening inside the coroutine are not tagged (error=true) in the span, making troubleshooting difficult.
Changes made
Wrapped the callable execution in a try/catch/finally block to capture exceptions, add the error tag, and log the exception details to the span.
Implemented Co::defer as a fallback to guarantee the span is closed even if the coroutine lifecycle is interrupted by flow-control functions like Co::exit().
Used a lightweight \stdClass object to keep track of the span's finished state. This prevents multiple executions of finish()/flush() and avoids using variable references (&$finished) inside closures, which is a known anti-pattern in Swoole and triggers static analysis warnings.
Type of change
Bug fix (non-breaking change which fixes an issue)
New feature (non-breaking change which adds functionality)
Breaking change (fix or feature that would cause existing functionality to not work as expected)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR fixes a critical observability and memory management issue in the hyperf/tracer component when tracing coroutines (CoroutineAspect).
Motivation
Currently, if a coroutine executed via Co::create() throws an unhandled exception or is abruptly terminated, the tracer span is never finished ($child->finish()) and never flushed ($tracer->flush()).
This causes three main issues:
Changes made
Type of change