Skip to content

fix(chat): serialize zstd decompression to prevent concurrent segfault - #44

Merged
ImUpXuu merged 1 commit into
ImUpXuu:mainfrom
noobakong:fix-zstd-thread-safety
Nov 4, 2026
Merged

ImUpXuu merged 1 commit into
ImUpXuu:mainfrom
noobakong:fix-zstd-thread-safety

Conversation

@noobakong

@noobakong noobakong commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

这个 PR 做了什么

给 siwx/api_chat.py 里全局共享的 ZstdDecompressor 加锁,修复聊天翻页时两个并发请求同时解码分片导致 serve 进程段错误崩溃的问题。

python-zstandard 的 ZstdDecompressor 不是线程安全的,而 Flask 开发服务器用多线程处理请求。两个 /api/chat/messages 请求同时对未缓存的分片做全量解码时,会并发操作同一个解压上下文:轻则解出错误内容,重则 SIGSEGV 直接退出。详细分析、日志和复现脚本见 issue。

关联 issue:Fixes #43

改动类型

  • 🐛 Bug 修复
  • ✨ 新功能
  • 📝 文档
  • ♻️ 重构(不改变外部行为)
  • ⚡ 性能优化
  • 🔧 构建 / CI / 发布流程
  • 🖥 平台支持(Windows / macOS)

改动清单

  • siwx/api_chat.py:新增模块级 _ZCTX_LOCK = threading.Lock(),_decode_content 中的 _ZCTX.decompressobj().decompress(raw) 改为在锁内执行。全仓只有这一处在用共享的 ZstdDecompressor。

验证方式

python -m unittest discover -s tests
# Ran 863 tests in 11.248s
# OK (skipped=20)

并发复现脚本(#43 中的 zstd_race.py,8 个线程并发调用真实的 _decode_content,不依赖真实数据):

python zstd_race.py /path/to/SIWX
# 修复前:段错误退出,exit code 139(没有段错误的那几次运行里,也观察到解出错误内容)
# 修复后:连续 3 次 OK: no crash, all decoded correctly
  • 我已在本机跑通完整回归测试
  • 涉及解密 / 导出 / 媒体时,我已在真实数据上验证(真实账号解密后,在大群里向上翻页触发崩溃,修复后可以正常使用)
  • 涉及前端改动时,已执行 node --check <改动的 js 文件>(不涉及前端)

没有把并发压测加进单测:修复前一旦复现就是段错误,会直接带崩整个测试进程,而且压测本身也偏慢,不符合「轻量、自包含」的要求。如果需要,我可以补一个轻量版本。

兼容性检查

  • SIWX_NO_PLUGINS=1 零插件模式下,行为与改动前一致(不涉及插件)
  • 同时考虑了 Windows 与 macOS 的行为差异(纯 Python 层加锁,与平台无关)
  • 涉及持久化路径时统一使用 paths.data_dir(),未依赖当前工作目录(不涉及)
  • 新增/修改的配置项有合理默认值,老用户升级后无需手工干预(无配置项)
  • 未提交密钥、wxid 明文、未脱敏日志或真实聊天记录

截图 / 日志

崩溃时 logs/crash.log 的 faulthandler 栈(已打码):

Fatal Python error: Segmentation fault

Current thread 0x0000000171303000 (most recent call first):
  File "<repo>/siwx/api_chat.py", line 524 in _decode_content
  File "<repo>/siwx/api_chat.py", line 1549 in _decoded_shard
  File "<repo>/siwx/api_chat.py", line 1663 in _filtered_messages_response
  File "<repo>/siwx/api_chat.py", line 1819 in messages
  ...
Extension modules: psutil._psutil_osx, markupsafe._speedups, zstandard.backend_c (total: 3)

The module-level ZstdDecompressor is shared across Flask request threads,
but python-zstandard decompressors are not thread-safe. Two concurrent
/api/chat/messages requests decoding an uncached shard (e.g. scrolling up
in a large group chat) race on the same decompression context, producing
wrong content or crashing the serve process with SIGSEGV.

Guard the decompress call with a lock.

Verified:
- 8-thread repro calling _decode_content: exit 139 before, 3/3 clean runs after
- python -m unittest discover -s tests: Ran 863 tests, OK (skipped=20)

Fixes ImUpXuu#43
@ImUpXuu
ImUpXuu merged commit 8891dd2 into ImUpXuu:main Nov 4, 2026
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.

[Bug] 聊天翻页并发请求时 serve 段错误崩溃:共享 ZstdDecompressor 非线程安全

2 participants