Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

simplex-chat-python crashes on Windows: msvcrt.free on GHC-heap pointer (chat_migrate_init access violation) — propose exporting chat_free

未关闭
#7,288 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
68/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
冷清
技术栈
cpp, haskell, nodejs, python

调研方向

Start with src/Simplex/Chat/Mobile/Shared.hs, src/Simplex/Chat/Mobile.hs, and the binding call sites in packages/simplex-chat-python/src/simplex_chat/core.py and packages/simplex-chat-nodejs/cpp/simplex.cc. Reproduce the Windows failure with the documented ChatApi.init command, then verify that both bindings use the library-owned deallocator and that the command completes without the access violation.

由索引模型根据 Issue 内容生成。

描述

Bug: simplex-chat-python crashes on Windows — msvcrt.free on a GHC-heap pointer

Summary

On Windows, simplex-chat-python (simplex_chat/core.py::_read_and_free) frees the result strings returned by libsimplex via msvcrt.free. But libsimplex is GHC/mingw-compiled, and those strings are allocated with Foreign.Marshal.Alloc.mallocBytes — which allocates from the GHC runtime's own heap, not msvcrt's heap. Freeing a GHC-heap pointer through msvcrt.free is a cross-CRT free, and it crashes on the very first FFI call:

OSError: exception: access violation reading 0xFFFFFFFFFFFFFFFF

This makes the Python package unusable on Windows out of the box.

Reproduce
pip install simplex-chat
python -c "import asyncio; from simplex_chat import ChatApi, SqliteDb; asyncio.run(ChatApi.init(SqliteDb(file_prefix='x')))"

Crash occurs inside chat_migrate_init → _read_and_free → _native.libc().free(...).

Root cause (source-level)
  • Result strings are built in src/Simplex/Chat/Mobile/Shared.hs:37 and :45:
    buf <- mallocBytes (len + 1)   -- GHC runtime heap
    
    and returned from the foreign export ccall functions in src/Simplex/Chat/Mobile.hs.
  • The Python binding frees them with the C runtime's free (msvcrt on win32) in packages/simplex-chat-python/src/simplex_chat/core.py::_read_and_free.
  • On POSIX this happens to work because glibc's malloc/free is the shared allocator both sides resolve to. On Windows the two heaps are distinct, so the free is invalid.
Why the Node.js binding doesn't crash

packages/simplex-chat-nodejs/cpp/simplex.cc:163 also calls plain free(c_res). It gets away with it only because the binding runs inside a Node/V8 process whose CRT matches what libsimplex was linked against in that build. That's a coincidence of the host process, not a correct contract — the same latent mismatch exists there.

Proposed fix

Have libsimplex export its own deallocator so every caller frees with the matching allocator:

foreign export ccall "chat_free" cChatFree :: CString -> IO ()

cChatFree :: CString -> IO ()
cChatFree = Foreign.Marshal.Alloc.free

Then each binding calls chat_free (instead of libc().free / free) on the returned pointer. This is the standard pattern for C libraries that hand out heap-allocated buffers (cf. SDL_free, OPENSSL_free, sqlite3_free) and removes the cross-CRT assumption entirely on all platforms.

I'm happy to open a PR implementing chat_free plus the Python (_read_and_free) and Node (HandleCResult) call-site changes if you'd like — let me know your preferred name/signature.

Workaround (applied locally)

On win32, skip the free and only copy the string. The per-call JSON result strings are small (KBs), so the bounded leak is far safer than a hard crash; POSIX keeps the original free path.

Environment
  • simplex-chat (Python) 6.5.6, LIBS_VERSION 6.5.6
  • Windows 10/11 x86_64
  • Python 3.11+
主要语言
Haskell
星标
19.5k
派生
1.4k
平均合并
1 天 20 小时
30 天内合并 PR
126

环境准备

  • 提供 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

simplex-chat/simplex-chat 的其他 Issue

查看 simplex-chat/simplex-chat 的全部 Issue

相似的 Issue

更多 Haskell Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。