Skip to content

[Core] Add initial foundational configurations and flags for fork server - #67356

Merged
MengjinYan merged 1 commit into
ray-project:masterfrom
chiayi:fork-server
Nov 11, 2026
Merged

MengjinYan merged 1 commit into
ray-project:masterfrom
chiayi:fork-server

Conversation

@chiayi

@chiayi chiayi commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Description

Sets up configuration and fork-safety needed for the Python worker fork server.

No behavior change by default (RAY_enable_worker_fork_server=false).

Key Changes

  • Add preload_modules option to RuntimeEnv and fork_server_socket_path to RuntimeEnvContext
  • Add enable_worker_fork_server and worker_fork_server_timeout_ms config flags and forward fork server env vars to RuntimeEnvAgent
  • Extract initialize_default_worker() in default_worker.py so forked workers can initialize directly
  • Fix post-fork worker state (re-read internal config flags, reseed C++ PRNG on PID change, sync torch thread count, and only save worker PGID when pgid == pid)

Related issues

#67446 - Python worker fork server

Additional information

N/A

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@chiayi
chiayi requested a review from ryanaoleary October 23, 2026 22:54
@chiayi
chiayi force-pushed the fork-server branch 2 times, most recently from 213b493 to 9d6cc18 Compare October 23, 2026 22:56
@chiayi
chiayi marked this pull request as ready for review October 23, 2026 22:56
@chiayi
chiayi requested a review from a team as a code owner October 23, 2026 22:56
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

Comment thread src/ray/util/random.h Resolved
@ray-gardener ray-gardener Bot added core Issues that should be addressed in Ray Core community-contribution Contributed by the community labels Oct 24, 2026
@chiayi
chiayi force-pushed the fork-server branch 2 times, most recently from ff3dc87 to 0512a5e Compare October 26, 2026 19:26
Comment thread python/ray/_private/utils.py Outdated
os.environ["OMP_NUM_THREADS"] = str(omp_num_threads)
# If torch was pre-imported before fork(), its OpenMP thread pool is already
# initialized and ignores subsequent os.environ["OMP_NUM_THREADS"] updates.
if "torch" in sys.modules:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Prefer a constant str list (or just str if we can validate torch is the only sys module that needs this branching logic) here rather than hard-coding the "torch" module name

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added a THREAD_POOL_MODULES var to iterator over.

Comment thread python/ray/_raylet.pyx Resolved
Comment thread src/ray/util/random.h Resolved
Comment thread src/ray/util/random.h Resolved
Comment thread src/ray/common/ray_config.cc Resolved
@ryanaoleary ryanaoleary mentioned this pull request Oct 26, 2026
1 of 5 tasks
@ryanaoleary

Copy link
Copy Markdown
Contributor

created a Ray issue here: #67446 to link in the PRs so that they're easier to track

@chiayi
chiayi force-pushed the fork-server branch 2 times, most recently from 59b838c to 5bcd33a Compare October 26, 2026 23:56
@chiayi
chiayi requested a review from ryanaoleary October 26, 2026 23:59
Comment thread src/ray/raylet/worker_pool.cc Resolved

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

Comment thread python/ray/_raylet.pyx Resolved
@chiayi
chiayi force-pushed the fork-server branch 2 times, most recently from 6dfd31c to be33589 Compare October 27, 2026 22:26
@ryanaoleary

Copy link
Copy Markdown
Contributor

cc: @MengjinYan @edoakes for Ray core review, or if you know someone else able to approve that it'd be good to work with on this

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

There are 2 total unresolved issues (including 1 from previous review).

Fix All in Cursor

Reviewed by Cursor Bugbot for commit b97eb39. Configure here.

Comment thread src/ray/util/random.h Resolved
@Kunchd Kunchd added the go add ONLY when ready to merge, run all tests label Nov 3, 2026

@Kunchd Kunchd left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the PR! The first milestone looks good, and I look forward to the future milestones! I mainly left some questions to clarify my understanding of the change.

)
if stripped not in validated:
validated.append(stripped)
return validated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If the validated module doesn't actually exist, what's our plan for surfacing the import error to the user?

@chiayi chiayi Nov 4, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is actually being taken care of in the follow-up PR (#67514 but WIP), it will catch and log preload import errors in the fork server logs without failing server startup. If the task or actor imports that missing module at runtime, Python will raise ModuleNotFoundError to the user. Please let me know if you have thoughts on this!

num_threads = str(max(math.floor(num_assigned_cpus), 1))
for name in unset_env_vars:
os.environ[name] = num_threads
if "OMP_NUM_THREADS" in unset_env_vars:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

In the design doc, if I'm understanding the diagram correctly, the forkserver with the specified modules is started before the env vars is configured. If the user specifies OMP_NUM_THREADS to configure torch threads in the runtime env vars, would that configuration be ignored here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

No, it won't be ignored. This current change is only here to handle the case where the user doesn't set OMP_NUM_THREADS and Ray defaults it from num_cpus.

Followup changes will apply env_vars right after fork() and call torch.set_num_threads(...) when OMP_NUM_THREADS is set.

Comment on lines +892 to +894
RAY_LOG(WARNING) << "Worker " << pid << " pgid (" << pgid
<< ") != pid; skipping SetSavedProcessGroupId to avoid "
<< "signaling parent process group.";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Will this branch always trigger for the fork server? If so, this is intended behavior, should we be emitting at INFO level instead?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

So this branch won't trigger usually, this is just a safeguard in case setpgid fails unexpectedly. The plan is to place each forked child into its own process group via setpgid so forked workers will also have pgid == pid in normal operation.

@Kunchd Kunchd left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Might need to merge master again. But everything looks good!

@chiayi
chiayi force-pushed the fork-server branch 12 times, most recently from 46e4591 to 63d36fa Compare November 8, 2026 02:37
@chiayi
chiayi force-pushed the fork-server branch 7 times, most recently from 6efb430 to 1f098a5 Compare November 10, 2026 18:34
Signed-off-by: Aaron Liang <aaronliang@google.com>
@MengjinYan
MengjinYan merged commit 11a0a60 into ray-project:master Nov 11, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution Contributed by the community core Issues that should be addressed in Ray Core go add ONLY when ready to merge, run all tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants