Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork8.7k
Comments
🐛 Preserve traceback when an exception is raised in sync dependency withyield#5823
🐛 Preserve traceback when an exception is raised in sync dependency withyield#5823tiangolo merged 4 commits intofastapi:masterfrom
yield#5823Conversation
📝 Docs preview for commit202133b at:https://63af5d1c07bec913f8d350a9--fastapi.netlify.app |
MohammedAlhajji commentedDec 30, 2022
great workaround. thanks! |
📝 Docs preview for commit411ad02 at:https://63af6350238dde190a34aafc--fastapi.netlify.app |
MrSalman333 commentedDec 31, 2022
Nice work! We didn't upgrade to 3.11 because of this. |
iudeen left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Looks neat! And thanks for the tests as well!
sombek commentedJan 3, 2023
yezz123 commentedJan 3, 2023
📝 Docs preview for commitefd395c at:https://63bab4ceb11ed37e88ef871e--fastapi.netlify.app |
suzaku commentedJan 11, 2023
I patched your change with the following code and now the traceback's back. Thanks. fromcontextlibimportasynccontextmanagerasasynccontextmanagerfromtypingimportAsyncGenerator,ContextManager,TypeVarimportanyiofromanyioimportCapacityLimiterfromstarlette.concurrencyimportrun_in_threadpoolasrun_in_threadpool# noqa_T=TypeVar("_T")#@asynccontextmanagerasyncdefcontextmanager_in_threadpool(cm:ContextManager[_T],)->AsyncGenerator[_T,None]:# blocking __exit__ from running waiting on a free thread# can create race conditions/deadlocks if the context manager itself# has it's own internal pool (e.g. a database connection pool)# to avoid this we let __exit__ run without a capacity limit# since we're creating a new limiter for each call, any non-zero limit# works (1 is arbitrary)exit_limiter=CapacityLimiter(1)try:yieldawaitrun_in_threadpool(cm.__enter__)exceptExceptionase:traceback_=e.__traceback__ok=bool(awaitanyio.to_thread.run_sync(cm.__exit__,type(e),e,None,limiter=exit_limiter ) )ifnotok:ife.__traceback__isNone:e.__traceback__=traceback_raiseeelse:awaitanyio.to_thread.run_sync(cm.__exit__,None,None,None,limiter=exit_limiter )# Run this before starting anything FastAPI relatedfromfastapi.dependenciesimportutilsutils.contextmanager_in_threadpool=contextmanager_in_threadpool |
📝 Docs preview for commit45317a3 at:https://63ccf7d40fb8da632502fbec--fastapi.netlify.app |
sombek commentedFeb 2, 2023
@tiangolo Any idea when can we merge this? |
📝 Docs preview for commit11fd1ce at:https://63eb512c20977663c11a0846--fastapi.netlify.app |
etsvetanov commentedMar 22, 2023
Does this PR need additional work or help? |
- add async to get correct tracebacks till issue with fastapiand python 3.11 isfixedfastapi/fastapi#5823
Kludex commentedDec 2, 2024
Copied code from#8428 (comment), thanks@guillaumegenthial |
| from typing import AsyncGenerator, ContextManager, TypeVar | ||
| import anyio | ||
| import anyio.to_thread |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
nitpick.
contextmanager_in_threadpool| assert str(last_frame.path) == __file__ | ||
| assert last_frame.lineno == raise_value_error.__code__.co_firstlineno |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
If you revert the changes, you can verify that the dependency will not be the last frame (or in any frame, because it will not be present).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Awesome, great, I did this to verify. 🤓
tiangolo commentedDec 3, 2024
contextmanager_in_threadpoolyieldyieldyield… `yield` (fastapi#5823)Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
Uh oh!
There was an error while loading.Please reload this page.
There is a missing of traceback when an exception raised in contextmanager_in_threadpool
Happens on Python 3.11
I noticed the exception.traceback gets flushed after exiting the context manager.
There is an issue is opened#5740
Edit by@Kludex :