Skip to content

[17.0] fastapi: Oca port fastapi 18.0 to 17.0 c279bf#536

Merged
OCA-git-bot merged 10 commits into
OCA:17.0from
acsone:oca-port-fastapi-18.0-to-17.0-c279bf
Jun 4, 2025
Merged

[17.0] fastapi: Oca port fastapi 18.0 to 17.0 c279bf#536
OCA-git-bot merged 10 commits into
OCA:17.0from
acsone:oca-port-fastapi-18.0-to-17.0-c279bf

Conversation

@lmignon

@lmignon lmignon commented Jun 4, 2025

Copy link
Copy Markdown
Contributor

No description provided.

lmignon and others added 9 commits June 4, 2025 12:54
Each time a fastapi app is created, a new event loop thread is created by the ASGIMiddleware. Unfortunately, every time the cache is cleared, a new app is created with a new even loop thread. This leads to an increase in the number of threads created to manage the asyncio event loop, even though many of them are no longer in use. To avoid this problem, the thread in charge of the event loop is now created only once per thread / process and the result is stored in the thread's local storage. If a new instance of an app needs to be created following a cache reset, this ensures that the same event loop is reused.

refs OCA#484
This commit adds event loop lifecycle management to the FastAPI dispatcher.

Before this commit, an event loop and the thread to run it were created
each time a FastAPI app was created. The drawback of this approach is that
when the app was destroyed (for example, when the cache of app was cleared),
the event loop and the thread were not properly stopped, which could lead
to memory leaks and zombie threads. This commit fixes this issue by creating
a pool of event loops and threads that are shared among all FastAPI apps.
On each call to a FastAPI app, a event loop is requested from the pool and
is returned to the pool when the app is destroyed. At request time of
an event loop, the pool try to reuse an existing event loop and if no event
loop is available, a new event loop is created.

The cache of the FastAPI app is also refactored to use it's own mechanism.
It's now based on a dictionary of queues by root path by database,
where each queue is a pool of FastAPI app. This allows a better management
of the invalidation of the cache. It's now possible to invalidate
the cache of FastAPI app by root path without affecting the cache of others
root paths.
On server shutdown, ensure that created the event loops are closed properly.
defaultdict in python is not thread safe. Since this data structure
is used to store the cache of FastAPI apps, we must ensure that the
access to this cache is thread safe. This is done by using a lock
to protect the access to the cache.
This commit improves the lifecycle of the fastapi app cache.
It first ensures that the cache is effectively invalidated when changes
are made to the app configuration even if theses changes occur into an
other server instance.
It also remove the use of a locking mechanism put in place to ensure a thread
safe access to a value into the cache to avoid potential concurrency issue when
a default value is set to the cache at access time. This lock could lead
to unnecessary contention and reduce the performance benefits of queue.Queue's
fine-grained internal synchronization for a questionable gain. The only
expected gain was to avoid the useless creation of a queue.Queue instance
that would never be used since at the time of puting the value into the cache
we are sure that a value is already present into the dictionary.
…linting

[FIX] fastapi: Apply linting recommendations in 18

[FIX] fastapi: Apply feedback on PR
Before this change, an eventloop thread was created on each instanciation of the ASGIMiddleware class. We want to avoid this behavior to avoid the creation of zombie threads
@lmignon

lmignon commented Jun 4, 2025

Copy link
Copy Markdown
Contributor Author

/ocabot merge minor

@OCA-git-bot

Copy link
Copy Markdown
Contributor

This PR looks fantastic, let's merge it!
Prepared branch 17.0-ocabot-merge-pr-536-by-lmignon-bump-minor, awaiting test results.

@OCA-git-bot OCA-git-bot merged commit 65bc30e into OCA:17.0 Jun 4, 2025
7 checks passed
@OCA-git-bot

Copy link
Copy Markdown
Contributor

Congratulations, your PR was merged at 8526894. Thanks a lot for contributing to OCA. ❤️

@lmignon lmignon deleted the oca-port-fastapi-18.0-to-17.0-c279bf branch June 4, 2025 11:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants