-
-
Notifications
You must be signed in to change notification settings - Fork 315
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[16.0][FIX] fastapi: Avoid zombie threads #486
Open
lmignon
wants to merge
5
commits into
OCA:16.0
Choose a base branch
from
acsone:16.0-fastapi-event-loop-lifecycle-lmi
base: 16.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+251
−21
Open
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
696a7c0
[FIX] fastapi: Avoid zombie threads
lmignon 8c090cd
[IMP] fastapi: add event loop lifecycle management
lmignon 2db95d5
[FIX] fastapi: Graceful shutdown of event loop
lmignon 1bf9751
[FIX] fastapi: Ensure thread safety of the FastAPI app cache
lmignon e657680
[IMP] fastapi: Improves app cache lifecycle
lmignon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Indeed, having one loop per thread is mandatory, missed this in #485 👍EDIT: changing my mind, not working as expected, see below ⬇️
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.
I still have doubt here: this will attach the loop to the current thread, which is OK in workers mode as the current thread is always the main thread of the worker, but in multi-threads, that would mean we will have one loop per spawned thread to handle the user's request, which is not the main thread. Am I wrong?
I try to test all of this locally.
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.
Oh, is Odoo spawning a new thread for each request in multithread mode?
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.
What I did to reproduce this issue:
Here is Odoo running in multi-threads (2 exactly, forced with

ODOO_MAX_HTTP_THREADS
+ cron threads disabled, and without queue_job neither), so we have the main thread+2 = 3:Request done on a FastAPI endpoint (e.g.

curl http://localhost:8069/{root_path}/sales
), one daemon thread spawned to handle the loop as expected:Beside in a Odoo shell, I reset the cache and relaunch a request on FastAPI endpoint:
curl http://localhost:8069/{root_path}/sales
Repeating step 3. several times: I get a new thread each time, so we are not re-using the loop on requests, while it was working as expected with [16.0][FIX] fastapi: prevent the creation of multiple event loops/threads #485 because having the loop created at the module level was done only once in the main thread, not for each subsequent thread created by Odoo to handle user's requests.
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.
@sebalix does that mean sharing the same event loop across threads? Is that safe?
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.
Yes: https://github.com/odoo/odoo/blob/18.0/odoo/service/server.py#L205-L217
I don't know... 🤷♂️ maybe not something supported by default (reading https://www.neilbotelho.com/blog/multithreaded-async.html ).
And what if we attach the event loop to the current thread? Or one not with daemon=True? So we get one event loop created for each user's request, does it imply a lot of overhead? (and we maybe lose the benefit of FastAPI by doing so)