From 943669e13906738050548357928f82d1014adf2a Mon Sep 17 00:00:00 2001 From: oliver Date: Fri, 17 Apr 2026 00:13:34 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20=20use=20overflow=20thread?= =?UTF-8?q?pool=20rather=20than=20a=20standalone=20one=20for=20=5F=5Fexit?= =?UTF-8?q?=5F=5F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fastapi/concurrency.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/fastapi/concurrency.py b/fastapi/concurrency.py index c7811e0947..bfe454f8f2 100644 --- a/fastapi/concurrency.py +++ b/fastapi/concurrency.py @@ -94,20 +94,15 @@ async def contextmanager_in_threadpool( # can create race conditions/deadlocks if the context manager itself # has its 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: yield await run_in_threadpool(cm.__enter__) except Exception as e: ok = bool( - await anyio.to_thread.run_sync( - cm.__exit__, type(e), e, e.__traceback__, limiter=exit_limiter + await _run_in_threadpool_with_overflow( + cm.__exit__, type(e), e, e.__traceback__ ) ) if not ok: raise e else: - await anyio.to_thread.run_sync( - cm.__exit__, None, None, None, limiter=exit_limiter - ) + await _run_in_threadpool_with_overflow(cm.__exit__, None, None, None)