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

Php crashing with a TypeError even when declare(strict_types=1) is not set, or even set to strict_types=0

Open kladskull opened this issue 4 years ago • 7 comments

Description

The following code crashes in a fatal error if the socket is in a closed state. I have tried to get the status of the port, but have had no luck.

<?php
 Console::log('Opening a server socket on address: ' . $address . ' port: ' . $port);
        $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
        socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1);
        socket_set_nonblock($sock);
        socket_bind($sock, $address, $port);
        socket_listen($sock, 100);
        
        ...
        
        $read = $clients;
        $write = null;
        $except = null;

        // Set up a blocking call to socket_select
        try {
            $reads = @socket_select($read, $write, $except, 0, 250000);
        } catch (Exception) {

            }

Resulted in this output:

PHP Fatal error:  Uncaught TypeError: socket_select(): Argument #1 ($read) contains a closed socket in /home/ubuntu/xeros/src/Node.php:78
Stack trace:
#0 /home/ubuntu/xeros/src/Node.php(78): socket_select()
#1 /home/ubuntu/xeros/bin/xero(63): Xeros\Node->listen()
#2 {main}
  thrown in /home/ubuntu/xeros/src/Node.php on line 78

But I expected this output instead:

No Fatal Type error.. 

Note: converting everything to use stream_socket/etc produces same error.

PHP Version

PHP 8.1.2

Operating System

Ubuntu 20.04

kladskull avatar Feb 17 '22 20:02 kladskull

This isn't related to strict_types.

The error is, as you've noticed, that you're passing a closed socket to socket_select. That isn't going to work. I don't know what code you have to create $clients but you should make sure it doesn't include a socket that is already known to be closed.

damianwadley avatar Feb 17 '22 21:02 damianwadley

Internal function parameters have types and value validations enforced. The type doesn't match what is expected internally - that is the cause of the fatal error. Yes, the port is closed, however it is still a socket. A Try/Catch should be valid in this use case, if at minimum - it should be a warning, not a Fatal Error. My opinion is that- if you're closing a Socket/Resource for any reason, and its resource is closed, it should just "close" with a warning.

I agree about the strict typing, I may have gotten that confused with the internals.

kladskull avatar Feb 17 '22 21:02 kladskull

Uncaught TypeError

You can catch that, but according to "best practise" you should never attempt to catch Errors. If there is any change that the resource has been closed, better check that up-front (e.g. with is_resource()). And while I think a TypeError is not quite right (it should be a ValueError instead), that is intrinsic to all resource types (not only socket/stream resources), but these are considered legacy types anyway, and will likely be replaced by objects in future PHP releases. The only problem I see is that such is_resource() checks makes switching to objects a more serious BC break.

So, the behavior is not a bug, but we should think about some way to provide userland code with a forward compatible way to check for closed resources.

cmb69 avatar Feb 18 '22 10:02 cmb69

From what I remember from making socket server, socket/resource is closed only when explicitly closed using socket_close and there is no instance when broken connection causes this error. What is more important when e.g. remote host disconnects, socket_select will return immediately. Thus there is absolutely no reason to keep closed socket around even if socket_select would accept that. Removing socket from $read should follow any socket_close.

Before 8.0 $read = array_filter($read, 'is_resource') would be sufficient, but I have no idea how to cleanly check that in the current version.

KapitanOczywisty avatar Feb 18 '22 11:02 KapitanOczywisty

You can catch that

In the example provided, it is not catching the error, unless I am missing something fundamental. From my understanding, a Fatal error is not catchable.

kladskull avatar Feb 18 '22 14:02 kladskull

You would need to catch (TypeError) (or even catch (Error)), but again, that is not recommended.

cmb69 avatar Feb 18 '22 14:02 cmb69

As explained above, the error can be caught with the correct exception type, and the error itself is intentional (yes, including on strict_types=0), so I'm closing this.

nikic avatar Feb 27 '22 12:02 nikic