swoole-src icon indicating copy to clipboard operation
swoole-src copied to clipboard

The try structure does not catch some errors

Open Alion548 opened this issue 3 years ago • 3 comments

Please answer these questions before submitting your issue.

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

use Co\Http\Client;

register_shutdown_function(function () {
    var_dump('EXIT');
});

\Co\run(function () {
    try {
        $http = new Client('1.1.1.1', 443, true);

        go(function () use(&$http) {
            $http->get('/');
        });

        go(function () use(&$http) {
            try {
                $http->get('/'); // Trigger bugs on purpose !!
            } catch (\Error $e) {
                var_dump($e->getMessage());
                exit();
            }
        });

    } catch (\Error $e){
        var_dump($e->getMessage());
        exit();
    }
});

  1. What did you expect to see?
PHP Fatal error:  Uncaught Swoole\Error: Socket#5 has already been bound to another coroutine#2, writing of the same socket in coroutine#3 at the same time is not allowed in /error.php:14
Stack trace:
#0 /error.php(14): Swoole\Coroutine\Http\Client->get('/')
#1 [internal function]: {closure}()
#2 {main}
  thrown in /error.php on line 16
  1. What did you see instead? The try construct can catch similar errors, allowing it to do a cleanup on exiting with an error.

  2. What version of Swoole are you using (show your php --ri swoole)?

php --ri swoole

swoole

Swoole => enabled
Author => Swoole Team <[email protected]>
Version => 4.8.6
Built => Feb 12 2022 20:18:25
coroutine => enabled with boost asm context
epoll => enabled
eventfd => enabled
signalfd => enabled
cpu_affinity => enabled
spinlock => enabled
rwlock => enabled
openssl => OpenSSL 1.1.1l  FIPS 24 Aug 2021
dtls => enabled
http2 => enabled
pcre => enabled
c-ares => 1.17.2
zlib => 1.2.11
brotli => E16777225/D16777225
mutex_timedlock => enabled
pthread_barrier => enabled
futex => 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) ?
PHP 8.1.2 (cli) (built: Feb 12 2022 20:17:47) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies

Alion548 avatar Feb 12 '22 17:02 Alion548

The exception is throw at line 14, from the $http->get('/'), the one that is not wrapped in a try..catch block. Exceptions thrown inside coroutines aren't catchable by outter try..catch blocks, that is why it isn't couth by the try..catch block at the Co\run level.

leocavalcante avatar Feb 13 '22 02:02 leocavalcante

The exception is throw at line 14, from the $http->get('/'), the one that is not wrapped in a try..catch block. Exceptions thrown inside coroutines aren't catchable by outter try..catch blocks, that is why it isn't couth by the try..catch block at the Co\run level.

try.catch block wrapping still can't catch

Alion548 avatar Feb 21 '22 04:02 Alion548

Hi. This exception is swoole fatal error not php fatal error so it can not be catched. The programs can not execute shutdown callbacks that were registered by register_shutdown_function because it will cause some terrible error even crash. You need to fix your program to avoid throwing such exception.

NathanFreeman avatar Feb 21 '22 09:02 NathanFreeman