Skip to content

[core] Fix segfault on import ray when a RAY_* env var is malformed - #66364

Closed
LiRunGuo wants to merge 2 commits into
ray-project:masterfrom
LiRunGuo:fix-asio-chaos-static-init
Closed

LiRunGuo wants to merge 2 commits into
ray-project:masterfrom
LiRunGuo:fix-asio-chaos-static-init

Conversation

@LiRunGuo

Copy link
Copy Markdown

Description

import ray segfaults when any RAY_* config environment variable has a malformed value:

$ RAY_memory_monitor_refresh_ms='0# comment' python -c "import ray"
Segmentation fault (core dumped)

Root cause (gdb on the 2.55.1 wheel and on the master nightly 34562c2):

#0  ray::RayLog::RayLog(char const*, int, ray::RayLogLevel)
#1  ConvertValue<unsigned long>(...)
#2  RayConfig::ReadEnv<unsigned long>(...)
#3  RayConfig::RayConfig()
#4  RayConfig::instance()
#5  ray::asio::testing::(anonymous namespace)::DelayManager::Init()
#6  _GLOBAL__sub_I_asio_chaos.cc
#7  call_init (elf/dl-init.c:74)
  • src/ray/asio/asio_chaos.cc constructs its DelayManager in a static initializer (auto &delay_manager = []{...}();), and that constructor calls RayConfig::instance().
  • RayConfig's constructor parses every RAY_* env var. A malformed value trips the RAY_CHECK in ConvertValue, which builds a RayLog.
  • This runs while _raylet.so is still being dlopen'd, before RayLog's static members in logging.cc are initialized. The log call dereferences an uninitialized ostream (null vptr) and the process dies with SIGSEGV instead of printing the check failure.

As far as I can tell this is the only static initializer in src/ray that touches RayConfig.

Fix: make DelayManager a function-local static constructed on first use (GetDelayManager()), so RayConfig is first touched after static initialization is complete.

  • With RAY_testing_asio_delay_us set, behaviour is unchanged: the manager is still built from config on the first GetDelayUs() / Init() call.
  • The hot path when it is unset is unchanged: GetDelayUs() returns early before touching the manager.

With the fix, the same command fails with the intended message instead of crashing:

ray_config.h:33: ... Check failed: !value.empty() && stream.eof() Cannot parse "0# comment" to uint64_t

A subprocess regression test is added to python/ray/tests/test_ray_init.py. It asserts a non-zero exit that isn't SIGSEGV, and that the Cannot parse message is in stderr.

Related issues

Closes #49222

Not a duplicate: no open PR addresses this issue. The only PR referencing it, #49512 (closed unmerged), replaced the raw gettid syscall in util.h on the suspicion that GetTid was at fault. This PR fixes the static-initialization-order cause shown in the backtrace above.

Additional information

Tests run

Baseline (7204ade) and patched wheels were built in quay.io/pypa/manylinux2014_x86_64:2026.01.02-1 (the base of Ray's CI manylinux image, gcc 10 devtoolset) with identical flags. They were compared in fresh venvs on Linux x86_64, Python 3.11:

baseline wheel patched wheel
RAY_memory_monitor_refresh_ms='0# comment' python -c "import ray" exit 139 (SIGSEGV), empty stderr exit 1, Cannot parse "0# comment" to uint64_t
python -c "import ray" (control) ok ok
pytest python/ray/tests/test_ray_init.py -k malformed 1 failed (returncode=-11) 1 passed
pytest python/ray/tests/test_ray_init.py (whole file) – 22 passed
bazel test //src/ray/asio/tests:asio_defer_test – PASSED

Note: a native gcc 13 build of the same commit does not segfault. There the check fires and the log call happens to survive, but the static-init path is identical: the stack still goes through _GLOBAL__sub_I_asio_chaos.cc, and the patch removes it. That's why the fix was validated against the release (manylinux) toolchain. The regression test only catches the crash on toolchains where the bug shows up, but it asserts the readable error everywhere.

Lint: ruff, black, pydoclint, clang-format, the import-order check and the C++ inclusion check all pass on the changed files. cpplint isn't configured for src/ray/asio/.

AI assistance

Claude Code was used to investigate the root cause, write the patch and test, and draft this description. The change was reviewed by the submitter, and the tests above were run on a Linux x86_64 dev machine.

`asio_chaos.cc` constructed its `DelayManager` in a static initializer, and
that constructor reads `RayConfig::instance()`. When any `RAY_*` config env
var fails to parse, the `RAY_CHECK` in `ConvertValue` tries to log before the
`RayLog` statics are initialized, and the process segfaults while loading
`_raylet.so`, i.e. on `import ray`.

Construct the `DelayManager` lazily on first use instead, so `RayConfig` is
first touched after static initialization. A malformed value now fails with
the intended `Cannot parse "..." to <type>` check failure.

Add a subprocess regression test that imports ray with a malformed
`RAY_memory_monitor_refresh_ms` and asserts a clean failure instead of a
SIGSEGV.

Closes ray-project#49222

Signed-off-by: RunguoLi <[email protected]>
@LiRunGuo
LiRunGuo requested a review from a team as a code owner September 22, 2026 02:51

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request prevents a crash during import ray when a malformed RAY_* environment variable is present. The fix lazily constructs the DelayManager in src/ray/asio/asio_chaos.cc to avoid accessing RayConfig during static initialization. A regression test is also added to verify this behavior. The review feedback suggests making the test assertion more robust by checking for a broader error message prefix rather than asserting the exact type uint64_t.

Comment thread python/ray/tests/test_ray_init.py Outdated
# The import must fail, but with a readable error rather than a crash.
assert proc.returncode != -signal.SIGSEGV, proc.stderr
assert proc.returncode != 0
assert 'Cannot parse "0# comment" to uint64_t' in proc.stderr, proc.stderr

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.

medium

To make the test more robust against future changes to the type of RAY_memory_monitor_refresh_ms (or platform-specific type differences), we should avoid asserting the exact type uint64_t in the error message and instead check for the common prefix.

Suggested change
assert 'Cannot parse "0# comment" to uint64_t' in proc.stderr, proc.stderr
assert 'Cannot parse "0# comment"' in proc.stderr, proc.stderr

Assert on `Cannot parse "0# comment"` instead of the full message, so the
test does not depend on the declared type of `memory_monitor_refresh_ms`.

Signed-off-by: RunguoLi <[email protected]>
@ray-gardener ray-gardener Bot added core Issues that should be addressed in Ray Core community-contribution Contributed by the community labels Sep 22, 2026
@LiRunGuo

Copy link
Copy Markdown
Author

Hi @rueian @jjyao, could one of you take a look when you have a moment? This fixes #49222 (import ray segfaults when a RAY_* env var is malformed). The root cause is a static-initialization-order bug in src/ray/asio/asio_chaos.cc. The fix is a small lazy-init change (+8/−4) plus a regression test, and the description has the backtrace and A/B results on manylinux-built wheels. microcheck is green. If it looks good, could you add the go label to run the full CI? Thanks!

dstrodtman added a commit that referenced this pull request Sep 24, 2026
## Why are these changes needed?

Since about 2026-09-23 04:45 UTC, Read the Docs builds on `anyscale-ray`
have been intermittently dying with a bare segfault in `sphinx-build`:

```
[autosummary] generating autosummary for: 404.md, ...
make: *** [Makefile:100: html] Segmentation fault (core dumped)
```

What I found in the build history:

- None of the 132 finished builds from 2026-09-18 up to the onset
segfaulted. There have been 23 since, on both `master` and PR previews
across at least 10 PRs. In one 13-hour window, 10 of 32 finished builds
crashed.
- Every crash is at the same point, about 6 to 7 seconds into the `make
html` step, right after autosummary starts. This isn't the memory-limit
failure that the `SPHINX_JOBS=2` comment describes, which surfaces as an
`EOFError` from a worker much later in the build.
- It isn't deterministic per commit. `master` at 8a94f8b segfaulted in
builds 4473269 and 4474033, then passed in 4474041. A line-by-line diff
of the failing and passing logs shows the same commands, the same Python
(3.11.15), and the same installed package versions.
- No doc-build input changed in the onset window. The only commit that
landed on `master` in it was a core C++ change (#66383), which the docs
build doesn't compile.

That points to a change in the build environment rather than in this
repo, but the log gives nothing to hand to Read the Docs support or to
act on. This PR sets `PYTHONFAULTHANDLER=1` on the html build step so
the next crash dumps the Python stack of every thread to stderr. That
should name the module or C extension that crashed. The handler only
does anything when a fatal signal arrives, so passing builds are
unaffected.

Once the cause is found and fixed, this can stay as cheap insurance or
be reverted.

## Related issue number

None. I searched open and closed PRs for `faulthandler`, `readthedocs
segfault`, and `Segmentation fault`, and none of them cover the docs
build. #66364 fixes a segfault on `import ray` when a `RAY_*` env var is
malformed, which is a different path in core's static initialization.

## Testing

- Ran a deliberately segfaulting Python child through a `make` recipe,
the same way the build invokes `sphinx-build`. With
`PYTHONFAULTHANDLER=1`, make's output includes `Fatal Python error:
Segmentation fault` and the Python traceback. Without it, the output is
only make's `Segmentation fault` line.
- `python3 -c "import yaml; yaml.safe_load(open('.readthedocs.yaml'))"`
parses, and the html build command reads back as expected.
- `pre-commit run --files .readthedocs.yaml` passes.
- The Read the Docs preview for this PR runs the modified step, because
the preview guard builds whenever `.readthedocs.yaml` changes.

## AI assistance

I used Claude Code to survey the Read the Docs build history through the
RtD API, diff the build logs, and draft this change. I reviewed the diff
and the evidence above.

## Checks

- [x] I've signed off every commit (by using the -s flag, i.e., `git
commit -s`) in this PR.
- [x] I've run pre-commit jobs to lint the changes in this PR.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Signed-off-by: Douglas Strodtman <[email protected]>
Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
@karticam karticam self-assigned this Sep 29, 2026
@LiRunGuo

LiRunGuo commented Oct 5, 2026

Copy link
Copy Markdown
Author

Hi @karticam, thanks for picking this up! It's a small fix for the import ray segfault in #49222: lazy init in asio_chaos.cc (+8/−4) plus a regression test, and microcheck is green. Could you take a look when you have a chance, and add the go label if it looks good? Happy to address any feedback.

nadongjun pushed a commit to nadongjun/ray that referenced this pull request Oct 6, 2026
…ject#66452)

## Why are these changes needed?

Since about 2026-09-23 04:45 UTC, Read the Docs builds on `anyscale-ray`
have been intermittently dying with a bare segfault in `sphinx-build`:

```
[autosummary] generating autosummary for: 404.md, ...
make: *** [Makefile:100: html] Segmentation fault (core dumped)
```

What I found in the build history:

- None of the 132 finished builds from 2026-09-18 up to the onset
segfaulted. There have been 23 since, on both `master` and PR previews
across at least 10 PRs. In one 13-hour window, 10 of 32 finished builds
crashed.
- Every crash is at the same point, about 6 to 7 seconds into the `make
html` step, right after autosummary starts. This isn't the memory-limit
failure that the `SPHINX_JOBS=2` comment describes, which surfaces as an
`EOFError` from a worker much later in the build.
- It isn't deterministic per commit. `master` at 8a94f8b segfaulted in
builds 4473269 and 4474033, then passed in 4474041. A line-by-line diff
of the failing and passing logs shows the same commands, the same Python
(3.11.15), and the same installed package versions.
- No doc-build input changed in the onset window. The only commit that
landed on `master` in it was a core C++ change (ray-project#66383), which the docs
build doesn't compile.

That points to a change in the build environment rather than in this
repo, but the log gives nothing to hand to Read the Docs support or to
act on. This PR sets `PYTHONFAULTHANDLER=1` on the html build step so
the next crash dumps the Python stack of every thread to stderr. That
should name the module or C extension that crashed. The handler only
does anything when a fatal signal arrives, so passing builds are
unaffected.

Once the cause is found and fixed, this can stay as cheap insurance or
be reverted.

## Related issue number

None. I searched open and closed PRs for `faulthandler`, `readthedocs
segfault`, and `Segmentation fault`, and none of them cover the docs
build. ray-project#66364 fixes a segfault on `import ray` when a `RAY_*` env var is
malformed, which is a different path in core's static initialization.

## Testing

- Ran a deliberately segfaulting Python child through a `make` recipe,
the same way the build invokes `sphinx-build`. With
`PYTHONFAULTHANDLER=1`, make's output includes `Fatal Python error:
Segmentation fault` and the Python traceback. Without it, the output is
only make's `Segmentation fault` line.
- `python3 -c "import yaml; yaml.safe_load(open('.readthedocs.yaml'))"`
parses, and the html build command reads back as expected.
- `pre-commit run --files .readthedocs.yaml` passes.
- The Read the Docs preview for this PR runs the modified step, because
the preview guard builds whenever `.readthedocs.yaml` changes.

## AI assistance

I used Claude Code to survey the Read the Docs build history through the
RtD API, diff the build logs, and draft this change. I reviewed the diff
and the evidence above.

## Checks

- [x] I've signed off every commit (by using the -s flag, i.e., `git
commit -s`) in this PR.
- [x] I've run pre-commit jobs to lint the changes in this PR.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Signed-off-by: Douglas Strodtman <[email protected]>
Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
@github-actions

Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has not had
any activity for 14 days. It will be closed in another 14 days if no further activity occurs.
Thank you for your contributions.

You can always ask for help on our discussion forum or Ray's public slack channel.

If you'd like to keep this open, just leave any comment, and the stale label will be removed.

@github-actions github-actions Bot added the stale The issue is stale. It will be closed within 7 days unless there are further conversation label Oct 19, 2026
@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown

This pull request has been automatically closed because there has been no more activity in the 14 days
since being marked stale.

Please feel free to reopen or open a new pull request if you'd still like this to be addressed.

Again, you can always ask for help on our discussion forum or Ray's public slack channel.

Thanks again for your contribution!

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 stale The issue is stale. It will be closed within 7 days unless there are further conversation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Core Bad environment variable format cause segfault on import

2 participants