amphp / ext-uv

Other
189 stars 28 forks source link

uv_queue_work() - Error in `zts-php': double free or corruption (fasttop) #46

Open uasan opened 7 years ago

uasan commented 7 years ago
PHP 7.1.5 (cli) (built: May  9 2017 17:55:16) ( ZTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.1.5, Copyright (c) 1999-2017, by Zend Technologies

libuv Support => enabled
Version => 0.2.1
libuv Version => 1.10
<?php

$loop  = uv_default_loop();

$thread = function()
{
    echo "THREAD-CALL\n";
};

$after = function()
{
    echo "THREAD-CALL-AFTER\n";
};

uv_queue_work($loop, $thread, $after);
uv_queue_work($loop, $thread, $after);

$timer = uv_timer_init($loop);

uv_timer_start($timer, 1000, 1000, function()
{
    static $i = 0;

    echo "TIMER: $i\n";

    $i++;
});

uv_run($loop);
*** Error in `zts-php': double free or corruption (fasttop): 0x000055ae15c1b1f0 ***
======= Backtrace: =========
/lib64/libc.so.6(+0x791fb)[0x7f21dc3b81fb]
/lib64/libc.so.6(+0x8288a)[0x7f21dc3c188a]
/lib64/libc.so.6(cfree+0x4c)[0x7f21dc3c52bc]
zts-php(zend_hash_destroy+0x146)[0x55ae146ab706]
zts-php(tsrm_free_interpreter_context+0x4a)[0x55ae146218ea]
/usr/lib64/php-zts/modules/uv.so(+0x1fbb2)[0x7f21d0d23bb2]
/usr/lib64/php-zts/modules/uv.so(+0x1fc60)[0x7f21d0d23c60]
/lib64/libuv.so.1(+0x96d4)[0x7f21d0ae76d4]
/lib64/libpthread.so.0(+0x76ca)[0x7f21ddcd56ca]
/lib64/libc.so.6(clone+0x5f)[0x7f21dc446f7f]
======= Memory map: ========
....
Aborted (core dumped)
bwoebi commented 7 years ago

This thing is very much unsafe … I definitely wouldn't use it currently :x … I probably should remove that thing altogether here.

That's what ext/pthreads is actually for… Use that, if you need threads in a safe way.

uasan commented 7 years ago

I resolve the cause, this problem in php-ext-raphf is thread not safe, without it everything works well. Do not delete uv_queue_work() function.

kelunik commented 7 years ago

Did you try with PHP 7.2? ZTS is broken in PHP 7.0 and 7.1.

uasan commented 6 years ago

# zts-php -v

PHP 7.2.0 (cli) (built: Nov 28 2017 19:54:32) ( ZTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2017 Zend Technologies

_# zts-php test_uv_queuework.php

THREAD-CALL
THREAD-CALL-AFTER
THREAD-CALL
*** Error in `zts-php': double free or corruption (fasttop): 0x00005557c12a77d0 ***
======= Backtrace: =========
/lib64/libc.so.6(+0x7cbac)[0x7f6b75e1dbac]
/lib64/libc.so.6(+0x87a59)[0x7f6b75e28a59]
/lib64/libc.so.6(cfree+0x16e)[0x7f6b75e2e3be]
zts-php(zend_hash_destroy+0xfa)[0x5557c024ffda]
zts-php(tsrm_free_interpreter_context+0x55)[0x5557c01c57c5]
/usr/lib64/php-zts/modules/uv.so(+0x1e3e9)[0x7f6b72ee43e9]
/usr/lib64/php-zts/modules/uv.so(+0x1e48a)[0x7f6b72ee448a]
/lib64/libuv.so.1(+0x96d4)[0x7f6b72ca96d4]
/lib64/libpthread.so.0(+0x736d)[0x7f6b7776d36d]
/lib64/libc.so.6(clone+0x3f)[0x7f6b75eb1e1f]
...
Aborted (core dumped)
TheTechsTech commented 1 year ago

For future reference, I find the issues with other extensions installed.

Having opcache enabled is the main problem, and using xdebug too does not play well.

I find segmentation faults really happening on type of variables being referenced in the callbacks. Everything executes as expected, but segfaults when done, like using stream_socket_pair()

As the current Windows version does not do after callbacks, but using uv_async_send that behavior can be achieved, create a thread queue for the callbacks.

So removing https://github.com/amphp/ext-uv/issues/83 not really a good idea.