FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix: ensure exception is available when BackgroundConsumer open stream fails by parthea · Pull Request #357 · googleapis/python-api-core · GitHub

This repository was archived by the owner on Feb 23, 2026. It is now read-only.
/ python-api-core Public archive

fix: ensure exception is available when BackgroundConsumer open stream fails - #357

Merged
parthea merged 12 commits into
mainfrom
fix-background-consumer-start-eats-exception
Oct 17, 2023
Merged

fix: ensure exception is available when BackgroundConsumer open stream fails#357
parthea merged 12 commits into
mainfrom
fix-background-consumer-start-eats-exception

Conversation

parthea commented Mar 14, 2022
edited
Loading

Copy link
Copy Markdown
Contributor

Fixes #268🦕

This PR updates the bidi.py code to reflect what is stated in the comments.

The comment for add_done_callback states This occurs when the RPC errors or is successfully terminated. however the done callback was not being called when an error occurs.

def add_done_callback(self, callback):
"""Adds a callback that will be called when the RPC terminates.
This occurs when the RPC errors or is successfully terminated.
Args:
callback (Callable[[grpc.Future], None]): The callback to execute.
It will be provided with the same gRPC future as the underlying
stream which will also be a :class:`grpc.Call`.
"""

def _on_call_done(self, future):
for callback in self._callbacks:
callback(future)

parthea requested review from a team March 14, 2022 20:37
parthea force-pushed the fix-background-consumer-start-eats-exception branch from 7f05e51 to 767b215 Compare March 14, 2022 20:38
parthea force-pushed the fix-background-consumer-start-eats-exception branch from dddb4d3 to 97c2363 Compare March 15, 2022 13:18
tswast previously requested changes Mar 15, 2022

tswast left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

This looks great! Let's block this until we figure out google-cloud-bigquery-storage though. googleapis/python-bigquery-storage#414

Comment thread google/api_core/bidi.py
Comment thread google/api_core/bidi.py Outdated
try:
call = self._start_rpc(iter(request_generator), metadata=self._rpc_metadata)
except exceptions.GoogleAPICallError as exc:
self._on_call_done(exc)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

on_call_done expects a future (based on the name of its argument). How does it work with an exception here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

The exception will be raised in the call back add_done_callback and must be handled by the user. This is based on the comment here:

Note that error handling *must* be done by using the provided
``bidi_rpc``'s ``add_done_callback``. This helper will automatically exit
whenever the RPC itself exits and will not provide any error details.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

add_done_callback specifies callback (Callable[[grpc.Future], None]), but it's not clear to me that GoogleAPICallError is a grpc.Future (see this)

parthea Oct 13, 2023
edited
Loading

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

The change that I proposed is exactly the same behaviour as the _on_call_done method of ResumableBidiRpc

# Unlike the base class, we only execute the callbacks on a terminal
# error, not for errors that we can recover from. Note that grpc's
# "future" here is also a grpc.RpcError.

From grpc/grpc#10885 (comment), grpc.RpcError is also grpc.Call.

I added this note in c7f63bd

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I reverted c7f63bd because GoogleAPICallError is not grpc.Call or grpc.RpcError. The original grpc.RpcError is available on the response property of GoogleAPICallError

def wrap_errors(callable_):
"""Wrap a gRPC callable and map :class:`grpc.RpcErrors` to friendly error
classes.
Errors raised by the gRPC callable are mapped to the appropriate
:class:`google.api_core.exceptions.GoogleAPICallError` subclasses.
The original `grpc.RpcError` (which is usually also a `grpc.Call`) is
available from the ``response`` property on the mapped exception. This
is useful for extracting metadata from the original error.

The behaviour proposed in this PR is the same as ResumableBidiRpc and we typically raise GoogleAPICallError instead of grpc.RpcError

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I changed the code in 753a591 to raise grpc.RpcError instead of GoogleAPICallError since grpc.RpcError is also grpc.Call and it is a better fit

product-auto-label Bot added the size: s Pull request size is small. label Jun 14, 2022
parthea requested review from atulep and tswast June 24, 2022 15:31
parthea dismissed tswast’s stale review June 30, 2022 02:22

Dismiss stale review

parthea requested a review from loferris June 30, 2022 02:22

parthea commented Jun 30, 2022

Copy link
Copy Markdown
Contributor Author

@loferris, please could you review/approve?

tswast requested a review from rosiezou January 24, 2023 22:02

tswast commented Jan 24, 2023

Copy link
Copy Markdown
Contributor

@rosiezou You might be interested in this PR. I forget the full context, but I know it's something I wanted for better debugging of the Python BQ Storage Write API.

parthea assigned vchudnov-g and unassigned steffnay and loferris Sep 1, 2023

Copy link
Copy Markdown

@parthea any idea when this will be merged?

parthea commented Sep 19, 2023

Copy link
Copy Markdown
Contributor Author

@vchudnov-g Please could you take a look?

Comment thread google/api_core/bidi.py
self._request_queue, initial_request=self._initial_request
)
call = self._start_rpc(iter(request_generator), metadata=self._rpc_metadata)
try:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

In examining this file, it seems to me that _RequestQueueGenerator._is_active (lines 91 et seq) should be defd as return self.call is not None and self.call.is_active(). The way it's currently written it will return True when self.call == None

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Fixed in e120a0c

Comment thread google/api_core/bidi.py Outdated
try:
call = self._start_rpc(iter(request_generator), metadata=self._rpc_metadata)
except exceptions.GoogleAPICallError as exc:
self._on_call_done(exc)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

add_done_callback specifies callback (Callable[[grpc.Future], None]), but it's not clear to me that GoogleAPICallError is a grpc.Future (see this)

parthea assigned parthea and unassigned vchudnov-g Sep 29, 2023
parthea assigned vchudnov-g and unassigned parthea Oct 13, 2023

parthea commented Oct 13, 2023

Copy link
Copy Markdown
Contributor Author

@vchudnov-g Please could you take another look?

parthea merged commit 405272c into main Oct 17, 2023
parthea deleted the fix-background-consumer-start-eats-exception branch October 17, 2023 19:54
parthea added a commit that referenced this pull request Nov 30, 2023
parthea added a commit that referenced this pull request Dec 1, 2023
…eam caught unexpected exception and will exit` (#562)

* chore: partial revert of PR #357

This reverts commit e120a0c.

* add comment
This was referenced May 30, 2025
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 subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

size: s Pull request size is small.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bidi.BackgroundConsumer.start() eats exception when _bidi_rpc.open() fails

8 participants


Back | FazBrowse Home | New Git URL