Skip to content

fix(api-core): preserve errors raised by async stream factories - #19323

Open
jaideeppyne wants to merge 1 commit into
googleapis:mainfrom
jaideeppyne:fix-async-stream-awaitable-errors
Open

jaideeppyne wants to merge 1 commit into
googleapis:mainfrom
jaideeppyne:fix-async-stream-awaitable-errors

Conversation

@jaideeppyne

Copy link
Copy Markdown

An async stream factory that raises TypeError while being awaited currently surfaces an unrelated AttributeError on __aiter__. The wrapper catches every TypeError from the await and assumes the output was not awaitable. As a result, the original failure is lost and a retry predicate for that error never receives it.

Use inspect.isawaitable to choose the unwrap path before awaiting the factory. Errors from that await then flow through the existing retry/error handling. Direct async iterables continue to use their iterator directly.

The four new regression cases cover a native coroutine and a custom awaitable, each with retryable and non-retryable setup errors. They verify the original exception identity, error callback, retry delay and successful values after retry. All four fail on the original code with AttributeError.

Validation on Python 3.13:

  • Sync and async streaming retry suites: 140 passed.
  • Full unit/asyncio suites with gRPC and async REST: 2,070 passed, 1 skipped.
  • No-gRPC configuration: 1,372 passed, 117 skipped.
  • gRPC with minimum supported google-auth==2.14.1: 1,932 passed, 93 skipped.
  • Combined package coverage: 100% statements and branches (5,505 statements, 1,536 branches). The dependency configurations exercise the optional transport/import and compatibility branches.
  • Ruff import and format checks, flake8 on both changed files, and git diff --check pass.

Prepared and tested with OpenAI Codex assistance.

Signed-off-by: jaideeppyne <jaideeppyne1997@gmail.com>
@jaideeppyne
jaideeppyne requested a review from a team as a code owner December 3, 2026 02:33

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request replaces a try-except block in retry_target_stream with an explicit check using inspect.isawaitable to determine if the target output is awaitable, and adds corresponding unit tests. The review feedback suggests optimizing startup performance by avoiding the import of the inspect module, recommending the use of collections.abc.Awaitable with isinstance instead.


import asyncio
import functools
import inspect

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.

medium

To optimize startup performance and avoid importing the heavy inspect module, we can import collections.abc instead and use isinstance(..., collections.abc.Awaitable).

Suggested change
import inspect
import collections.abc

*init_args, **init_kwargs
)
try:
if inspect.isawaitable(target_output):

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.

medium

Use isinstance(target_output, collections.abc.Awaitable) instead of inspect.isawaitable(target_output) to avoid the overhead of importing the inspect module.

Suggested change
if inspect.isawaitable(target_output):
if isinstance(target_output, collections.abc.Awaitable):

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant