FastGPT icon indicating copy to clipboard operation
FastGPT copied to clipboard

[code-sandbox] Linux ≥5.1 内核上 JS worker 返回较大结果时崩溃,报 Invalid worker response(seccomp 拦截了 libuv 的 io_uring_enter)

Open shikaiwei1 opened this issue 3 weeks ago • 1 comments

例行检查

  • [x] 我已确认目前没有类似 issue
  • [x] 我已完整查看过项目 README,以及项目文档
  • [x] 我使用了自己的 key,并确认我的 key 是可正常使用的
  • [x] 我理解并愿意跟进此 issue,协助测试和提供反馈
  • [x] 我理解并认可上述内容,并理解项目维护者精力有限,不遵循规则的 issue 可能会被无视或直接关闭

你的版本

  • [ ] 公有云版本
  • [x] 私有部署版本, 具体版本号: FastGPT v4.17.0,code-sandbox 镜像 v4.17.0(v4.16.2 起出现;对照源码,截至 v4.17.4 和 main 分支 2026-11-05 的代码仍未修复)

问题描述, 日志截图,配置文件等

在 Linux 内核 ≥ 5.1 且容器允许 io_uring 的环境中,JS 代码节点只要返回较大的结果(实测约 150KB 以上),就会间歇性甚至稳定地失败,报错如下:

Invalid worker response: SyntaxError: Unterminated string in JSON at position 292352 (line 1 column 292353)
| workerId: 11 | responseLength: 292352 | stderr: libuv: io_uring_enter(getevents): Operation not permitted

少数请求报的是:

Worker crashed during execution (exit code: null, signal: SIGSEGV)

主进程日志:

ERR sandbox:server JSProcessPool: failed to parse or handle worker response: {
  workerId: 11,
  responseLength: 292352,
  error: 'SyntaxError: Unterminated string in JSON at position 292352 (line 1 column 292353)
    at JSON.parse (<anonymous>)
    at Interface.u (file:///app/code-sandbox/index.js:4:1440)
    at Interface.emit (node:events:514:28)
    at Socket.onend (node:internal/readline/interface:276:12)
    ...
}

部署环境:阿里云函数计算 FC(1 vCPU / 4GB),SANDBOX_POOL_SIZE=20,SANDBOX_MAX_MEMORY_MB=128,未设置 SANDBOX_DISABLE_SECCOMP。内存占用一直正常,可以排除 OOM。

原因分析

  1. Node 启动时,libuv 在 uv__platform_loop_init 里调用 uv__iou_init(loop->backend_fd, &lfields->ctl, 256, 0),创建一个 io_uring 环,用于批量提交 epoll_ctl。这个环不带 SQPOLL,uv__use_io_uring() 直接返回 1,不受 UV_USE_IO_URING 环境变量控制。这一步发生在 worker 安装 seccomp 之前,所以能成功。
  2. worker 初始化时,native/js-sandbox/fastgpt_js_sandbox.c 的 install_seccomp() 安装白名单,默认动作为 SCMP_ACT_ERRNO(EPERM)。白名单里没有 io_uring_enter / io_uring_setup / io_uring_register。
  3. worker 用 process.stdout.write 把结果写回主进程。stdout 是 libuv 创建的 socketpair,处于非阻塞模式。结果超过 socket 缓冲区、主进程又没能及时读走时,libuv 需要给 stdout 注册 POLLOUT 监听,于是经 io_uring 环调用 io_uring_enter,被 seccomp 拒绝并返回 EPERM。
  4. libuv 在 uv__epoll_ctl_flush 里打印 libuv: io_uring_enter(getevents)(源码注释为 "Can't happen"),随后因 rc != n 调用 abort()。worker 在写到一半时退出。
  5. 主进程的 readline 在 stdout 关闭时(Socket.onend)把残留的半截行交给 onLine,JSON.parse 失败,于是报 Invalid worker response。如果进程退出事件先被处理,报的就是 Worker crashed。

关于第 5 步出现 SIGSEGV 而不是 SIGABRT:我们推测是 abort() 要用 tgkill 给自己发信号,而 tgkill 同样不在白名单里,glibc 只能退回到别的方式强行终止进程。这一点没有逐行核对 glibc 源码。

截断位置只有 146176、182720、292352 等几个固定值,与用户数据内容无关,符合"缓冲区写满后注册监听失败"的特征。

实测证据

两个 FC 实例运行相同的 sandbox 镜像,进程状态对比如下(通过 /proc/<pid>/status 的 Seccomp 字段和 /proc/<pid>/fd 中 anon_inode:[io_uring] 的数量统计):

实例 内核 进程中的 io_uring fd worker Seccomp 2MB 测试结果
A 4.19.91 0(内核不支持 io_uring) 2 20/20 成功
B 5.10.134 每个进程 3 个 2 10/10 失败
B,设置 SANDBOX_DISABLE_SECCOMP=true 5.10.134 每个进程 3 个 应用层已关闭 报错消失

复现步骤

  1. 在内核 ≥ 5.1、容器允许 io_uring 的 Linux 主机上部署 code-sandbox(v4.16.2 ~ v4.17.x),保持默认 seccomp 开启。可以先用下面的命令确认 worker 已持有 io_uring 环:
    for p in /proc/[0-9]*; do grep -q worker.js $p/cmdline 2>/dev/null && ls -l $p/fd | grep -c io_uring; done
    
  2. 新建一个工作流,只放一个 JS 代码节点:
    function main() {
      return { s: 'x'.repeat(2 * 1024 * 1024) }
    }
    
  3. 运行 10~20 次(可以用循环节点)。几乎每次都会报上面的错误。
  4. 设置 SANDBOX_DISABLE_SECCOMP=true 并重启后再测,报错消失。

预期结果

开启 seccomp 时,JS 代码节点返回较大的结果(不超过 SANDBOX_MAX_OUTPUT_MB)也能正常返回,不会因为 libuv 内部的 io_uring 调用被拒绝而崩溃。

相关截图

Image

修复建议(供参考,未在上游代码上验证)

  • 方案 1(倾向):在 worker 安装原生隔离之前,把 stdout 和 stderr 设为阻塞写,例如:
    (process.stdout as any)._handle?.setBlocking?.(true);
    (process.stderr as any)._handle?.setBlocking?.(true);
    
    阻塞写时,libuv 会循环写完而不注册 POLLOUT,不会走到 io_uring_enter,也不用放宽 seccomp。worker 本来就是一次性进程,阻塞写没有明显副作用。不过 stdin、定时器等其他监听在 seccomp 安装后是否还会经 io_uring 提交,需要维护者再确认。
  • 方案 2:在白名单中放行 io_uring_enter(继续禁止 io_uring_setup / io_uring_register)。这样能修好,但已有的环可以提交 openat、connect 等操作,而这些操作不经过 seccomp 过滤,会削弱隔离效果,不推荐。
  • 设置 UV_USE_IO_URING=0 无效:它只控制 SQPOLL 那个用于文件读写的环,管不到 epoll 批量提交的环。而且 base-process-pool.ts 启动 worker 时,env 只传了 PATH 和 CHECK_INTERNAL_IP,这个变量本来也传不进 worker。
  • 另外建议在 .env.template 和升级文档中说明:目前 SANDBOX_DISABLE_SECCOMP 的适用场景不只是"内核不支持 seccomp",在支持 io_uring 的内核上也会遇到本问题。

shikaiwei1 avatar Nov 06 '26 07:11 shikaiwei1

Thanks for the thorough analysis — this is a really solid root-cause. I read through native/js-sandbox/fastgpt_js_sandbox.c and src/pool/worker.ts to confirm, and your diagnosis lines up with the code:

  • install_seccomp()'s allow-list has epoll_* and eventfd2 but no io_uring_setup / io_uring_enter / io_uring_register, and the default action is SCMP_ACT_ERRNO(EPERM).
  • libuv on Linux ≥5.1 initializes its io_uring ring during loop setup (before the worker calls addon.init(...)), so the ring exists but every subsequent io_uring_enter is denied once seccomp is installed.
  • A large process.stdout.write on a non-blocking socketpair makes libuv register a POLLOUT watcher via that ring → io_uring_enter → EPERM → libuv's "Can't happen" abort(), which matches the truncated-JSON / Invalid worker response errors exactly.

A few notes that might help whoever picks this up:

  1. The setBlocking(true) fix (your option 1) targets the symptom's most common trigger but is not a complete fix. Making stdout/stderr blocking stops libuv from registering the POLLOUT watcher for those streams, but any other async watcher the worker still uses after addon.init() (stdin, timers, signals, the readline interface itself) can still route through the io_uring ring and hit the same EPERM. It's a good mitigation, not a guarantee.

  2. The robust fix is to reconcile the seccomp policy with libuv's io_uring usage. Either (a) add the three io_uring_* syscalls to the allow-list (matching the already-allowed epoll_*/eventfd2), or (b) force libuv off io_uring before addon.init() — note UV_USE_IO_URING=0 alone does not reliably disable it because uv__use_io_uring() short-circuits (as you found), so (a) is the more deterministic route. Given epoll_ctl is already whitelisted and the ring is created pre-seccomp, allowing io_uring_enter/io_uring_setup/io_uring_register is consistent with the existing policy and the lower-risk of the two.

  3. If the maintainers prefer to keep io_uring fully blocked for hardening, then the blocking-stdout change plus setting stdin to a fully-read/blocking mode before addon.init() is the fallback, but it needs verification on a ≥5.1 kernel that the remaining watchers don't still crash.

I can't reproduce the seccomp path on macOS to submit a tested PR myself, so I'm leaving this as a review note rather than a patch. If a maintainer /approves or wants a PR for the allow-list route, happy to put one together.

fxia0018 avatar Nov 07 '26 14:11 fxia0018