Skip to content

Guard the cudatoolkit lib_path() call during startup - #1478

Merged
dfalbel merged 2 commits into
mlverse:mainfrom
cornball-ai:fix/guard-cudatoolkit-lib-path
Oct 21, 2026
Merged

dfalbel merged 2 commits into
mlverse:mainfrom
cornball-ai:fix/guard-cudatoolkit-lib-path

Conversation

@TroyHernandez

Copy link
Copy Markdown
Contributor

load_cudatoolkit_libs() calls the cuda* package's lib_path() unguarded, so any error raised there propagates out of .onLoad and is reported as torch failing to start.

On a machine with a cuda* package installed:

$ R_DEFAULT_PACKAGES=NULL Rscript --vanilla -e 'loadNamespace("torch")'
ℹ torch failed to start, restart your R session to try again.
ℹ You might need to reinstall torch using `install_torch()`
✖ could not find function "packageName"

The reported fault is torch; the actual fault is in the toolkit package. That particular trigger is mlverse/cudatoolkit#5, and R CMD check clears R_DEFAULT_PACKAGES for its whole driver process, so it fires there too.

Not hypothetical: the same signature is in #1463 (comment, 2026-07-05), where it outlives the symlink workaround that thread is otherwise about. For a package that touches torch at load time the cost is not a NOTE, it is three R CMD check WARNINGs and .onLoad failed in loadNamespace().

The existing guard cannot fire

lib_path <- getExportedValue(pkg_name, "lib_path")()
if (!dir.exists(lib_path)) return(invisible(FALSE))

cuda*::lib_path() is system.file("lib", package = ..., mustWork = TRUE), which errors rather than returning "" when the directory is missing, so control never arrives at dir.exists() with a bad path.

Change

Catch the error, warn naming the toolkit package, and continue without preloading. This matches how the dyn.load() calls further down are already handled.

Verified against the real failing call:

# before
ERROR propagates: could not find function "packageName"

# after
Warning message:
ℹ Skipping CUDA library preload from cuda12.8.
✖ could not find function "packageName"
guard returned: NULL -> caller returns invisible(FALSE)

cli::cli_warn() was checked in the same R_DEFAULT_PACKAGES=NULL environment, so the handler does not reintroduce the failure it reports.

Startup then continues with whatever libraries remain available. Whether that lands on a working session depends on the build: a CPU build, or one with another usable CUDA source, starts; a CUDA build that needed the skipped libraries still fails, but names the toolkit package rather than torch.

One question

Possibly also upstream of #1463's original symptom. load_cudatoolkit_libs() exists to dyn.load() the toolkit's CUDA libraries before cpp_lantern_init(), so if it aborts or silently no-ops, liblantern.so has nothing to resolve libcudart.so.12 against. Not asserting it — the original trace there reaches cpp_lantern_init(), so the preload returned rather than errored in that invocation.

load_cudatoolkit_libs() called the cuda* package's lib_path() unguarded,
so any error raised there propagated out of .onLoad and was reported as
torch failing to start, naming torch rather than the toolkit package.

The dir.exists() check on the following line cannot catch the case it
names: cuda*::lib_path() is system.file(..., mustWork = TRUE), which
errors rather than returning "" when the directory is missing, so
control never reaches the guard with a bad path.

Catch it, warn naming the package at fault, and continue without
preloading those libraries.

Reported at mlverse#1463 (2026-07-05 comment), where the trigger
is mlverse/cudatoolkit#5 and R CMD check clears R_DEFAULT_PACKAGES for
its whole driver process. For a package that touches torch at load time
the cost there is three check WARNINGs and a namespace load failure.
@TroyHernandez

Copy link
Copy Markdown
Contributor Author

The 4 R CMD check failures here are the "less than" block in tests/testthat/test-distributions-constraints.R, which this PR does not touch. #1482 fixes both and its check jobs are green. I'll merge main into this branch once #1482 is in so the checks rerun.

@TroyHernandez

Copy link
Copy Markdown
Contributor Author

@sebffischer I merged main and resolved the NEWS.md conflict, and the CI run here needs a maintainer to approve it.

The root cause has a fix in mlverse/cudatoolkit#7, which adds the missing utils and stats imports. That should also fix #1463. This PR is the torch-side guard: if a cuda* package's lib_path() errors for any reason, torch warns and names that package instead of reporting that torch failed to start. Happy to close it if you'd rather rely on #7 alone.

@dfalbel
dfalbel merged commit f1395e9 into mlverse:main Oct 21, 2026
8 of 10 checks passed
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