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
Create a new multiprocessing context instead of using default context to avoid RuntimeError: context has already been set #3407
base: master
Are you sure you want to change the base?
Conversation
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.
/lgtm
python/kserve/kserve/model_server.py
Outdated
if start_method is None: | ||
logger.info("Setting 'fork' as multiprocessing start method.") | ||
multiprocessing.set_start_method('fork') | ||
elif start_method in ['spawn', 'forkserver']: |
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.
Can we add inline comment on why we only support 'fork' and not the other 2 ? Rest LGTM
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.
here the workers are only meant for uvicorn processes, if it is intended to launch a model with multi-processing then it would not work, for example pytorch does not support fork.
https://pytorch.org/docs/stable/notes/multiprocessing.html#cuda-in-multiprocessing
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.
Can we add inline comment on why we only support 'fork' and not the other 2 ? Rest LGTM
There is already a comment above the changed lines. Because of the way github shows changes, you might have missed it. But looking into this PR will give you more context though :)
8278bbb
to
4f336ea
Compare
New changes are detected. LGTM label has been removed. |
self._rest_server = UvicornServer(self.http_port, [serversocket], | ||
self.dataplane, self.model_repository_extension, | ||
self.enable_docs_url, log_config=self.log_config, | ||
access_log_format=self.access_log_format) | ||
# Since py38 MacOS/Windows defaults to use spawn for starting multiprocessing. | ||
# https://docs.python.org/3/library/multiprocessing.html#contexts-and-start-methods | ||
# Spawn does not work with FastAPI/uvicorn in multiprocessing mode, use fork for multiprocessing |
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.
Interestingly, uvicorn internally uses 'spawn' for creating new subprocesses.
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: rachitchauhan43, sivanantha321, terrytangyuan The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
4f336ea
to
6348f55
Compare
Signed-off-by: Sivanantham Chinnaiyan <sivanantham.chinnaiyan@ideas2it.com>
Signed-off-by: Sivanantham Chinnaiyan <sivanantham.chinnaiyan@ideas2it.com>
6348f55
to
70483d0
Compare
# https://docs.python.org/3/library/multiprocessing.html#contexts-and-start-methods | ||
# Spawn does not work with FastAPI/uvicorn in multiprocessing mode, use fork for multiprocessing | ||
# https://github.com/tiangolo/fastapi/issues/1586 | ||
context = multiprocessing.get_context('fork') |
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.
but this breaks the development on Mac?
Note: Tried after merging sivanantha321:fix-multiprocess-context into the master on local When I run the server with multiple workers, It starts properly. But when I send the request to the server, the model gets the payload but is stuck in the prediction step. But during the inference time, the server gets stuck and can't return a prediction. After I wait nearly 20 minutes, I try to stop the server combination of ctrl+c. Then, When I want to run server again, I get error. |
Hope this is fixed and new version is released. Facing this issue when trying to set --workers in docker . |
What this PR does / why we need it:
Currently, we are using default multiprocessing context to set the process start method to
fork
, since FastApi/Uvicorn server does not supportspawn
orforkserver
. If the end user already sets this start method we are gettingRuntimeError: context has already been set
error. This PR creates a new multiprocessing context and uses that to set the process start method. So, modifying the default context usingmultiprocessing.set_start_method
function no longer affects our model server.Which issue(s) this PR fixes (optional, in
fixes #<issue number>(, fixes #<issue_number>, ...)
format, will close the issue(s) when PR gets merged):Fixes #3406
Type of changes
Please delete options that are not relevant.
Feature/Issue validation/testing:
Please describe the tests that you ran to verify your changes and relevant result summary. Provide instructions so it can be reproduced.
Please also list any relevant details for your test configuration.
Test A
Test B
Logs
Special notes for your reviewer:
Checklist:
Release note: