PHP 如何修复调用fclose 时的段错误?

PHP how to fix segmentation fault when call fclose?

我有 Ubuntu 18.04,PHP CLI 7.2.2 ZTS。当我调用以下代码时: 我不是每次都崩溃,但有时会崩溃。 $stream 变量包含资源,由 stream_socket_client 函数打开。

if (is_resource($stream)) {
    fclose($stream);
}

我得到下一个分割错误:

Thread 4 "php" received signal SIGPIPE, Broken pipe.
[Switching to Thread 0x7fffe3fff700 (LWP 22374)]
0x00007ffff5ea82b7 in __libc_write (fd=47, buf=0x7fffd9de7e23, nbytes=136) at ../sysdeps/unix/sysv/linux/write.c:27
27      ../sysdeps/unix/sysv/linux/write.c: No such file or directory.
(gdb) bt
#0  0x00007ffff5ea82b7 in __libc_write (fd=47, buf=0x7fffd9de7e23, nbytes=136) at ../sysdeps/unix/sysv/linux/write.c:27
#1  0x00007ffff542dcd5 in ?? () from /usr/lib/x86_64-linux-gnu/libcrypto.so.1.1
#2  0x00007ffff542907a in ?? () from /usr/lib/x86_64-linux-gnu/libcrypto.so.1.1
#3  0x00007ffff54280d5 in ?? () from /usr/lib/x86_64-linux-gnu/libcrypto.so.1.1
#4  0x00007ffff5428573 in BIO_write () from /usr/lib/x86_64-linux-gnu/libcrypto.so.1.1
#5  0x00007ffff586ac67 in ?? () from /usr/lib/x86_64-linux-gnu/libssl.so.1.1
#6  0x00007ffff586bb85 in ?? () from /usr/lib/x86_64-linux-gnu/libssl.so.1.1
#7  0x00007ffff586bddf in ?? () from /usr/lib/x86_64-linux-gnu/libssl.so.1.1
#8  0x00007ffff587e78c in ?? () from /usr/lib/x86_64-linux-gnu/libssl.so.1.1
#9  0x00007ffff587e8c3 in SSL_write () from /usr/lib/x86_64-linux-gnu/libssl.so.1.1
#10 0x000055555578b460 in php_openssl_sockop_io (read=0, stream=0x7fffa031c500, buf=0x7fff9ed559d8 "2 AUTHENTICATE PLAIN Zm5wY2FtcG9iYXNzb0B2aXJnaWxpby5pdABmbnBjYW1wb2Jhc3NvQHZpcmdpbGlvLml0AGJhcmJhbm9ubm8=\r\n", 
    count=107) at /home/mike/Documents/PHP/php-src-php-7.2.2/ext/openssl/xp_ssl.c:2049
#11 0x000055555578b21a in php_openssl_sockop_write (stream=0x7fffa031c500, buf=0x7fff9ed559d8 "2 AUTHENTICATE PLAIN Zm5wY2FtcG9iYXNzb0B2aXJnaWxpby5pdABmbnBjYW1wb2Jhc3NvQHZpcmdpbGlvLml0AGJhcmJhbm9ubm8=\r\n", 
    count=107) at /home/mike/Documents/PHP/php-src-php-7.2.2/ext/openssl/xp_ssl.c:1971
#12 0x0000555555e65b45 in _php_stream_write_buffer (stream=0x7fffa031c500, buf=0x7fff9ed559d8 "2 AUTHENTICATE PLAIN Zm5wY2FtcG9iYXNzb0B2aXJnaWxpby5pdABmbnBjYW1wb2Jhc3NvQHZpcmdpbGlvLml0AGJhcmJhbm9ubm8=\r\n", 
    count=107) at /home/mike/Documents/PHP/php-src-php-7.2.2/main/streams/streams.c:1088
#13 0x0000555555e65e96 in _php_stream_write (stream=0x7fffa031c500, buf=0x7fff9ed559d8 "2 AUTHENTICATE PLAIN Zm5wY2FtcG9iYXNzb0B2aXJnaWxpby5pdABmbnBjYW1wb2Jhc3NvQHZpcmdpbGlvLml0AGJhcmJhbm9ubm8=\r\n", count=107)
    at /home/mike/Documents/PHP/php-src-php-7.2.2/main/streams/streams.c:1203
#14 0x0000555555cae209 in zif_fwrite (execute_data=0x7fffe8623810, return_value=0x7fffe8623760) at /home/mike/Documents/PHP/php-src-php-7.2.2/ext/standard/file.c:1215
#15 0x0000555555f7978a in ZEND_DO_FCALL_SPEC_RETVAL_USED_HANDLER () at /home/mike/Documents/PHP/php-src-php-7.2.2/Zend/zend_vm_execute.h:1032
#16 0x0000555556005f1d in execute_ex (ex=0x7fffe8623620) at /home/mike/Documents/PHP/php-src-php-7.2.2/Zend/zend_vm_execute.h:59752
#17 0x00007fffeae59583 in pthreads_execute_ex (data=0x7fffe8623620) at /home/mike/Documents/PHP/pthreads/php_pthreads.c:135
#18 0x0000555555f78edb in ZEND_DO_FCALL_SPEC_RETVAL_UNUSED_HANDLER () at /home/mike/Documents/PHP/php-src-php-7.2.2/Zend/zend_vm_execute.h:888
#19 0x0000555556005f10 in execute_ex (ex=0x7fffe8623360) at /home/mike/Documents/PHP/php-src-php-7.2.2/Zend/zend_vm_execute.h:59749
#20 0x00007fffeae59583 in pthreads_execute_ex (data=0x7fffe8623360) at /home/mike/Documents/PHP/pthreads/php_pthreads.c:135
#21 0x0000555555f78edb in ZEND_DO_FCALL_SPEC_RETVAL_UNUSED_HANDLER () at /home/mike/Documents/PHP/php-src-php-7.2.2/Zend/zend_vm_execute.h:888
#22 0x0000555556005f10 in execute_ex (ex=0x7fffe8622ff0) at /home/mike/Documents/PHP/php-src-php-7.2.2/Zend/zend_vm_execute.h:59749
#23 0x00007fffeae59583 in pthreads_execute_ex (data=0x7fffe8622ff0) at /home/mike/Documents/PHP/pthreads/php_pthreads.c:135
#24 0x0000555555f7967c in ZEND_DO_FCALL_SPEC_RETVAL_USED_HANDLER () at /home/mike/Documents/PHP/php-src-php-7.2.2/Zend/zend_vm_execute.h:1013
#25 0x0000555556005f1d in execute_ex (ex=0x7fffe8622950) at /home/mike/Documents/PHP/php-src-php-7.2.2/Zend/zend_vm_execute.h:59752
#26 0x00007fffeae59583 in pthreads_execute_ex (data=0x7fffe8622950) at /home/mike/Documents/PHP/pthreads/php_pthreads.c:135
#27 0x0000555555f7967c in ZEND_DO_FCALL_SPEC_RETVAL_USED_HANDLER () at /home/mike/Documents/PHP/php-src-php-7.2.2/Zend/zend_vm_execute.h:1013
#28 0x0000555556005f1d in execute_ex (ex=0x7fffe8621e10) at /home/mike/Documents/PHP/php-src-php-7.2.2/Zend/zend_vm_execute.h:59752
#29 0x00007fffeae59583 in pthreads_execute_ex (data=0x7fffe8621e10) at /home/mike/Documents/PHP/pthreads/php_pthreads.c:135
---Type <return> to continue, or q <return> to quit---
#30 0x0000555555f7967c in ZEND_DO_FCALL_SPEC_RETVAL_USED_HANDLER () at /home/mike/Documents/PHP/php-src-php-7.2.2/Zend/zend_vm_execute.h:1013
#31 0x0000555556005f1d in execute_ex (ex=0x7fffe8621d10) at /home/mike/Documents/PHP/php-src-php-7.2.2/Zend/zend_vm_execute.h:59752
#32 0x00007fffeae59583 in pthreads_execute_ex (data=0x7fffe8621d10) at /home/mike/Documents/PHP/pthreads/php_pthreads.c:135
#33 0x0000555555f78edb in ZEND_DO_FCALL_SPEC_RETVAL_UNUSED_HANDLER () at /home/mike/Documents/PHP/php-src-php-7.2.2/Zend/zend_vm_execute.h:888
#34 0x0000555556005f10 in execute_ex (ex=0x7fffe8621c20) at /home/mike/Documents/PHP/php-src-php-7.2.2/Zend/zend_vm_execute.h:59749
#35 0x00007fffeae59583 in pthreads_execute_ex (data=0x7fffe8621c20) at /home/mike/Documents/PHP/pthreads/php_pthreads.c:135
#36 0x0000555555f7967c in ZEND_DO_FCALL_SPEC_RETVAL_USED_HANDLER () at /home/mike/Documents/PHP/php-src-php-7.2.2/Zend/zend_vm_execute.h:1013
#37 0x0000555556005f1d in execute_ex (ex=0x7fffe86218c0) at /home/mike/Documents/PHP/php-src-php-7.2.2/Zend/zend_vm_execute.h:59752
#38 0x00007fffeae59583 in pthreads_execute_ex (data=0x7fffe86218c0) at /home/mike/Documents/PHP/pthreads/php_pthreads.c:135
#39 0x0000555555f7967c in ZEND_DO_FCALL_SPEC_RETVAL_USED_HANDLER () at /home/mike/Documents/PHP/php-src-php-7.2.2/Zend/zend_vm_execute.h:1013
#40 0x0000555556005f1d in execute_ex (ex=0x7fffe8621280) at /home/mike/Documents/PHP/php-src-php-7.2.2/Zend/zend_vm_execute.h:59752
#41 0x00007fffeae59583 in pthreads_execute_ex (data=0x7fffe8621280) at /home/mike/Documents/PHP/pthreads/php_pthreads.c:135
#42 0x0000555555f7967c in ZEND_DO_FCALL_SPEC_RETVAL_USED_HANDLER () at /home/mike/Documents/PHP/php-src-php-7.2.2/Zend/zend_vm_execute.h:1013
#43 0x0000555556005f1d in execute_ex (ex=0x7fffe8621030) at /home/mike/Documents/PHP/php-src-php-7.2.2/Zend/zend_vm_execute.h:59752
#44 0x00007fffeae59583 in pthreads_execute_ex (data=0x7fffe8621030) at /home/mike/Documents/PHP/pthreads/php_pthreads.c:135
#45 0x0000555555eec3a6 in zend_call_function (fci=0x7fffe3ffeb80, fci_cache=0x7fffe3ffebb8) at /home/mike/Documents/PHP/php-src-php-7.2.2/Zend/zend_execute_API.c:819
#46 0x00007fffeae6e019 in pthreads_routine_run_function (object=0x7fffeadc5a00, connection=0x7fffe866c900, work=0x7fffe3ffed00) at /home/mike/Documents/PHP/pthreads/src/object.c:481
#47 0x00007fffeae6e3a0 in pthreads_routine (routine=0x7fffffffad70) at /home/mike/Documents/PHP/pthreads/src/object.c:516
#48 0x00007ffff5e9e6db in start_thread (arg=0x7fffe3fff700) at pthread_create.c:463
#49 0x00007ffff3cc088f in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95

问题可能出在哪里?是php核心问题吗?如何避免呢?也许我可以添加额外的资源检查?

您已将启用了加密的流传递给线程,文档中提到,虽然 pthreads 会将流资源传递给另一个线程,但它无法为该流提供安全性。在启用加密的流的情况下,底层 openssl 对象不是线程安全的。

pthreads 已过时,请使用积极开发和支持的 http://php.net/parallel