Skip to content

🐛 fix deadlocks in depends by using a separate CapacityLimiter for teardown#15388

Open
olliemath wants to merge 1 commit into
fastapi:masterfrom
olliemath:teardown-limiter
Open

🐛 fix deadlocks in depends by using a separate CapacityLimiter for teardown#15388
olliemath wants to merge 1 commit into
fastapi:masterfrom
olliemath:teardown-limiter

Conversation

@olliemath
Copy link
Copy Markdown

@olliemath olliemath commented Apr 20, 2026

what

This is #12066 but with a single global "teardown" CapacityLimiter instead of unlimited one-off capacity limiters (and hence threads).

why

If there is a limited pool of resources, like database connections, we need separate pools of threads for acquiring and releasing those resources, otherwise there will always be deadlocks. However the PR doesn't seem to have moved for a while due to the use of one-off / unlimited CapacityLimiters.

anything else

We use a fixed, but arbitrary, limit of 5. It seems that a limit of 1 is too low as operations will serialise. Testing locally with a concurrency of 1000 I didn't notice any difference beyond a limit of ~3. Any number under 40 could be deemed "wrong" if the user has expensive teardown operations.

We can still trigger similar deadlocks by releasing resources in the "finally" section of a middleware. However this is not recommended in the documentation (although it's not explicitly prohibited), so I think it's OK.

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Apr 20, 2026

Merging this PR will not alter performance

✅ 20 untouched benchmarks


Comparing olliemath:teardown-limiter (3140dc8) with master (2fa00db)

Open in CodSpeed

…ardown

If there is a limited pool of resources, like database connections, we need separate pools of threads for acquiring and releasing those resources, otherwise there will always be deadlocks.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants