fix rpcz root client span lifetime (#3420) - #3421
Conversation
|
There is a scenario like this:
Is that right? |
There was a problem hiding this comment.
Pull request overview
Fixes missing rpcz records for root client spans by ensuring the current span stays alive via Controller ownership until the RPC completes, SubmitSpan() runs, or the Controller is reset—preventing premature destruction when the caller-side temporary shared_ptr goes out of scope.
Changes:
- Keep the active span alive by changing
Controller::_spanfromstd::weak_ptr<Span>tostd::shared_ptr<Span>. - Update
Controllerspan-access sites to use the strong pointer and preserve submission/reset behavior. - Clarify span submission comments to reflect that server spans and root client spans (no local parent) are submitted independently.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/brpc/span.cpp | Updates comments to reflect root client span submission behavior (no local parent). |
| src/brpc/controller.h | Changes _span from weak_ptr to shared_ptr to extend span lifetime through RPC completion/reset. |
| src/brpc/controller.cpp | Updates span access patterns to use strong ownership and keep submission/reset flow consistent. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| // NOTE: align and group fields to make Controller as compact as possible. | ||
|
|
||
| std::weak_ptr<Span> _span; | ||
| std::shared_ptr<Span> _span; |
dfc0e1b to
d6aad1a
Compare
Thanks for pointing this out. For the exact case where the parent span is submitted while the async child span is still active, the child is not serialized under the parent, because The real duplicate risk is a nearby race: the child finishes first, the parent dump serializes it into I fixed this by making rpcz collection idempotent. Each
Performance impact should be negligible. The flag is |
Keep the current RPC span alive from Controller until the RPC finishes, SubmitSpan runs, or the Controller is reset. This lets root client spans without a local parent be submitted to rpcz instead of being destroyed after the caller-side temporary shared_ptr goes out of scope. Child client spans remain linked to their parent through weak local-parent references and parent-owned client lists, so they are still serialized under their parent without introducing shared_ptr cycles.
d6aad1a to
84008ca
Compare
Fixes #3420
Summary
This change keeps the current RPC span alive from Controller until the RPC finishes, SubmitSpan runs, or the Controller is reset. This lets root client spans without a local parent be submitted to rpcz instead of being destroyed after the caller-side temporary shared_ptr goes out of scope.
Child client spans remain linked to their parent through weak local-parent references and parent-owned client lists, so they are still serialized under their parent without introducing shared_ptr cycles.
Testing
Note: the local CMake build used a temporary include-only workaround for an unrelated UBShm timer_mgr.cpp atomic_fetch_add/sub compile issue on GCC 8; no UBShm source change is included in this PR.