Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The try structure does not catch some errors #4604

Open
Alion548 opened this issue Feb 12, 2022 · 3 comments
Open

The try structure does not catch some errors #4604

Alion548 opened this issue Feb 12, 2022 · 3 comments
Labels

Comments

@Alion548
Copy link

Alion548 commented Feb 12, 2022

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
@leocavalcante
Copy link
Member

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.

@Alion548
Copy link
Author

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

@NathanFreeman
Copy link
Member

NathanFreeman commented Feb 21, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants