Fix async wrappers using @wraps incorrectly detected as sync#14445
Closed
aghabidareh wants to merge 2 commits into
Closed
Fix async wrappers using @wraps incorrectly detected as sync#14445aghabidareh wants to merge 2 commits into
aghabidareh wants to merge 2 commits into
Conversation
When a decorator uses `@wraps(func)` on an async wrapper around a sync function, FastAPI incorrectly treats the handler as sync because `is_coroutine_callable` uses `inspect.unwrap()` which follows `__wrapped__` back to the original sync function. This fix checks the actual callable first (not the unwrapped version) to correctly detect if the wrapper itself is async/generator, and only falls back to the unwrapped version for cases like `partial`. Fixes #14444
Author
|
Note: This PR needs the |
Member
|
Thanks! I'm doing it here: #14448 So I'll close this one. ☕ |
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
Fixes #14444
Since FastAPI 0.123.5 (PR #14434), using
functools.wrapson an async wrapper around a sync function causes FastAPI to incorrectly treat the handler as sync, resulting in:Root Cause
The
is_coroutine_callable,is_gen_callable, andis_async_gen_callableproperties inDependantuse_unwrapped_call(which usesinspect.unwrap()) to determine if a function is async/generator. When a decorator uses@wraps(func), it sets__wrapped__pointing to the original function, soinspect.unwrap()follows that chain back to the original sync function instead of checking the actual async wrapper.Fix
Check the actual callable first (not the unwrapped version) to correctly detect if the wrapper itself is async/generator, and only fall back to the unwrapped version for cases like
partial.Example that was broken
Test plan
tests/test_async_wrapper_sync_function.pythat reproduces the issuetests/test_dependency_wrapped.pytests still pass