php死循环的故事
1、某日线上php服务器cpu缓慢增长,重启php-fpm之后cpu又恢复正常,过一段时间cpu又飙升上去,按道理讲php-fpm执行超过配置的次数后会自动重启,而cpu飙上去的php-fpm似乎都没有自动重启过,所以此时php-fpm的状态有点像hang住了。
怀疑过1)请求量,2)慢接口,3)并发量,4)最终没想到是程序员造出来的死循环。
2、根据进程追踪,发现php-fpm的cpu一直处于某个方法,不向后继续执行。从下往上找发现有个方法有死循环,他妈的。
[root@php-xxx-xxx tmp]# pstack 3892
#0 0x0000000000b1500e in zend_mm_small_size_to_bin (size=1406601789455) at /php/Zend/zend_alloc.c:1191
#1 0x0000000000b155e2 in zend_mm_alloc_heap (heap=0x7f5699800040, size=256, __zend_filename=0x1450e38 "/php/Zend/zend_smart_str.c", __zend_lineno=155, __zend_orig_filename=0x0, __zend_orig_lineno=0) at /php/Zend/zend_alloc.c:1360
#2 0x0000000000b18270 in _emalloc (size=224, __zend_filename=0x1450e38 "/php/Zend/zend_smart_str.c", __zend_lineno=155, __zend_orig_filename=0x0, __zend_orig_lineno=0) at /php/Zend/zend_alloc.c:2506
#3 0x0000000000ba6ef4 in _smart_string_alloc (str=0x7ffda9567b50, len=1) at /php/Zend/zend_smart_str.c:155
#4 0x0000000000ac133e in smart_string_alloc (str=0x7ffda9567b50, len=1, persistent=0 '\000') at /php/Zend/zend_smart_string.h:61
#5 0x0000000000ac137a in smart_string_appendc_ex (dest=0x7ffda9567b50, ch=85 'U', persistent=0 '\000') at /php/Zend/zend_smart_string.h:82
#6 0x0000000000ac149d in xbuf_format_converter (xbuf=0x7ffda9567b50, is_char=1 '\001', fmt=0x1451eed "Undefined offset: %ld", ap=0x7ffda9568130) at /php/main/spprintf.c:239
#7 0x0000000000ac34c4 in php_printf_to_smart_string (buf=0x7ffda9567b50, format=0x1451eed "Undefined offset: %ld", ap=0x7ffda9568130) at /php/main/spprintf.c:830
#8 0x0000000000b4b587 in zend_vspprintf (pbuf=0x7ffda9567be0, max_len=1024, format=0x1451eed "Undefined offset: %ld", ap=0x7ffda9568130) at /php/Zend/zend.c:184
#9 0x0000000000abaa25 in php_error_cb (type=8, error_filename=0x7f568f19db78 "/xxx.php", error_lineno=293, format=0x1451eed "Undefined offset: %ld", args=0x7ffda9568130) at /php/main/main.c:1203
#10 0x00000000008ba2ff in soap_error_handler (error_num=8, error_filename=0x7f568f19db78 "xxx.php", error_lineno=293, format=0x1451eed "Undefined offset: %ld", args=0x7ffda95682c8) at /php/ext/soap/soap.c:2109
#11 0x0000000000b4d5d3 in zend_error (type=8, format=0x1451eed "Undefined offset: %ld") at /php/Zend/zend.c:1290
#12 0x0000000000bacb41 in zend_undefined_offset (lval=1) at /php/Zend/zend_execute.c:1611
#13 0x0000000000bacef6 in zend_fetch_dimension_address_inner (ht=0x7f568ee51ea0, dim=0x7f568db02548, dim_type=8, type=0) at /php/Zend/zend_execute.c:1697
#14 0x0000000000c1624b in ZEND_FETCH_DIM_R_SPEC_CV_CV_HANDLER () at /php/Zend/zend_vm_execute.h:49217
#15 0x0000000000c1f55b in execute_ex (ex=0x7f569981f520) at /php/Zend/zend_vm_execute.h:60763
#16 0x0000000000b374ba in zend_call_function (fci=0x7ffda9568870, fci_cache=0x7ffda9568820) at /php/Zend/zend_execute_API.c:756
#17 0x0000000000b77cd2 in zend_call_method (object=0x7ffda95689d0, obj_ce=0x7f568ff8a8c8, fn_proxy=0x0, function_name=0x7f568f9a8118 "xxx", function_name_len=32, retval_ptr=0x7ffda95689f0, param_count=0, arg1=0x0, arg2=0x0) at /php/Zend/zend_interfaces.c:103
#18 0x00007f569283899f in yaf_dispatcher_handle (dispatcher=0x7f568ff13e78, request=0x7f56998f8e48, response=0x7ffda9568c90, view=0x7ffda9568b00) at /opt/soft/php7lnmp/sourcepackages/yaf-3.1.3/yaf_dispatcher.c:541
#19 0x00007f5692839f14 in yaf_dispatcher_dispatch (dispatcher=0x7f568ff13e78, response_ptr=0x7ffda9568c90) at /opt/soft/php7lnmp/sourcepackages/yaf-3.1.3/yaf_dispatcher.c:821
#20 0x00007f569283595e in zim_yaf_application_run (execute_data=0x7f569981f4d0, return_value=0x7ffda9568c90) at /opt/soft/php7lnmp/sourcepackages/yaf-3.1.3/yaf_application.c:466
#21 0x0000000000bb2d9a in ZEND_DO_FCALL_SPEC_RETVAL_UNUSED_HANDLER () at /php/Zend/zend_vm_execute.h:984
#22 0x0000000000c1a23f in execute_ex (ex=0x7f569981f030) at /php/Zend/zend_vm_execute.h:55527
#23 0x0000000000c1f865 in zend_execute (op_array=0x7f569987b100, return_value=0x0) at /php/Zend/zend_vm_execute.h:60939
#24 0x0000000000b4e592 in zend_execute_scripts (type=8, retval=0x0, file_count=3) at /php/Zend/zend.c:1568
#25 0x0000000000abdb01 in php_execute_script (primary_file=0x7ffda956b3e0) at /php/main/main.c:2639
#26 0x0000000000c317c9 in main (argc=1, argv=0x7ffda956b618) at /php/sapi/fpm/fpm/fpm_main.c:1951