Repository navigation
Fix torch CUDA symbol errors from LD_LIBRARY_PATH conflicts - #4402
danielhanchen wants to merge 4 commits into
Conversation
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.
|
Warning You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again! |
There was a problem hiding this comment.
💡 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".
| 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) |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
💡 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".
| """ | ||
| global _server, _shutdown_event | ||
|
|
||
| _maybe_reexec_for_cuda_ld_path() |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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
| if sys.platform != "linux": | ||
| return False | ||
| ld_path = os.environ.get("LD_LIBRARY_PATH", "") | ||
| if not ld_path: |
There was a problem hiding this comment.
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.
|
@codex review |
|
Codex Review: Didn't find any major issues. Keep it up! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
|
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. |
Problem
Users who have
LD_LIBRARY_PATHset with system CUDA lib paths (e.g. from conda, manual CUDA installs, or Docker base images) get torch symbol errors when launching Studio:This happens because on Linux, the dynamic linker checks
LD_LIBRARY_PATHbefore theRUNPATHembedded in torch's.sofiles. 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_PATHbefore 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()torun.py:LD_LIBRARY_PATHis a Linux-specific linker mechanismtorch/lib/andnvidia/*/lib/) usingimportlib.util.find_spec(does not import torch)LD_LIBRARY_PATHso they take priority over system CUDAsys.orig_argv(preservespython -m,-c, etc.) so the dynamic linker sees the updated path_UNSLOTH_STUDIO_LD_FIXED) prevents infinite re-execThe re-exec is called from
run_server()and the__main__block -- not at module import time. Importingrun.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_PATHis empty or torch is not installed, the fix is a no-op.Test plan
LD_LIBRARY_PATH=/usr/local/cuda-13/lib64set -- should work without symbol errorsLD_LIBRARY_PATHset -- should behave identically to before (no re-exec)python -c "from studio.backend.run import run_server"withLD_LIBRARY_PATHset -- should NOT trigger re-exec (import safety)unsloth studio -H 0.0.0.0 -p 8888withLD_LIBRARY_PATHset -- should work (CLI path)