Fix swallowed TypeError in async generator wrapper fallback#1659
Open
Parth-001-git wants to merge 1 commit into
Open
Fix swallowed TypeError in async generator wrapper fallback#1659Parth-001-git wants to merge 1 commit into
Parth-001-git wants to merge 1 commit into
Conversation
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
Replaces broad
except TypeErrorfallback logic with explicitsys.version_infochecks when handling async generator task creation.This prevents unrelated user
TypeErrorexceptions from being silently swallowed inside_ContextPreservedAsyncGeneratorWrapper.__anext__.Reproduction
The issue can be reproduced using the async generator example from #12520, where a user-side
TypeErrorwas previously swallowed and iteration completed silently.Fix
except TypeErrorfallback with explicit Python version branching.Greptile Summary
This PR fixes a long-standing bug where a broad
except TypeErrorcatch in_ContextPreservedAsyncGeneratorWrapper.__anext__andaclosesilently swallowed anyTypeErrorraised by the user's generator, replacing it with an explicitsys.version_info >= (3, 11)branch that reflects the true Python version boundary at whichasyncio.create_taskgained itscontextkeyword argument.TypeErrors inside async generators now propagate correctly instead of being silently caught and re-routed through the Python 3.10 fallback path.acloseand__anext__receive the same fix; thesysmodule is added at the top of the module (compliant with the repo import rule).import sysis inserted ahead ofimport asyncio, breaking the existing alphabetical import order.Confidence Score: 4/5
Safe to merge — the changed code correctly replaces runtime exception catching with an explicit Python version check, and behavior on both code paths is preserved.
The logic change is accurate: asyncio.create_task's context keyword was introduced in Python 3.11, so the version guard is the right boundary. Error propagation through the outer except block is unchanged. The only remaining notes are style-level: the duplicated version predicate and the out-of-order import sys line.
No files require special attention; both affected methods in langfuse/_client/observe.py received the same treatment consistently.
Flowchart
%%{init: {'theme': 'neutral'}}%% flowchart TD A["__anext__ called"] --> B{sys.version_info >= 3.11?} B -- Yes --> C["asyncio.create_task(generator.__anext__(), context=self.context)"] B -- No --> D["self.context.run(asyncio.create_task, generator.__anext__())"] C --> E{Exception?} D --> E E -- StopAsyncIteration --> F["_finalize() + re-raise"] E -- Exception / CancelledError --> G["_finalize_with_error(e) + re-raise"] E -- No exception --> H["Append item if capture_output"] H --> I["return item"] J["aclose called"] --> K{sys.version_info >= 3.11?} K -- Yes --> L["asyncio.create_task(generator.aclose(), context=self.context)"] K -- No --> M["self.context.run(asyncio.create_task, generator.aclose())"] L --> N{Exception?} M --> N N -- Yes --> O["_finalize_with_error(error) + re-raise"] N -- No --> P["_finalize()"]Prompt To Fix All With AI
Reviews (1): Last reviewed commit: "Fix swallowed TypeError in async generat..." | Re-trigger Greptile
Context used:
Learned From
langfuse/langfuse-python#1387