Skip to content

Conversation

@juandelacruz-calvo
Copy link

@juandelacruz-calvo juandelacruz-calvo commented Oct 22, 2025

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 the SubscribeOpenTelemetry context 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

                logger.warning("Calling end() on an ended span.")

This happens because using pubsub instrumentation the end() method may be called twice:

Solution

Added a condition self._subscribe_span.is_recording() to the existing check before calling self.end_process_span():

if self._process_span and self._subscribe_span.is_recording():
    self.end_process_span()

Testing

  • Existing unit tests pass
  • Integration tests with OpenTelemetry context propagation pass

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.

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.
@juandelacruz-calvo juandelacruz-calvo requested review from a team as code owners October 22, 2025 15:18
@product-auto-label product-auto-label bot added the size: xs Pull request size is extra small. label Oct 22, 2025
@google-cla
Copy link

google-cla bot commented Oct 22, 2025

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.

@product-auto-label product-auto-label bot added the api: pubsub Issues related to the googleapis/python-pubsub API. label Oct 22, 2025
@juandelacruz-calvo juandelacruz-calvo marked this pull request as draft October 22, 2025 15:18
@juandelacruz-calvo juandelacruz-calvo marked this pull request as ready for review October 22, 2025 15:40
@juandelacruz-calvo juandelacruz-calvo marked this pull request as draft October 22, 2025 15:40
@product-auto-label product-auto-label bot added size: s Pull request size is small. and removed size: xs Pull request size is extra small. labels Oct 22, 2025
@juandelacruz-calvo juandelacruz-calvo marked this pull request as ready for review October 23, 2025 08:23
@abbrowne126
Copy link
Collaborator

Thanks for putting this together!

@abbrowne126 abbrowne126 enabled auto-merge (squash) October 28, 2025 20:59
@abbrowne126 abbrowne126 added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Oct 29, 2025
@yoshi-kokoro yoshi-kokoro removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Oct 29, 2025
@juandelacruz-calvo
Copy link
Author

Thanks for putting this together!

Thanks for the review, I'll be away next week, but I'll try to fix the flaky test the week after that 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: pubsub Issues related to the googleapis/python-pubsub API. size: s Pull request size is small.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants