krakjoe/pthreads

Segmentation fault when shutting down "special workers"

Opened this issue · 3 comments

Environment

  • PHP: PHP 7.2.3 (cli) (built: Mar 16 2018 19:48:29) ( ZTS ) with Xdebug 2.6.0 (no different result without xdebug)
  • pthreads: 3.1.7dev
  • OS: Debian Jessie

Summary

I am trying to implement for reactphp filesystem (asychronous evented filesystem access) a pthreads adapter. I have implemented for a different project a pthreads worker pool manager, with workers which maintain an event loop to send messages forth and back.

When executing more than one threaded object, I get a segfault (core dump below) when shutting down the workers on Linux (same on Windows), but not while running them.

For the different project (specifically a framework for running a discord bot's commands in a separate thread) I do not get any segfaults at all, even when sending 20 threaded objects to the worker. So I'm assuming (as I know little to nothing) that there is a specific problem I'm running at with the pthreads adapter.

Reproducing Code

I'm not really sure how I can minify the complete code to a MCVE, as it seems to be a complex problem (illegal access in zend).

The segfault can be seen when running the phpunit test of the pthreads adapter (composer install && composer require charlottedunois/phoebe dev-master).

vendor/bin/phpunit tests/Pthreads/AdapterTest.php

The source code for the pool manager lives at https://github.com/CharlotteDunois/Phoebe.
The source code for the filesystem adapter lives at https://github.com/CharlotteDunoisLabs/filesystem/tree/feature-pthreads-new/src/Pthreads.

Expected Output

No segfault. :)

Actual Output

Core dump:

Core was generated by `php-zts vendor/bin/phpunit tests/Pthreads/AdapterTest.php'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  zend_unmangle_property_name_ex (name=0x7f900bd28740, class_name=class_name@entry=0x7ffddc4f4128, prop_name=prop_name@entry=0x7ffddc4f4130,
    prop_len=prop_len@entry=0x7ffddc4f4138) at /tmp/php-7.2.3/Zend/zend_compile.c:1345
1345            if (!ZSTR_LEN(name) || ZSTR_VAL(name)[0] != '\0') {
[Current thread is 1 (Thread 0x7f901787e840 (LWP 14215))]
(gdb) bt
#0  zend_unmangle_property_name_ex (name=0x7f900bd28740, class_name=class_name@entry=0x7ffddc4f4128, prop_name=prop_name@entry=0x7ffddc4f4130,
    prop_len=prop_len@entry=0x7ffddc4f4138) at /tmp/php-7.2.3/Zend/zend_compile.c:1345
#1  0x00007f900cd0de8e in pthreads_base_init (base=0x7f900cb77cc0) at /tmp/php-7.2.3/ext/pthreads/src/object.c:290
#2  pthreads_base_ctor (base=0x7f900cb77cc0, entry=<optimized out>) at /tmp/php-7.2.3/ext/pthreads/src/object.c:319
#3  0x00007f900cd0e0e0 in pthreads_threaded_ctor (entry=0x7f900c85d148) at /tmp/php-7.2.3/ext/pthreads/src/object.c:166
#4  0x00000000008e28ab in _object_and_properties_init (arg=arg@entry=0x7f900e0230d0, class_type=class_type@entry=0x7f900c85d148,
    properties=properties@entry=0x0) at /tmp/php-7.2.3/Zend/zend_API.c:1332
#5  0x00000000008e29b7 in _object_init_ex (arg=arg@entry=0x7f900e0230d0, class_type=class_type@entry=0x7f900c85d148) at /tmp/php-7.2.3/Zend/zend_API.c:1340
#6  0x0000000000989fef in ZEND_NEW_SPEC_CONST_HANDLER () at /tmp/php-7.2.3/Zend/zend_vm_execute.h:3231
#7  0x0000000000997481 in execute_ex (ex=0x7f900bd28740) at /tmp/php-7.2.3/Zend/zend_vm_execute.h:59812
#8  0x00007f900cd06275 in pthreads_execute_ex (data=<optimized out>) at /tmp/php-7.2.3/ext/pthreads/php_pthreads.c:135
#9  0x00007f900ddc9d42 in xdebug_execute_ex (execute_data=0x7f900e023070) at /tmp/pear/temp/xdebug/xdebug.c:1912
#10 0x0000000000925166 in ZEND_DO_FCALL_SPEC_RETVAL_UNUSED_HANDLER () at /tmp/php-7.2.3/Zend/zend_vm_execute.h:888
#11 0x0000000000997481 in execute_ex (ex=0x7f900bd28740) at /tmp/php-7.2.3/Zend/zend_vm_execute.h:59812
#12 0x00007f900cd06275 in pthreads_execute_ex (data=<optimized out>) at /tmp/php-7.2.3/ext/pthreads/php_pthreads.c:135
#13 0x00007f900ddc9d42 in xdebug_execute_ex (execute_data=0x7f900e022e90) at /tmp/pear/temp/xdebug/xdebug.c:1912
#14 0x0000000000925166 in ZEND_DO_FCALL_SPEC_RETVAL_UNUSED_HANDLER () at /tmp/php-7.2.3/Zend/zend_vm_execute.h:888
#15 0x0000000000997481 in execute_ex (ex=0x7f900bd28740) at /tmp/php-7.2.3/Zend/zend_vm_execute.h:59812
#16 0x00007f900cd06275 in pthreads_execute_ex (data=<optimized out>) at /tmp/php-7.2.3/ext/pthreads/php_pthreads.c:135
#17 0x00007f900ddc9d42 in xdebug_execute_ex (execute_data=0x7f900e022e20) at /tmp/pear/temp/xdebug/xdebug.c:1912
#18 0x0000000000925166 in ZEND_DO_FCALL_SPEC_RETVAL_UNUSED_HANDLER () at /tmp/php-7.2.3/Zend/zend_vm_execute.h:888
#19 0x0000000000997481 in execute_ex (ex=0x7f900bd28740) at /tmp/php-7.2.3/Zend/zend_vm_execute.h:59812
#20 0x00007f900cd06275 in pthreads_execute_ex (data=<optimized out>) at /tmp/php-7.2.3/ext/pthreads/php_pthreads.c:135
#21 0x00007f900ddc9d42 in xdebug_execute_ex (execute_data=0x7f900e022db0) at /tmp/pear/temp/xdebug/xdebug.c:1912
#22 0x0000000000925166 in ZEND_DO_FCALL_SPEC_RETVAL_UNUSED_HANDLER () at /tmp/php-7.2.3/Zend/zend_vm_execute.h:888
#23 0x0000000000997481 in execute_ex (ex=0x7f900bd28740) at /tmp/php-7.2.3/Zend/zend_vm_execute.h:59812
#24 0x00007f900cd06275 in pthreads_execute_ex (data=<optimized out>) at /tmp/php-7.2.3/ext/pthreads/php_pthreads.c:135
#25 0x00007f900ddc9d42 in xdebug_execute_ex (execute_data=0x7f900e022750) at /tmp/pear/temp/xdebug/xdebug.c:1912
#26 0x0000000000925166 in ZEND_DO_FCALL_SPEC_RETVAL_UNUSED_HANDLER () at /tmp/php-7.2.3/Zend/zend_vm_execute.h:888
#27 0x0000000000997481 in execute_ex (ex=0x7f900bd28740) at /tmp/php-7.2.3/Zend/zend_vm_execute.h:59812
#28 0x00007f900cd06275 in pthreads_execute_ex (data=<optimized out>) at /tmp/php-7.2.3/ext/pthreads/php_pthreads.c:135
#29 0x00007f900ddc9d42 in xdebug_execute_ex (execute_data=0x7f900e020650) at /tmp/pear/temp/xdebug/xdebug.c:1912
#30 0x00000000009258c0 in ZEND_DO_FCALL_SPEC_RETVAL_USED_HANDLER () at /tmp/php-7.2.3/Zend/zend_vm_execute.h:1013
#31 0x0000000000997481 in execute_ex (ex=0x7f900bd28740) at /tmp/php-7.2.3/Zend/zend_vm_execute.h:59812
#32 0x00007f900cd06275 in pthreads_execute_ex (data=<optimized out>) at /tmp/php-7.2.3/ext/pthreads/php_pthreads.c:135
#33 0x00007f900ddc9d42 in xdebug_execute_ex (execute_data=0x7f900e020270) at /tmp/pear/temp/xdebug/xdebug.c:1912
#34 0x00000000009258c0 in ZEND_DO_FCALL_SPEC_RETVAL_USED_HANDLER () at /tmp/php-7.2.3/Zend/zend_vm_execute.h:1013
#35 0x0000000000997481 in execute_ex (ex=0x7f900bd28740) at /tmp/php-7.2.3/Zend/zend_vm_execute.h:59812
#36 0x00007f900cd06275 in pthreads_execute_ex (data=<optimized out>) at /tmp/php-7.2.3/ext/pthreads/php_pthreads.c:135
#37 0x00007f900ddc9d42 in xdebug_execute_ex (execute_data=0x7f900e0201a0) at /tmp/pear/temp/xdebug/xdebug.c:1912
#38 0x0000000000925166 in ZEND_DO_FCALL_SPEC_RETVAL_UNUSED_HANDLER () at /tmp/php-7.2.3/Zend/zend_vm_execute.h:888
#39 0x0000000000997481 in execute_ex (ex=0x7f900bd28740) at /tmp/php-7.2.3/Zend/zend_vm_execute.h:59812
#40 0x00007f900cd06275 in pthreads_execute_ex (data=<optimized out>) at /tmp/php-7.2.3/ext/pthreads/php_pthreads.c:135
#41 0x00007f900ddc9d42 in xdebug_execute_ex (execute_data=0x7f900e020030) at /tmp/pear/temp/xdebug/xdebug.c:1912
#42 0x000000000099cfee in zend_execute (op_array=<optimized out>, return_value=<optimized out>) at /tmp/php-7.2.3/Zend/zend_vm_execute.h:63760
#43 0x00000000008dfae7 in zend_execute_scripts (type=type@entry=8, retval=retval@entry=0x0, file_count=file_count@entry=3) at /tmp/php-7.2.3/Zend/zend.c:1496

Looks like I was able to fix the issue (for Windows) by removing extends \Volatile (which was pretty unneeded) from the class Message (Commit: https://github.com/CharlotteDunois/Phoebe/commit/b1310daec4008efabcae1e2faed5e7dd6707e171).

Windows Build:

PHP 7.2.8 (cli) (built: Jul 18 2018 10:03:03) ( ZTS MSVC15 (Visual C++ 2017) x64 )

For the linux one it still segfaults, but I can see tomorrow if a newer builds works itself out.

I don't know if it would still be worth it to tail this segfault - unless the experts here think otherwise - then it's fine to close this issue.

The segfault persists on Debian Jessie.

PHP 7.2.9 (cli) (built: Sep 18 2018 19:09:05) ( ZTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies

I do am wondering why it works on Windows. 🤔

The segfault looks about the same.

Core was generated by `php-zts vendor/bin/phpunit tests/Pthreads/AdapterTest.php'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  zend_unmangle_property_name_ex (name=0x7feee2928740, class_name=class_name@entry=0x7fff54052708, prop_name=prop_name@entry=0x7fff54052710,
    prop_len=prop_len@entry=0x7fff54052718) at /tmp/php-src/Zend/zend_compile.c:1345
1345            if (!ZSTR_LEN(name) || ZSTR_VAL(name)[0] != '\0') {
[Current thread is 1 (Thread 0x7feeee67fc80 (LWP 7517))]
(gdb) bt
#0  zend_unmangle_property_name_ex (name=0x7feee2928740, class_name=class_name@entry=0x7fff54052708, prop_name=prop_name@entry=0x7fff54052710,
    prop_len=prop_len@entry=0x7fff54052718) at /tmp/php-src/Zend/zend_compile.c:1345
#1  0x00007feee390e03e in pthreads_base_init (base=0x7feee37a9cc0) at /tmp/pthreads/src/object.c:290
#2  pthreads_base_ctor (base=0x7feee37a9cc0, entry=<optimized out>) at /tmp/pthreads/src/object.c:319
#3  0x00007feee390e290 in pthreads_threaded_ctor (entry=0x7feee34572d8) at /tmp/pthreads/src/object.c:166
#4  0x00000000008e3d5b in _object_and_properties_init (arg=arg@entry=0x7feee4c25330, class_type=class_type@entry=0x7feee34572d8,
    properties=properties@entry=0x0) at /tmp/php-src/Zend/zend_API.c:1332
#5  0x00000000008e3e67 in _object_init_ex (arg=arg@entry=0x7feee4c25330, class_type=class_type@entry=0x7feee34572d8) at /tmp/php-src/Zend/zend_API.c:1340
#6  0x000000000098b6af in ZEND_NEW_SPEC_CONST_HANDLER () at /tmp/php-src/Zend/zend_vm_execute.h:3231
#7  0x0000000000998b41 in execute_ex (ex=0x7feee2928740) at /tmp/php-src/Zend/zend_vm_execute.h:59828
#8  0x00007feee3906335 in pthreads_execute_ex (data=<optimized out>) at /tmp/pthreads/php_pthreads.c:135
#9  0x00007feee49c9d42 in xdebug_execute_ex (execute_data=0x7feee4c252d0) at /tmp/pear/temp/xdebug/xdebug.c:1912
#10 0x0000000000926676 in ZEND_DO_FCALL_SPEC_RETVAL_UNUSED_HANDLER () at /tmp/php-src/Zend/zend_vm_execute.h:888
#11 0x0000000000998b41 in execute_ex (ex=0x7feee2928740) at /tmp/php-src/Zend/zend_vm_execute.h:59828
#12 0x00007feee3906335 in pthreads_execute_ex (data=<optimized out>) at /tmp/pthreads/php_pthreads.c:135
#13 0x00007feee49c9d42 in xdebug_execute_ex (execute_data=0x7feee4c250f0) at /tmp/pear/temp/xdebug/xdebug.c:1912
#14 0x0000000000926676 in ZEND_DO_FCALL_SPEC_RETVAL_UNUSED_HANDLER () at /tmp/php-src/Zend/zend_vm_execute.h:888
#15 0x0000000000998b41 in execute_ex (ex=0x7feee2928740) at /tmp/php-src/Zend/zend_vm_execute.h:59828
#16 0x00007feee3906335 in pthreads_execute_ex (data=<optimized out>) at /tmp/pthreads/php_pthreads.c:135
#17 0x00007feee49c9d42 in xdebug_execute_ex (execute_data=0x7feee4c25080) at /tmp/pear/temp/xdebug/xdebug.c:1912
#18 0x0000000000926676 in ZEND_DO_FCALL_SPEC_RETVAL_UNUSED_HANDLER () at /tmp/php-src/Zend/zend_vm_execute.h:888
#19 0x0000000000998b41 in execute_ex (ex=0x7feee2928740) at /tmp/php-src/Zend/zend_vm_execute.h:59828
#20 0x00007feee3906335 in pthreads_execute_ex (data=<optimized out>) at /tmp/pthreads/php_pthreads.c:135
#21 0x00007feee49c9d42 in xdebug_execute_ex (execute_data=0x7feee4c24fe0) at /tmp/pear/temp/xdebug/xdebug.c:1912
#22 0x0000000000926676 in ZEND_DO_FCALL_SPEC_RETVAL_UNUSED_HANDLER () at /tmp/php-src/Zend/zend_vm_execute.h:888
#23 0x0000000000998b41 in execute_ex (ex=0x7feee2928740) at /tmp/php-src/Zend/zend_vm_execute.h:59828
#24 0x00007feee3906335 in pthreads_execute_ex (data=<optimized out>) at /tmp/pthreads/php_pthreads.c:135
#25 0x00007feee49c9d42 in xdebug_execute_ex (execute_data=0x7feee4c248f0) at /tmp/pear/temp/xdebug/xdebug.c:1912
#26 0x0000000000926676 in ZEND_DO_FCALL_SPEC_RETVAL_UNUSED_HANDLER () at /tmp/php-src/Zend/zend_vm_execute.h:888
#27 0x0000000000998b41 in execute_ex (ex=0x7feee2928740) at /tmp/php-src/Zend/zend_vm_execute.h:59828
#28 0x00007feee3906335 in pthreads_execute_ex (data=<optimized out>) at /tmp/pthreads/php_pthreads.c:135
#29 0x00007feee49c9d42 in xdebug_execute_ex (execute_data=0x7feee4c23940) at /tmp/pear/temp/xdebug/xdebug.c:1912
#30 0x0000000000926676 in ZEND_DO_FCALL_SPEC_RETVAL_UNUSED_HANDLER () at /tmp/php-src/Zend/zend_vm_execute.h:888
#31 0x0000000000998b41 in execute_ex (ex=0x7feee2928740) at /tmp/php-src/Zend/zend_vm_execute.h:59828
#32 0x00007feee3906335 in pthreads_execute_ex (data=<optimized out>) at /tmp/pthreads/php_pthreads.c:135
#33 0x00007feee49c9d42 in xdebug_execute_ex (execute_data=0x7feee4c22db0) at /tmp/pear/temp/xdebug/xdebug.c:1912
#34 0x0000000000926676 in ZEND_DO_FCALL_SPEC_RETVAL_UNUSED_HANDLER () at /tmp/php-src/Zend/zend_vm_execute.h:888
#35 0x0000000000998b41 in execute_ex (ex=0x7feee2928740) at /tmp/php-src/Zend/zend_vm_execute.h:59828
#36 0x00007feee3906335 in pthreads_execute_ex (data=<optimized out>) at /tmp/pthreads/php_pthreads.c:135
#37 0x00007feee49c9d42 in xdebug_execute_ex (execute_data=0x7feee4c22750) at /tmp/pear/temp/xdebug/xdebug.c:1912
#38 0x0000000000926676 in ZEND_DO_FCALL_SPEC_RETVAL_UNUSED_HANDLER () at /tmp/php-src/Zend/zend_vm_execute.h:888
#39 0x0000000000998b41 in execute_ex (ex=0x7feee2928740) at /tmp/php-src/Zend/zend_vm_execute.h:59828
#40 0x00007feee3906335 in pthreads_execute_ex (data=<optimized out>) at /tmp/pthreads/php_pthreads.c:135
#41 0x00007feee49c9d42 in xdebug_execute_ex (execute_data=0x7feee4c20650) at /tmp/pear/temp/xdebug/xdebug.c:1912
#42 0x0000000000926dd0 in ZEND_DO_FCALL_SPEC_RETVAL_USED_HANDLER () at /tmp/php-src/Zend/zend_vm_execute.h:1013
#43 0x0000000000998b41 in execute_ex (ex=0x7feee2928740) at /tmp/php-src/Zend/zend_vm_execute.h:59828
#44 0x00007feee3906335 in pthreads_execute_ex (data=<optimized out>) at /tmp/pthreads/php_pthreads.c:135
#45 0x00007feee49c9d42 in xdebug_execute_ex (execute_data=0x7feee4c20270) at /tmp/pear/temp/xdebug/xdebug.c:1912

My issue is definitely fixed in Debian Stretch with PHP build:

PHP 7.2.10 (cli) (built: Sep 22 2018 23:12:11) ( ZTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Xdebug v2.6.0, Copyright (c) 2002-2018, by Derick Rethans

It still segfaulted on Debian Jessie with the PHP 7.2.9 build from previous comment (same configure options).

The unit tests for the filesystem adapter also pass on Travis CI without any issues. So I'll close this issue to cleanup. If you feel like this still needs to be tailed, don't hold back and reopen the issue.