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

未关闭
#446 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
46/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
技术栈
c, r
领域
backend

调研方向

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.

由索引模型根据 Issue 内容生成。

描述

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.

主要语言
C
星标
252
派生
87
PR 合并指标
30 天内没有已合并 PR

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

rstudio/httpuv 的其他 Issue

查看 rstudio/httpuv 的全部 Issue

相似的 Issue

更多 C Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。