Skip to content

Fix torch CUDA symbol errors from LD_LIBRARY_PATH conflicts - #4402

Closed
danielhanchen wants to merge 4 commits into
mainfrom
fix/studio-ld-library-path-cuda
Closed

danielhanchen wants to merge 4 commits into
mainfrom
fix/studio-ld-library-path-cuda

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Mar 18, 2026 •

Copy link
Copy Markdown
Member

Problem

Users who have LD_LIBRARY_PATH set with system CUDA lib paths (e.g. from conda, manual CUDA installs, or Docker base images) get torch symbol errors when launching Studio:

ImportError: /usr/local/cuda-13/lib64/libcudart.so.13: undefined symbol: ...

This happens because on Linux, the dynamic linker checks LD_LIBRARY_PATH before the RUNPATH embedded in torch's .so files. So system CUDA libs get loaded instead of the ones torch was built against (site-packages/nvidia/*/lib/).

The workaround today is for the user to manually unset LD_LIBRARY_PATH before launching Studio. Most users will not figure this out on their own.

Fix

Add _fix_torch_cuda_ld_path() and _maybe_reexec_for_cuda_ld_path() to run.py:

  1. Linux-only -- LD_LIBRARY_PATH is a Linux-specific linker mechanism
  2. Locate torch's bundled CUDA lib dirs (torch/lib/ and nvidia/*/lib/) using importlib.util.find_spec (does not import torch)
  3. Prepend them to LD_LIBRARY_PATH so they take priority over system CUDA
  4. Re-exec the process using sys.orig_argv (preserves python -m, -c, etc.) so the dynamic linker sees the updated path
  5. A sentinel env var (_UNSLOTH_STUDIO_LD_FIXED) prevents infinite re-exec

The re-exec is called from run_server() and the __main__ block -- not at module import time. Importing run.py (e.g. from the CLI or notebooks) is safe and will not replace the host process.

The user's original paths are preserved (just deprioritized), so non-torch tools that need system CUDA still work. If LD_LIBRARY_PATH is empty or torch is not installed, the fix is a no-op.

Test plan

  • Launch Studio with LD_LIBRARY_PATH=/usr/local/cuda-13/lib64 set -- should work without symbol errors
  • Launch Studio without LD_LIBRARY_PATH set -- should behave identically to before (no re-exec)
  • python -c "from studio.backend.run import run_server" with LD_LIBRARY_PATH set -- should NOT trigger re-exec (import safety)
  • unsloth studio -H 0.0.0.0 -p 8888 with LD_LIBRARY_PATH set -- should work (CLI path)
  • Launch on macOS/Windows -- should be a complete no-op
  • Run a training job after launch to confirm CUDA works end-to-end

When a user has LD_LIBRARY_PATH pointing at system CUDA libs (e.g.
/usr/local/cuda-13/lib64), the dynamic linker loads those instead of
the CUDA libs bundled with the torch wheel (in nvidia/*/lib/). This
causes symbol version mismatches and crashes on import.

Fix: before importing anything, detect torch's bundled CUDA lib paths
(without importing torch itself) and prepend them to LD_LIBRARY_PATH
so they take priority. Then re-exec so the dynamic linker picks up
the corrected path. A sentinel env var prevents infinite re-exec.

The user's original paths are preserved (just deprioritized), so
non-torch tools that need system CUDA still work.
@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!

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 58c640dfe5

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread studio/backend/run.py Outdated
Comment on lines +72 to +74
if _LD_FIXED_SENTINEL not in os.environ and _fix_torch_cuda_ld_path():
os.environ[_LD_FIXED_SENTINEL] = "1"
os.execv(sys.executable, [sys.executable] + sys.argv)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Avoid re-exec on module import

This os.execv(...) is executed at module import time, not only when run.py is launched as a script. studio/backend/colab.py imports run_server via from run import run_server, so in environments like Colab/Jupyter (where LD_LIBRARY_PATH is commonly set) importing run can replace the live kernel process and drop in-memory state or break kernel IPC. Restrict the re-exec path to explicit process entrypoints (e.g., under a __main__ guard or startup function) to avoid import-time process replacement.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good catch. That was true on the first commit where the execv ran at module scope. I moved it into _maybe_reexec_for_cuda_ld_path(), so importing run (e.g. from run import run_server in colab.py) no longer re-execs; only the main guard and run_server call it. The remaining run_server case I am handling separately.

Addresses all reviewer feedback:

1. Moved os.execv out of module-level code into a helper function
   _maybe_reexec_for_cuda_ld_path() that is called explicitly from
   run_server() and __main__. Importing run.py no longer replaces the
   host process -- safe for CLI, notebooks, tests, and embedders.

2. Changed platform guard from "not win32" to "linux only", since
   LD_LIBRARY_PATH is a Linux-specific linker mechanism.

3. Use sys.orig_argv (Python 3.10+) when available to preserve the
   original interpreter invocation (python -m, -c, -X flags, etc.).
   Falls back to [sys.executable] + sys.argv for older Python.

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e235df6d2a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread studio/backend/run.py Outdated
"""
global _server, _shutdown_event

_maybe_reexec_for_cuda_ld_path()

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Restrict re-exec to CLI entrypoints

Calling _maybe_reexec_for_cuda_ld_path() inside run_server() makes a library API perform os.execv, which replaces the current process whenever LD_LIBRARY_PATH is set and torch paths are prepended. This breaks embedding flows that call run_server directly (for example studio/backend/colab.py:start()), because launching Studio can restart the notebook/kernel process and drop in-memory state instead of just starting the server thread as documented. The re-exec should be limited to true process entrypoints (CLI/script startup), not the reusable server function.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Agreed. run_server is a library/embed entry (colab.start calls it directly), so the unconditional execv can restart the Colab kernel and drop in-memory state. I gated it behind an allow_reexec flag defaulting to False, so embedders no longer re-exec. The run.py main path already re-execs before run_server, and the unsloth_cli studio/ui entrypoints pass allow_reexec=True so the CLI keeps the CUDA LD fix.

@Datta0 Datta0 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.

I really am not sure if this is the best way to deal with this. Our install itself should respect the system's CUDA paths and all. This doesn't feel right

Comment thread studio/backend/run.py
if sys.platform != "linux":
return False
ld_path = os.environ.get("LD_LIBRARY_PATH", "")
if not ld_path:

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.

Wait do we skip setting if it is empty? I'm confused

…arted

run_server is a library/embed entry point (colab.start calls it directly), so the
unconditional os.execv for the torch CUDA LD_LIBRARY_PATH fix would replace the live
Colab/Jupyter kernel and drop in-memory state. Gate the re-exec behind a new
allow_reexec flag defaulting to False; the run.py __main__ path already re-execs
before calling run_server, and the unsloth_cli studio/ui entrypoints opt in with
allow_reexec=True so the CLI keeps the CUDA LD fix.
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Jul 6, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: 2773f98357

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen

Copy link
Copy Markdown
Member Author

Closing in favor of a fresh reimplementation against current main: #6905. The underlying issue is still real (a system CUDA on LD_LIBRARY_PATH shadows torch's bundled libs and causes undefined-symbol errors when the Studio backend imports torch, which main does not correct for the backend's own import), but this branch is about 212 commits behind and studio/backend/run.py was rewritten since, so it no longer applies. #6905 re-anchors the same Linux-only, sentinel-guarded re-exec into the current run.py main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants