fix: Check span recording state before ending process span#1513
Open
juandelacruz-calvo wants to merge 5 commits intogoogleapis:mainfrom
Open
fix: Check span recording state before ending process span#1513juandelacruz-calvo wants to merge 5 commits intogoogleapis:mainfrom
juandelacruz-calvo wants to merge 5 commits intogoogleapis:mainfrom
Conversation
Adds a check to verify the subscribe span is still recording before attempting to end the process span in the __exit__ method. This prevents errors when the span has already been terminated or is no longer active. This fix ensures proper cleanup of OpenTelemetry spans during subscriber callback execution and prevents potential errors in the context manager exit flow.
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
abbrowne126
approved these changes
Oct 28, 2025
Collaborator
|
Thanks for putting this together! |
Author
Thanks for the review, I'll be away next week, but I'll try to fix the flaky test the week after that 👍 |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
This PR adds a check to verify that the subscribe span is still recording before attempting to end the process span in the
__exit__method of theSubscribeOpenTelemetrycontext manager.Problem
When the OpenTelemetry subscriber context manager exits, it may attempt to end the process span even when the parent subscribe span has already been terminated or is no longer in a recording state. This leads to log errors like:
/.venv/lib/python3.12/site-packages/opentelemetry/sdk/trace/init.py#L943
This happens because using
pubsubinstrumentation theend()method may be called twice:On
ackpython-pubsub/google/cloud/pubsub_v1/subscriber/message.py
Line 276 in e6294a1
Then on
__exit__python-pubsub/google/cloud/pubsub_v1/subscriber/_protocol/streaming_pull_manager.py
Lines 174 to 175 in e6294a1
Solution
Added a condition
self._subscribe_span.is_recording()to the existing check before callingself.end_process_span():Testing
Impact
This is a defensive code change that prevents edge case errors in span management. It should not affect normal operation but provides better handling when spans are terminated in non-standard flows.