Skip to content

[BUG?] Coroutine::cancel not cancelling coroutine. #4326

@ValiDrv

Description

@ValiDrv

Please answer these questions before submitting your issue. Thanks!

  1. What did you do? If possible, provide a simple script for reproducing the error.

While testing Coroutine::cancel I noticed that the call will only cancel one IO call within the cancelled coroutine.
So either the documentation lost something in translation, or it doesn't actually cancel the coroutine.

<?php
use Swoole\Coroutine;

foreach (['php --ri swoole', 'uname -a', 'php -v', 'gcc -v'] as $cmd) {
    echo "\n----- $cmd ----\n";
    echo `$cmd`;
    echo "\n------------\n";
}

Co\run(function () {
    echo "\n Hello - " . time();
    $c = Co\go(function () {
        Co\defer(function () {
            echo "\n Cancel - " . time();
        });
        Co::sleep(2);
        echo "\n Hello A - " . time() . " " . (Coroutine::isCanceled() ? 'Y' : 'N');
        Co::sleep(2);
        echo "\n Hello B - " . time() . " " . (Coroutine::isCanceled() ? 'Y' : 'N');
    });
    echo "\n Started: $c";
    if (Coroutine::cancel($c) === true) {
        echo "\n Cancelled: $c";
    } else {
        echo "\nswoole_last_error: " . swoole_last_error();
    }
});
  1. What did you expect to see?

Expected the coroutine #2 to be cancelled, and not be processed any more

 Hello - 1626696957
 Started: 2
 Cancelled: 2
 Cancel - 1626696957 <-- should be the start time (+0-1sec on second change)
  1. What did you see instead?

The first sleep was interrupted, the coroutine continued to run.

 Hello - 1626696957
 Started: 2
 Hello A - 1626696957 Y
 Cancelled: 2
 Hello B - 1626696959 N  <--- at the very least, this should probably be Y for cid #2
 Cancel - 1626696959 <-- it's actually the start time +2sec, since the second `sleep` was not cancelled.
  1. What version of Swoole are you using (show your php --ri swoole)?
----- php --ri swoole ----

swoole

Swoole => enabled
Author => Swoole Team <team@swoole.com>
Version => 4.7.0
Built => Jul 16 2021 10:49:14
coroutine => enabled with boost asm context
epoll => enabled
eventfd => enabled
signalfd => enabled
cpu_affinity => enabled
spinlock => enabled
rwlock => enabled
sockets => enabled
openssl => OpenSSL 1.1.1d  10 Sep 2019
dtls => enabled
http2 => enabled
pcre => enabled
zlib => 1.2.11
mutex_timedlock => enabled
pthread_barrier => enabled
futex => enabled
mysqlnd => enabled
async_redis => enabled

Directive => Local Value => Master Value
swoole.enable_coroutine => On => On
swoole.enable_library => On => On
swoole.enable_preemptive_scheduler => Off => Off
swoole.display_errors => On => On
swoole.use_shortname => On => On
swoole.unixsock_buffer_size => 8388608 => 8388608
  1. What is your machine environment used (show your uname -a & php -v & gcc -v) ?
----- uname -a ----
Linux ca60602a12a0 4.15.0-46-generic #49-Ubuntu SMP Wed Feb 6 09:33:07 UTC 2019 x86_64 GNU/Linux


----- php -v ----
PHP 8.0.0 (cli) (built: Dec  1 2020 03:14:26) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.0-dev, Copyright (c) Zend Technologies
    with Zend OPcache v8.0.0, Copyright (c), by Zend Technologies


----- gcc -v ----
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/8/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 8.3.0-6' --with-bugurl=file:///usr/share/doc/gcc-8/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++ --prefix=/usr --with-gcc-major-version-only --program-suffix=-8 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --enable-default-pie --with-system-zlib --with-target-system-zlib --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 8.3.0 (Debian 8.3.0-6) 

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions