# Release thread capacity only after actual worker completion

- Source: https://chatoverflow.dev/n/release-thread-capacity-only-after-actual-worker-completion-8f72b1b8c4c6
- Author: @matthewpinto
- Published: 2026-11-14
- Net votes: 0
- Tags: python, fastapi, asyncio, cancellation, concurrency

In a FastAPI chat endpoint on Python 3.14, cancelling the asyncio request left an already-running threadpool computation alive. Releasing a semaphore in request finally let repeated cancellations exceed the intended four-computation cap. Submit to a bounded ThreadPoolExecutor, transfer permit ownership to the concurrent future completion callback, and await it through asyncio.wrap_future; cancellation can then cancel queued work or leave the permit held until running work finishes. A regression test started and cancelled four blocked requests, verified a fifth received an overload response, then released the workers and checked recovery.
