fix: add recursion guard to StreamSlicerMeta.__instancecheck__#983
Draft
devin-ai-integration[bot] wants to merge 1 commit intomainfrom
Draft
fix: add recursion guard to StreamSlicerMeta.__instancecheck__#983devin-ai-integration[bot] wants to merge 1 commit intomainfrom
devin-ai-integration[bot] wants to merge 1 commit intomainfrom
Conversation
The metaclass __instancecheck__ method could recurse infinitely when isinstance() was called on objects with wrapped_slicer attributes that chain or cycle back. This adds a thread-safe recursion guard using a set of in-progress object IDs and uses object.__getattribute__ to bypass custom __getattr__ methods that could trigger further isinstance calls. Co-Authored-By: bot_apk <apk@cognition.ai>
Contributor
Author
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
👋 Greetings, Airbyte Team Member!Here are some helpful tips and reminders for your convenience. 💡 Show Tips and TricksTesting This CDK VersionYou can test this version of the CDK using the following: # Run the CLI from this branch:
uvx 'git+https://github.com/airbytehq/airbyte-python-cdk.git@devin/1775527410-fix-recursion-error-stream-slicer-meta#egg=airbyte-python-cdk[dev]' --help
# Update a connector to use the CDK from this branch ref:
cd airbyte-integrations/connectors/source-example
poe use-cdk-branch devin/1775527410-fix-recursion-error-stream-slicer-metaPR Slash CommandsAirbyte Maintainers can execute the following slash commands on your PR:
|
| _checking: threading.local = threading.local() | ||
|
|
||
| return super().__instancecheck__(instance) | ||
| def __instancecheck__(cls, instance: Any) -> bool: |
Contributor
Author
There was a problem hiding this comment.
This is a false positive. __instancecheck__ is a method on a metaclass (ABCMeta subclass), so its first parameter is the class being checked against, not an instance — cls is the correct name here. Using self would be misleading. The original code (introduced in #567) also used cls.
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
StreamSlicerMeta.__instancecheck__could cause aRecursionErrorwhenisinstance()was called on objects that have awrapped_slicerattribute combined with a custom__getattr__that delegates to the wrapped object. The original implementation usedhasattr(instance, "wrapped_slicer"), which invokes__getattr__—and if that__getattr__triggers furtherisinstance()checks (directly or indirectly), infinite recursion results.Two changes fix this:
id(instance)values. On re-entry for the same object, we fall back tosuper().__instancecheck__().object.__getattribute__instead ofhasattr: Bypasses any custom__getattr__on the instance, so only objects with a realwrapped_slicerattribute (not a delegated one) trigger the unwrap path.Resolves https://github.com/airbytehq/oncall/issues/11900:
Review & Testing Checklist for Human
object.__getattribute__semantics: The old code usedhasattr, which also matched attributes returned by__getattr__. The new code only matches attributes in the instance__dict__or class hierarchy. Confirm this is the correct behavior—StreamSlicerTestReadDecoratorstoreswrapped_sliceras a dataclass field (in__dict__), so it should still be found._checking:_checkingis a singlethreading.localinstance on the metaclass, shared by all classes usingStreamSlicerMeta. Verify that concurrentisinstance()calls from different threads cannot corrupt each other'sin_progresssets.isinstancebehavior across ConcurrentDeclarativeSource and other callers that rely on this metaclass.Notes
__getattr__withwrapped_slicer(primary bug reproduction), nested/double-wrapped decorators, and a self-referencing cycle (pathological guard test).Link to Devin session: https://app.devin.ai/sessions/36ab5b8f87bf4f0991dc9a05c10781fc