Segfault when libuv symbols (e.g. from libjulia) are loaded before httpuv: bundled libuv calls get interposed

Open
#446 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
46/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
c, r
Domain
backend

Research direction

Start by reading src/Makevars and the libuv selection and linking logic described in the issue; compare a bundled libuv build with a system libuv build. Reproduce the reported segfault using the R reprex with Julia loaded first, then check symbol bindings with LD_DEBUG=bindings. Done means bundled libuv calls stay bound to httpuv when Julia is loaded first and the reprex survives; system libuv builds may still be affected.

Written by the indexing model from the issue text.

Description

On Linux, if a shared library that exports its own uv_* symbols is loaded into the R process globally before httpuv, httpuv's calls into its bundled libuv resolve to that other libuv. The struct layouts differ, so the server segfaults on the first request.

I hit this with Julia embedded in R (JuliaCall, or packages that load a juliac-compiled library). Julia's loader libjulia.so opens libjulia-internal.so with RTLD_GLOBAL, and that library exports Julia's forked libuv. Just dyn.load()ing libjulia.so is enough to trigger it.

Whether you're affected depends on how httpuv was linked. The crash happens with the Posit Package Manager binary (__linux__/noble). It doesn't happen when I build httpuv from source with Ubuntu's r-base and the bundled libuv (USE_BUNDLED_LIBUV=true), because that R's Makeconf links every package with -Wl,-Bsymbolic-functions (Ubuntu default).

Reprex

Needs julia and curl on the PATH.

libjulia <- system2(
  "julia",
  c("--startup-file=no", "-e", shQuote('print(joinpath(Sys.BINDIR, Base.LIBDIR, "libjulia.so"))')),
  stdout = TRUE
)
dyn.load(libjulia)

port <- httpuv::randomPort()
s <- httpuv::startServer(
  "127.0.0.1", port,
  list(call = function(req) list(status = 200L, body = "ok"))
)
system2("curl", c("-s", "-o", "/dev/null", paste0("http://127.0.0.1:", port)), wait = FALSE)
for (i in 1:20) httpuv::service(100)
cat("survived\n")

Output with the PPM binary:

 *** caught segfault ***
address 0x60, cause 'memory not mapped'

Traceback:
 1: execCallbacks(timeoutSecs, all, loop$id)
 2: run_now(timeoutMs/1000, all = FALSE)
 3: httpuv::service(100)
An irrecoverable exception occurred. R is aborting now ...

Without the dyn.load(), or with loadNamespace("httpuv") before it, the script prints survived.

Diagnosis

LD_DEBUG=bindings shows that when libjulia is loaded first, all 92 of httpuv.so's uv_* references bind to libjulia-internal.so.1.13. In the reverse order they bind to httpuv.so itself.

httpuv.so exports its bundled libuv's symbols (252 uv_*), and in the PPM binary its own calls to them go through the PLT, so they can be interposed. R loads package DLLs with RTLD_LOCAL, but that doesn't help here: the dynamic linker searches the global scope before the library's own symbols, so any libuv already loaded globally wins.

Possible fix

Keep httpuv's references to its bundled libuv internal to httpuv.so, independent of how R was configured. For example, add -Wl,-Bsymbolic-functions to PKG_LIBS in src/Makevars, or build libuv with -fvisibility=hidden.

I tested this by building 1.6.17 from source with USE_BUNDLED_LIBUV=true (bundled libuv, linked with -Bsymbolic-functions). The reprex then survives every time, and LD_DEBUG shows no uv_* references from httpuv.so resolving to libjulia.

(This only helps when httpuv uses its bundled libuv; a build against the system libuv.so.1, which configure picks when libuv1-dev is installed, crashes the same way and no link flag on httpuv's side can prevent that.)

Versions

httpuv 1.6.17 (PPM binary), R 4.6.1, Julia 1.13.2, Linux Mint 22.3 x86_64 (kernel 7.0.0-38-generic).

I ran into this myself; Claude Code (Opus 5.5) helped with debugging and the possible fix.

Dominant language
C
Stars
252
Forks
87
PR merge metrics
No merged PRs in 30d

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from rstudio/httpuv

All issues in rstudio/httpuv

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.