Skip to content

fix(workflow): kill sandbox on cancel - #7

Open
woniu1288 wants to merge 5 commits into
UnicomAI:devfrom
woniu1288:fix/sandbox-cancel-kill-process-group
Open

woniu1288 wants to merge 5 commits into
UnicomAI:devfrom
woniu1288:fix/sandbox-cancel-kill-process-group

Conversation

@woniu1288

@woniu1288 woniu1288 commented Aug 12, 2026 •

Copy link
Copy Markdown
Contributor

Summary

修复工作流代码节点两类问题:

  1. 沙箱生命周期:用户取消后 Python/Deno 仍在跑,os.Pipe() 两端不关,抽视频等代码节点假死,最长拖到 CODE_RUNNER_TIMEOUT_SECONDS 默认 600 秒。
  2. 输出类型转换:代码节点声明为 Number/Integer 的字段,运行结果是数字却被置为 null,并报 field xxx is not number。

根因都在 wanwu-workflow 引擎(镜像 workflow-wanwu),不在万悟主仓 BFF/前端,也不在社创 run_by_wanwu 协议。

问题 1:取消杀不掉沙箱,pipe 泄漏

现象

  • POST /v1/workflow/run_by_wanwu 进入引擎后,代码节点走 sandbox/runner.go → sandbox.py → Deno/Pyodide。
  • 取消接口立刻成功,但 get_process 长时间仍是 Running。
  • 容器内 python sandbox.py / deno 残留,父进程挂着 4 个 pipe 端。
  • 社创远程执行协议没有选错;改 BFF 治不好。

根因

  • Run() 使用 exec.Command,不听 ctx。
  • 取消只写 Redis workflow:cancel:status:{exeID},事件循环 cancelFn() 后图的 ctx 被取消,但沙箱阻塞在 json.Decode,OnError 收不到 context.Canceled。
  • 父进程 Start 后不关多余 pipe;错误路径不 Kill/Wait/Close。
  • sandbox.py 用 subprocess.run,超时只杀 Deno 一层;请求读端不关。

修复

  • sandbox/runner.go:CommandContext + Setpgid;ctx.Done() 时 SIGKILL 整个进程组;Decode 放入 select;所有路径 Kill + Wait + Close。
  • sandbox.py:Popen + setsid + PR_SET_PDEATHSIG;超时/异常 os.killpg;finally 关闭 fd 3/4。
  • code/code.go:context.Canceled / DeadlineExceeded 不再包成执行失败,便于 get_process 记为 Cancel。

问题 2:Number/Integer 输出被置 null

现象

代码节点输出类型为 Number/Integer 时,代码已正确返回数字,节点输出仍为 null,并出现 field xxx is not number。

根因

运行结果经 UseNumber() 反序列化后是 json.Number(底层为 string)。convertToFloat64 / convertToInt64 / convertToString 的 type switch 未处理该类型,落入 default,生成 warning 并把字段置空。

修复

三个转换函数增加 json.Number 分支,分别调用 .Float64() / .Int64() / .String()。说明见 BUGFIX-json-number-convert.md。

变更文件

文件 说明
backend/infra/coderunner/impl/sandbox/runner.go 进程组取消、pipe 生命周期
backend/infra/coderunner/impl/script/sandbox.py Deno 进程组、fd 关闭
backend/domain/workflow/internal/nodes/code/code.go 取消错误透传
backend/domain/workflow/internal/nodes/convert.go json.Number 转换
BUGFIX-json-number-convert.md Number 问题说明

影响范围

  • 仅引擎代码节点 / 沙箱。
  • 主仓 wanwu 无需为漏 fd 改业务代码。
  • 部署需重新打 workflow-wanwu 镜像并重启该容器后生效。

Test plan

  • 跑流程代码节点:随机抽取等代码节点在数秒内结束,run成功。
  • 代码节点执行中调用 cancel:get_process 在数秒内变为 Cancel,而不是再挂满 10 分钟。
  • 取消后容器内无残留 python sandbox.py / deno;引擎进程 fd 中无多余 pipe。
  • 代码节点输出 Number/Integer 字段不再被置 null,不再报 field xxx is not number。
  • 正常成功路径结果与改前一致(非取消场景)。

Commits

  • 144dadda fix(workflow): code node Number/Integer output null due to json.Number not handled
  • a385e82e fix(workflow): kill sandbox process group on cancel and close leaked pipes

a1289626484 and others added 2 commits August 1, 2026 03:41
…r not handled

修复 convert.go 中 convertToFloat64/convertToInt64/convertToString
遗漏 json.Number 类型处理导致 Number/Integer 字段被置 null 的问题。

问题现象:
代码节点输出声明为 Number/Integer 类型的字段,在代码正确返回数字时,
节点输出被置为 null,并报错 "field xxx is not number"。

根因:
代码运行结果经 sonic UseNumber() 反序列化后产生 json.Number 类型
(type Number string),而 convertToFloat64/convertToInt64 的 type switch
只处理了 int64/float64/string,未处理 json.Number,导致走到 default 分支
生成 warning 并将字段置 null。

修复:
在 convertToFloat64/convertToInt64/convertToString 三个函数中新增
json.Number case,分别调用 .Float64()/.Int64()/.String() 转换。

详见 BUGFIX-json-number-convert.md
…pipes

Cancel only set a Redis flag while exec.Command ignored ctx, so Deno kept the ExtraFiles pipes open and the code node stayed hung.

Co-authored-by: Cursor <cursoragent@cursor.com>
@woniu1288 woniu1288 changed the title Fix/sandbox cancel kill procefix(workflow): 代码沙箱取消无法终止子进程及 json.Number 输出被置空ss group fix(workflow): kill sandbox on cancel Aug 13, 2026
a1289626484 and others added 3 commits August 13, 2026 08:09
…aths

Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
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.

1 participant