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

WebSocket: $this->socket->recv(); on client-side ends up with warning and websocket connection auto disconnects. #5374

Closed
fakharksa opened this issue Jun 23, 2024 · 2 comments

Comments

@fakharksa
Copy link

fakharksa commented Jun 23, 2024

Please answer these questions before submitting your issue.

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

I have found that there comes an issue in your client-side recv() function of socket object ...

$this->socket->recv();

When server-side pushes all the messages, the function recv() returns not only "false" but it also issues a warning as below:

PHP Warning: Swoole\Client::recv(): recv() failed, Error: Resource temporarily unavailable[11] in /var/www/html/swoole-prac/websocketclient/wbsocketclient.php on line 87"

Side Note: And, it also gives a warning that there is no property $errMsg on socket object.

And, finally the connection between the server and client is disconnected (causing function onClose() to

Reference to Client-side Code:
Please, see on line 85 of this gist which i am using now.

My server-side code snippet is below:

        $this->server->on('Message', function($webSocketServer, Frame $frame) {
            if ($frame === '') {
                $webSocketServer->close();
            } else if ($frame === false) {
                echo 'errorCode: ' . swoole_last_error() . "\n";
                $webSocketServer->close();
            } else if ($frame->data == 'close' || get_class($frame) === CloseFrame::class || $frame->opcode == 0x08) {
                echo "Close frame received: Code {$frame->code} Reason {$frame->reason}\n";
            } else {
                $i=0;
                while (!$frame->finish) {
                    if ($i > 4000) {
                        $server->disconnect($frame->fd, SWOOLE_WEBSOCKET_CLOSE_NORMAL, "Frame Finish Time Exceeded.");
                    } else {
                        $i++;
                        echo "Frame is not Finished".PHP_EOL;
                        continue;
                    }
                }

                include_once __DIR__ . '/Controllers/WebSocketController.php';
                $sw_websocket_controller = new WebSocketController($webSocketServer, $frame, $this->dbConnectionPools);

                $response = $sw_websocket_controller->handle();

                $webSocketServer->push($frame->fd,
                    json_encode($response),
                    WEBSOCKET_OPCODE_TEXT,
                    SWOOLE_WEBSOCKET_FLAG_FIN); // OpenSwoole\WebSocket\Server::WEBSOCKET_FLAG_FIN
            }
        });

        $this->server->on('Close', function($server, $fd, $reactorId) {
            echo "client {$fd} closed\n ReactorId:{$reactorId}";
        });

My consumer-code of WebSocket-Client-side is as below:

include 'wbsocketclient.php'; // this is name of the websocket client file, as in provided gist above.
$w = new WebSocketClient('127.0.0.1', 9501);
if ($x = $w->connect()) {
    var_dump($x);
    for ($i=1;$i<4;$i++)
        $w->send('test'.$i, 'text', 1);
    $w->send('end', 'text', 1);
    while($data = $w->recv()) {
        var_export($data);
    }
} else {
    echo "Could not connect to server".PHP_EOL;
}
  1. What did you expect to see?

When server-side pushes all the messages, the client connection should not dicsonnect.

  1. What did you see instead?

After all pushes are done from server-side, on client-side (function $this->socket->recv() ) issues a Warning with error-code 11, in addition to return value "false" and connection is closed. (Reactor Id, as echoed in function onClose() on server-side, is shown as 2)

Warning is as below:

PHP Warning: Swoole\Client::recv(): recv() failed, Error: Resource temporarily unavailable[11] in /var/www/html/swoole-prac/websocketclient/wbsocketclient.php on line 87"

And, it also gives a warning that there is no property $errMsg on socket object, as gist uses this property on socket object.

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

swoole

Swoole => enabled
Author => Swoole Team [email protected]
Version => 5.1.2
Built => May 26 2024 22:51:27
coroutine => enabled with thread context
debug => enabled
trace_log => enabled
epoll => enabled
eventfd => enabled
signalfd => enabled
cpu_affinity => enabled
spinlock => enabled
rwlock => enabled
sockets => enabled
openssl => OpenSSL 3.0.2 15 Mar 2022
dtls => enabled
http2 => enabled
json => enabled
curl-native => enabled
c-ares => 1.18.1
zlib => 1.2.11
mutex_timedlock => enabled
pthread_barrier => enabled
futex => enabled
mysqlnd => enabled
async_redis => enabled
coroutine_pgsql => enabled

Directive => Local Value => Master Value
swoole.enable_coroutine => On => On
swoole.enable_library => On => On
swoole.enable_fiber_mock => Off => Off
swoole.enable_preemptive_scheduler => On => On
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) ?

Linux HP-Laptop 5.17.5-76051705-generic #202204271406165150484020.04~63e51bd-Ubuntu SMP PREEMPT Wed Ma x86_64 x86_64 x86_64 GNU/Linux

PHP 8.3.8 (cli) (built: Jun 6 2024 16:58:27) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.8, Copyright (c) Zend Technologies
with Zend OPcache v8.3.8, Copyright (c), by Zend Technologies

COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/11/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 11.4.0-1ubuntu122.04' --with-bugurl=file:///usr/share/doc/gcc-11/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,m2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-11 --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-plugin --enable-default-pie --with-system-zlib --enable-libphobos-checking=release --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --enable-cet --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none=/build/gcc-11-XeT9lY/gcc-11-11.4.0/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/gcc-11-XeT9lY/gcc-11-11.4.0/debian/tmp-gcn/usr --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu --with-build-config=bootstrap-lto-lean --enable-link-serialization=2
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 11.4.0 (Ubuntu 11.4.0-1ubuntu1
22.04)

@yoyoyeahyoyo
Copy link

version 6.0.0-2 is not working with pdo_pgsql giving that same error: "Error: Resource temporarily unavailable".

@matyhtf
Copy link
Member

matyhtf commented Aug 26, 2024

PHP Warning:  Swoole\Client::recv(): recv() failed, Error: Resource temporarily unavailable[11] in /var/www/html/swoole-prac/websocketclient/wbsocketclient.php on line 87

Swoole\Client::recv() returned false with an errCode of EAGAIN indicates a receive timeout, not a closed connection. The server sends four times, and there should be four corresponding receives, not a looped receive.

If you want the client to never time out until the server sends a message, set the third parameter of Swoole\Client::connect() to -1. This way, it won't return false and the EAGAIN error.

@matyhtf matyhtf closed this as completed Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants