天天看点

记录php7 curl请求报Segmentation fault: 11的解决过程

因为macbook pro送修,在新的mac电脑搭建了php7.1的环境,在跑脚本的时候报Segmentation fault: 11,脚本中请求了一些外部接口,没什么特殊的逻辑。查询无果之后。决定打开coredump 看下进程crash信息

问题追溯

首先打开coredump

➜  ~  ulimit -c unlimited // 开启核心转储
           

然后重新执行脚本,不出意外 Segmentation fault: 11, 此时在/cores目录下会生成格式为core.{PID}的coredump文件

➜  /cores ls
core.8565
           

然后我们看下进程崩溃时的栈信息, (mac下的调试工具时lldb,linux gdb)

➜  /cores lldb -c core.8565
(lldb) target create --core "core.8565"
bt
Core file '/cores/core.8565' (x86_64) was loaded.
(lldb) bt
* thread #1, stop reason = signal SIGSTOP
  * frame #0: 0x0000000106adbfe0 libcurl.4.dylib`sh_delentry + 40
    frame #1: 0x0000000106adbfa9 libcurl.4.dylib`Curl_multi_closed + 122
    frame #2: 0x0000000106b35252 libcares.2.dylib`ares__close_sockets + 222
    frame #3: 0x0000000106b3d9fd libcares.2.dylib`end_query + 307
    frame #4: 0x0000000106b3e31c libcares.2.dylib`process_answer + 1374
    frame #5: 0x0000000106b3cef3 libcares.2.dylib`processfds + 1428
    frame #6: 0x0000000106af0963 libcurl.4.dylib`waitperform + 244
    frame #7: 0x0000000106af077c libcurl.4.dylib`Curl_resolver_is_resolved + 55
    frame #8: 0x0000000106adb4e5 libcurl.4.dylib`multi_runsingle + 2645
    frame #9: 0x0000000106ada995 libcurl.4.dylib`curl_multi_perform + 133
    frame #10: 0x0000000106ad449e libcurl.4.dylib`curl_easy_perform + 369
    frame #11: 0x000000010593d438 php`zif_curl_exec + 120
    frame #12: 0x0000000105c554b8 php`ZEND_DO_FCALL_BY_NAME_SPEC_RETVAL_UNUSED_HANDLER + 276
    frame #13: 0x0000000105c22571 php`execute_ex + 98
    frame #14: 0x0000000105bd86cd php`zend_call_function + 1510
    frame #15: 0x0000000105b00803 php`zif_call_user_func_array + 233
    frame #16: 0x0000000105c55890 php`ZEND_DO_FCALL_BY_NAME_SPEC_RETVAL_USED_HANDLER + 301
    frame #17: 0x0000000105c22571 php`execute_ex + 98
    frame #18: 0x0000000105c227c2 php`zend_execute + 531
    frame #19: 0x0000000105be6b72 php`zend_execute_scripts + 277
    frame #20: 0x0000000105b910d5 php`php_execute_script + 629
    frame #21: 0x0000000105c7fb81 php`do_cli + 3855
    frame #22: 0x0000000105c7eaff php`main + 1266
    frame #23: 0x00007fff7495e3d5 libdyld.dylib`start + 1
(lldb)
           

可以看到程序崩溃位置在此处,可以判断是发起curl请求的时候 libcurl crash掉了, 原因是curl 7.65.1的一个问题,具体可查证

https://bugs.php.net/bug.php?id=78145

https://github.com/curl/curl/pull/3997

frame #0: 0x0000000106adbfe0 libcurl.4.dylib`sh_delentry + 40
           

解决方案

1.mac下可 brew install [email protected] --build-from-source 执行此命令会从源码包重新make ,自动解决curl依赖的问题(已测试)

2.升级os curl版本到curl 7.65.2或者之后