Skip to content

OOM: customCommand called inside multiline function causes unbounded memory allocation #3

Description

@Haoxincode

Summary

当 customCommands 启用时,在多行函数定义的函数体内调用自定义命令(如 argv.py)会导致 MoonBit 引擎无限分配内存,最终 V8 进程 OOM 崩溃。

最小复现

import { Bash } from "./src/wrapper/index.ts";

const argvCommand = {
  name: "argv.py",
  run: async (args) => ({
    stdout: `[${args.map(a => `'${a}'`).join(", ")}]\n`,
    stderr: "",
    exitCode: 0,
  }),
};

const env = new Bash({ customCommands: [argvCommand] });

// OOM! 单个命令就会导致 V8 耗尽 4GB 堆后崩溃
await env.exec(`fun () {
  argv.py "$@" "$*"
}
fun "a b" "c d"`);

三个必要条件(缺一不可)

条件 去掉后
customCommands 启用 正常
多行函数定义(fun () {\n...\n}) 正常(单行 fun() { ...; } 不爆)
函数体内调用自定义命令 正常(函数体内调用内建命令如 echo 不爆)

对测试的影响

这是 spec bash 和 spec jq 测试套件 OOM 的根本原因。这些测试使用 customCommands(如 argv.py、printenv.py)且测试脚本包含多行函数定义。修复此 bug 后,spec 测试套件应该能在本地 4GB 堆内正常运行。

可能的根因方向

MoonBit 解释器在函数作用域内查找/执行自定义命令(custom command bridge)时,可能存在递归展开或循环引用,导致内存无限增长。建议从 interpreter 模块的函数调用 + customCommand dispatch 路径排查。

环境

  • MoonBash main 分支
  • Node.js v24, V8 heap 4096MB
  • 16GB RAM 机器上 100% 复现

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions