Skip to content

fix: Tokio worker 栈 2 MiB → 16 MiB,修复长上下文大流式请求下的栈溢出崩溃 - #583

Open
NaIWaniubi wants to merge 1 commit into
aimod-cc:mainfrom
NaIWaniubi:fix/tokio-worker-stack-size
Open

NaIWaniubi wants to merge 1 commit into
aimod-cc:mainfrom
NaIWaniubi:fix/tokio-worker-stack-size

Conversation

@NaIWaniubi

Copy link
Copy Markdown

问题现象(生产观测)

生产观测(Windows 10 x64,v3.4.2,2026-11-12 ~ 11-14):
- Windows Event ID 1000 三条:0xc00000fd(栈溢出)一次、0xc0000409(fail-fast)
  两次且偏移相同(同一代码路径);另有多达 10+ 次/天的进程消失。
- 崩溃 RVA 经 .pdata 解析落在 8337B 的 poll 函数,其引用 ≥15 KiB 的 async
  Future 状态体 —— 长上下文请求(观测签名:POST /v1/chat/completions
  stream=true msgs=366 bytes=2.68 MB)的处理链需要比 2 MiB 默认栈更多的
  空间。
- 所有请求 handler 经 spawn_task 承载在 Tokio worker(默认 2 MiB 栈)上。

修复:为三处 new_multi_thread() 统一加 thread_stack_size(16 MiB):
- server/src/lib.rs global_runtime()(spawn_task 回退运行时)
- bin/agent2api-server.rs headless 主运行时
- bin/agent2api-server.rs Windows 服务形态运行时

影响:仅增加线程栈虚拟地址预留(16 MiB/线程,物理页按需提交);
worker 数仍为 CPU 核数默认。
@coderabbitai

coderabbitai Bot commented Nov 14, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Summary

📌 通俗总结(这个 PR 做了什么?)

核心目的: 修复 Windows 上长上下文大流式请求导致的进程崩溃。

问题原因: Tokio 工作线程默认栈只有 2 MiB。网关处理一次请求时,会生成很大的 async Future 状态机。长上下文请求下,栈空间不够,进程会发生栈溢出,异常码为 0xc00000fd。

改了什么:

  • desktop-tauri/src-tauri/server/src/lib.rs:global_runtime() 创建的多线程运行时,工作线程栈从默认 2 MiB 改为 16 MiB。新增注释说明原因。
  • desktop-tauri/src-tauri/server/src/bin/agent2api-server.rs:
    • 前台运行形态(headless)的多线程运行时,栈设为 16 MiB。
    • Windows 服务形态的多线程运行时,栈设为 16 MiB。

实际效果: 每个工作线程的虚拟地址空间预留变大。物理内存按实际使用提交,不会一次性占用 16 MiB。工作线程数量不变。

变更规模: 2 个文件,新增 17 行,无删除。

💡 产品与技术方案评估

Bug 修复评估:

  • 是否从根源修复? 基本是。问题根源是默认栈太小,而请求处理链产生的 Future 状态机又很大。提高栈上限直接对应根因。
  • 是否只是“哪里报错堵哪里”? 不是。三处运行时配置都已统一修改,覆盖前台、服务和全局运行时三条路径。
  • 仍存在的风险:
    • 16 MiB 是经验值。PR 中没有给出压测数据,无法确认 16 MiB 是否足够。
    • 栈空间只是缓解手段。更根本的做法是减小 async Future 的状态体,例如把大对象装箱(Box::pin)或拆分大的 async 函数。这样可以降低每个请求的栈占用,而不是依赖更大的栈。
    • 三处配置重复写相同的值。建议把栈大小抽成一个共享常量,避免后续只改其中一处。
  • 验证缺口: 当前输出未包含 CI、构建或测试结果。无法确认改动是否已通过编译或长上下文压测。建议补充长上下文大流式请求的压力测试,并在 Windows 10 x64 上验证崩溃是否消失。

前台运行、Windows 服务和 global_runtime 创建的 Tokio 多线程运行时,现在都将工作线程栈大小设为 16 MiB。global_runtime 新增了相关注释。

Changes: Tokio 运行时栈配置

层 / 文件 摘要
设置运行时工作线程栈大小
desktop-tauri/src-tauri/server/src/lib.rs, desktop-tauri/src-tauri/server/src/bin/agent2api-server.rs
三个运行时构建配置新增 16 MiB 工作线程栈大小设置。global_runtime 中还新增了关于请求处理链、栈溢出观测及内存提交的注释。

Estimated code review effort: 2 (Simple) | ~8 minutes

Change: Bug fix

Suggested reviewers: aimod-cc

Merge Risk: Minimal · up to 3d867

This change raises the Tokio worker stack size to 16 MiB to prevent stack-overflow crashes on long-context streaming requests. Request handlers run on the configured runtime, and I found no concrete regression risk. It looks safe to merge.

Security architecture risk: Minimal · up to 3d867

The change consistently increases configured worker-stack capacity without changing access controls, privileges, or request routing. Inspection found no introduced material security, failure-containment, or rollback risk.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The directly changed resource boundary is worker-stack capacity within existing application processes. The inspected additions neither create a new caller-controlled runtime nor grant additional identity, credential, or service authority.
Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed 标题明确指出主要改动:将 Tokio worker 线程栈从 2 MiB 调整为 16 MiB,并说明目的是修复长上下文大流式请求下的栈溢出崩溃。该标题与变更集(global_runtime、无头运行时及 Windows 服务运行时的栈大小设置)一致,且与 PR 目标相符。标题为单句、具体,可读性良好。
Description check Passed 描述仅包含“问题现象(生产观测)”一个小节标题,没有给出具体现象、复现条件或日志内容。它与变更主题(栈溢出崩溃)相关,但信息很少。按照宽松标准,描述与变更存在关联,因此判定通过。
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Some tools did not complete. Review the errors below.

Clippy (1.99.1)

Clippy execution failed

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 930b33ad-7167-4ae0-87cd-e2b612f4746f
Commits

Reviewing files that changed from the base of the PR and between e8bed73 and 3d8679c.

Files selected for processing: 2

  • desktop-tauri/src-tauri/server/src/bin/agent2api-server.rs
  • desktop-tauri/src-tauri/server/src/lib.rs

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

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