Skip to content
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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

sivanantha321
Copy link
Member

@sivanantha321 sivanantha321 commented Feb 3, 2024

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 support spawn or forkserver. If the end user already sets this start method we are getting RuntimeError: 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 using multiprocessing.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.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

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:

  1. Please confirm that if this PR changes any image versions, then that's the sole change this PR makes.

Checklist:

  • Have you added unit/e2e tests that prove your fix is effective or that this feature works?
  • Has code been commented, particularly in hard-to-understand areas?
  • Have you made corresponding changes to the documentation?

Release note:

Create a new multiprocessing context instead of using default context to avoid RuntimeError: context has already been set

Copy link
Member

@terrytangyuan terrytangyuan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm

if start_method is None:
logger.info("Setting 'fork' as multiprocessing start method.")
multiprocessing.set_start_method('fork')
elif start_method in ['spawn', 'forkserver']:
Copy link
Contributor

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

Copy link
Member

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

Copy link
Member Author

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 :)

Copy link

oss-prow-bot bot commented Feb 4, 2024

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
Copy link
Member Author

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.

Copy link

oss-prow-bot bot commented Feb 4, 2024

[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 /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@oss-prow-bot oss-prow-bot bot added the approved label Feb 4, 2024
@sivanantha321 sivanantha321 changed the title Only set multiprocessing start method if it is not already set Create a new multiprocessing context instead of using default context Feb 5, 2024
@sivanantha321 sivanantha321 changed the title Create a new multiprocessing context instead of using default context Create a new multiprocessing context instead of using default context to avoid RuntimeError: context has already been set Feb 5, 2024
@sivanantha321 sivanantha321 marked this pull request as draft February 7, 2024 06:27
Signed-off-by: Sivanantham Chinnaiyan <sivanantham.chinnaiyan@ideas2it.com>
Signed-off-by: Sivanantham Chinnaiyan <sivanantham.chinnaiyan@ideas2it.com>
# 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')
Copy link
Member

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?

@serdarildercaglar
Copy link

serdarildercaglar commented Mar 25, 2024

Note: Tried after merging sivanantha321:fix-multiprocess-context into the master on local

When I run the server with multiple workers, It starts properly.

image

But when I send the request to the server, the model gets the payload but is stuck in the prediction step.

image

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.

image

Then, When I want to run server again, I get error.

image

@sivanantha321

@kjoth
Copy link

kjoth commented Apr 3, 2024

Hope this is fixed and new version is released. Facing this issue when trying to set --workers in docker .

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.

Model server throws error when multiprocessing start method is already set
6 participants