M2芯片Mac上执行Rails测试出现段错误求助
Rails测试在M2 MacBook上崩溃的问题
环境
- M2芯片MacBook
- Ruby 3.1.1(通过rbenv管理)
- Rails项目,依赖
ethon-0.16.0、typhoeus-1.4.1
问题现象
执行bin/rails test时程序崩溃,需按Ctrl+C退出,错误日志开头如下:
/Users/tristan/.rbenv/versions/3.1.1/lib/ruby/gems/3.1.0/gems/ethon-0.16.0/lib/ethon/easy/operations.rb:30: [BUG] Segmentation fault at 0x0000000000000110 ruby 3.1.1p18 (2022-02-18 revision 53f5fc4236) [arm64-darwin22] -- Crash Report log information ---------------------- See Crash Report log file in one of the following locations: * ~/Library/Logs/DiagnosticReports * /Library/Logs/DiagnosticReports for more details. Don't forget to include the above Crash Report log file in bug reports. -- Control frame information ------------------------------------ c:0124 p:---- s:0792 e:000791 CFUNC :easy_perform c:0123 p:0014 s:0787 e:000786 METHOD /Users/tristan/.rbenv/versions/3.1.1/lib/ruby/gems/3.1.0/gems/ethon-0.16.0/lib/ethon/easy/operations.rb:30 c:0122 p:0020 s:0783 e:000782 METHOD /Users/tristan/.rbenv/versions/3.1.1/lib/ruby/gems/3.1.0/gems/typhoeus-1.4.1/lib/typhoeus/request/operations.rb:16 c:0121 p:0025 s:0778 e:000777 METHOD /Users/tristan/.rbenv/versions/3.1.1/lib/ruby/gems/3.1.0/gems/typhoeus-1.4.1/lib/typhoeus/request/cacheable.rb:18 c:0120 p:0033 s:0773 e:000772 METHOD /Users/tristan/.rbenv/versions/3.1.1/lib/ruby/gems/3.1.0/gems/typhoeus-1.4.1/lib/typhoeus/request/block_connection.rb:31 c:0119 p:0040 s:0769 e:000768 METHOD /Users/tristan/.rbenv/versions/3.1.1/lib/ruby/gems/3.1.0/gems/typhoeus-1.4.1/lib/typhoeus/request/stubbable.rb:25 c:0118 p:0019 s:0764 e:000763 METHOD /Users/tristan/.rbenv/versions/3.1.1/lib/ruby/gems/3.1.0/gems/typhoeus-1.4.1/lib/typhoeus/request/before.rb:26 c:0117 p:0025 s:0760 e:000759 METHOD /Users/tristan/.rbenv/versions/3.1.1/lib/ruby/gems/3.1.0/gems/typhoeus-1.4.1/lib/typhoeus/request/actions.rb:38 c:0116 p:0085 s:0754 e:000753 METHOD /Users/tristan/workspace/lakaa/monorepo/backend/app/services/pdf_service.rb:24 c:0115 p:0660 s:0744 e:000743 METHOD /Users/tristan/workspace/lakaa/monorepo/backend/app/actions/reports/build_digest.rb:100
已尝试的方案
- 使用
sudo运行测试,仍崩溃,报错:objc[77684]: +[NSNumber initialize] may have been in progress in another thread when fork() was called. We cannot safely call it or ignore it in the fork() child process. Crashing instead. Set a breakpoint on objc_initializeAfterForkError to debug. - 针对Ethon的崩溃尝试了GitHub上的相关修复方案,但随后出现pg相关崩溃
- 仅运行单个测试文件或小批量测试时无报错,怀疑存在内存泄漏问题
补充信息
该测试在GitHub CI和Docker环境中可正常运行
解决方案
1. 升级Ruby版本
Ruby 3.1.x在ARM架构Mac上存在兼容性缺陷,建议升级到3.2.x或3.3.x版本(通过rbenv安装对应版本后重新执行bundle install)。新版本对arm64-darwin的底层支持更完善,能修复不少fork和内存相关问题。
2. 调整Ethon/Typhoeus依赖版本
- 尝试升级
ethon到最新稳定版(当前为0.17.x),同步升级typhoeus到适配版本,新版本可能已修复ARM架构下的段错误问题。 - 如果升级后仍有问题,可临时替换Typhoeus为Faraday+Net::HTTP组合,避开Ethon的底层C扩展依赖。
3. 解决fork()导致的objc初始化错误
运行测试前设置环境变量:
export OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES
该变量会关闭Objective-C的fork后初始化安全检查,解决sudo运行时的崩溃问题。
4. 排查内存泄漏与测试并行问题
- 关闭Rails测试并行模式:在
test/test_helper.rb中添加或修改:
并行测试时多进程共享资源可能触发内存异常,单进程运行可验证是否为并行导致的崩溃。parallelize(workers: 1) - 使用
memory_profiler工具分析pdf_service.rb和build_digest.rb中的代码,检查是否存在未释放的资源或循环引用导致内存泄漏。
5. 重新编译依赖gem
ARM架构下部分原生扩展gem可能编译不完整,执行以下命令重新编译所有依赖:
bundle pristine # 或针对单个gem: bundle pristine ethon pg
内容的提问来源于stack exchange,提问作者TristanB12
相关产品推荐
相关产品推荐

