simplex-chat-python crashes on Windows: msvcrt.free on GHC-heap pointer (chat_migrate_init access violation) — propose exporting chat_free
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 68/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 冷清
调研方向
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:37and:45:
and returned from thebuf <- mallocBytes (len + 1) -- GHC runtime heapforeign export ccallfunctions insrc/Simplex/Chat/Mobile.hs. - The Python binding frees them with the C runtime's
free(msvcrt on win32) inpackages/simplex-chat-python/src/simplex_chat/core.py::_read_and_free. - On POSIX this happens to work because glibc's
malloc/freeis 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 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
simplex-chat/simplex-chat 的其他 Issue
-
难度 1/5 1 小时以内 新手友好度 85/100
simplex-chat/simplex-chat#7714 · 1 个 reaction ·
维护者通常 1 天内回复
-
bug triage
难度 2/5 1-3 小时 新手友好度 68/100
simplex-chat/simplex-chat#7649 ·
维护者通常 1 天内回复
-
enhancement triage
难度 2/5 1-3 小时 新手友好度 72/100
simplex-chat/simplex-chat#7585 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 68/100
simplex-chat/simplex-chat#7248 ·
维护者通常 1 天内回复
-
Dead link at /docs/guide/readme.html可能重新可做 关联的 PR 已关闭且未合并。 未关闭
难度 1/5 1 小时以内 新手友好度 75/100
simplex-chat/simplex-chat#6782 · 1 条评论 ·
维护者通常 1 天内回复
查看 simplex-chat/simplex-chat 的全部 Issue
相似的 Issue
-
A `--symbolic` entry binding `𝑛3` and `!n3` passes the load check, since `once` compares spellings, and fails mid-run with a `user error`可能已有人在做 @sherenkov 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 66/100
objectionary/phino#1929 · 1 条评论 ·
维护者通常 1 天内回复
-
CJK text is italicized可能已有人在做 @daniel-gregg 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 75/100
-
难度 2/5 1-3 小时 新手友好度 70/100
维护者通常 4 天内回复
-
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 2 天内回复
-
component: hls-eval-plugin type: bug
难度 2/5 1-3 小时 新手友好度 65/100
haskell/haskell-language-server#5144 · 2 条评论 ·
维护者通常 1 天内回复