openswoole / ext-openswoole

Programmatic server for PHP with async IO, coroutines and fibers
https://openswoole.com
Apache License 2.0
808 stars 51 forks source link

PGSQL Pool inside HTTP Server [BUG] #321

Closed matheusagnes closed 1 year ago

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

declare(strict_types=1);

require_once 'vendor/autoload.php';

use OpenSwoole\Core\Coroutine\Client\PostgresClientFactory; use OpenSwoole\Core\Coroutine\Client\PostgresConfig; use OpenSwoole\Core\Coroutine\Pool\ClientPool;

$ini = require 'configs/zapwebhook.php'; $ini['name'] = 'zapwebhook_teste';

$server = new OpenSwoole\HTTP\Server("0.0.0.0", 9090, SWOOLE_PROCESS, SWOOLE_SOCK_TCP | SWOOLE_SSL);

$server->set([

// Setup SSL files
'ssl_cert_file' => '/etc/letsencrypt/live/...../fullchain.pem',
'ssl_key_file' => '/etc/letsencrypt/live/....../privkey.pem',
'worker_num' => OpenSwoole\Util::getCPUNum() * 4,
'display_errors' => true,
// 'log_level' => 0,
// 'log_file' => '/opt/zapwebhook/all_logs_dev.log'

]);

co::run(function () use ($ini, $server){ $config = (new PostgresConfig()); $config->withPassword($ini['pass']); $config->withUsername($ini['user']); $config->withDbname($ini['name']); $config->withPort( (int) $ini['port']); $config->withHost($ini['host']);

$pool   = new ClientPool(PostgresClientFactory::class, $config, 100);
$pool->fill();

$server->madPool = $pool;

});

$server->on("start", function (OpenSwoole\Http\Server $server) {

});

$server->on("request", function (OpenSwoole\Http\Request $request, OpenSwoole\Http\Response $response) use ($server){

co::run(function () use ($response, $request, $server) {

    $requestContentJson = $request->getContent();

    $odontoQuality = false;

    if(!empty($request->get['odq']))
    {
        $odontoQuality = true;
    }

    if($requestContentJson)
    {
        $requestContent = json_decode($requestContentJson, true);

        if($requestContent)
        {   
            $types = ['MessageStatusCallback', 'ReceivedCallback', 'ConnectedCallback', 'DisconnectedCallback'];

            if(in_array($requestContent['type'], $types))
            {
                if(!empty($requestContent['messageId']))
                {
                    $conn = $server->madPool->get();
                    insertFila($requestContentJson, $requestContent['messageId'] ?? null, $conn, $odontoQuality);
                    $server->madPool->put($conn);   
                }
                elseif(!empty($requestContent['ids']))
                {
                    foreach($requestContent['ids'] as $id)
                    {
                        $conn = $server->madPool->get();
                        insertFila($requestContentJson, $id, $conn, $odontoQuality);
                        $server->madPool->put($conn);
                    }
                }
                else
                {
                    $conn = $server->madPool->get();
                    insertFila($requestContentJson, null, $conn, $odontoQuality);
                    $server->madPool->put($conn);
                }
            }
        }
    }

    $response->header("Content-Type", "text/plain");
    $response->end("Hello World\n");

});

});

function queueInsert($data, $id, $conn, $odontoQuality = false) { $system_code = 'axon'; if($odontoQuality) { $system_code = 'odq'; }

try {

    //$uniqid = uniqid();

    // $conn->prepare("insert_fila_{$uniqid}", 'INSERT INTO fila_webhook (payload, message_id, type, system_code) VALUES($1, $2, \'Z\', $3); ');
    // $conn->execute("insert_fila_{$uniqid}");

    // THE ERROR HAPPEN HERE...
    $p = $conn->query('SELECT 1');

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

}

$server->start(); //$scheduler->start();

2. What did you expect to see?

I'm trying to use a PGSQL Pool Inside a HTTP Server. When I try to query/execute any sql in PGSQL the bug happens.

3. What did you see instead?

[2023-07-28 15:05:07 $2358259.0] WARNING Server::check_worker_exit_status(): worker(pid=2358365, id=15) abnormal exit, status=0, signal=11 A bug occurred in OpenSwoole-v22.0.0, please report it. Please submit bug report at:

https://github.com/openswoole/swoole-src/issues

OS: Linux 5.15.0-78-generic #85-Ubuntu SMP Fri Jul 7 15:25:09 UTC 2023 x86_64 GCC_VERSION: 11.3.0 OPENSSL_VERSION: OpenSSL 3.0.2 15 Mar 2022 PHP_VERSION : 8.2.0

4. What version of OpenSwoole are you using (show your `php --ri openswoole`)?

[2023-07-28 15:05:07 $2358259.0] WARNING Server::check_worker_exit_status(): worker(pid=2358365, id=15) abnormal exit, status=0, signal=11 A bug occurred in OpenSwoole-v22.0.0, please report it. Please submit bug report at:

https://github.com/openswoole/swoole-src/issues

OS: Linux 5.15.0-78-generic #85-Ubuntu SMP Fri Jul 7 15:25:09 UTC 2023 x86_64 GCC_VERSION: 11.3.0 OPENSSL_VERSION: OpenSSL 3.0.2 15 Mar 2022 PHP_VERSION : 8.2.0

5. What is your machine environment used (show your `uname -a` & `php -v` & `gcc -v`) ?

root@zap-webhook:/opt/zapwebhook# uname -a Linux zap-webhook 5.15.0-78-generic #85-Ubuntu SMP Fri Jul 7 15:25:09 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux

root@zap-webhook:/opt/zapwebhook# php -v PHP 8.2.8 (cli) (built: Jul 8 2023 07:10:21) (NTS) Copyright (c) The PHP Group Zend Engine v4.2.8, Copyright (c) Zend Technologies with Zend OPcache v8.2.8, Copyright (c), by Zend Technologies

root@zap-webhook:/opt/zapwebhook# gcc -v Using built-in specs. 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.3.0-1ubuntu1~22.04.1' --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-aYxV0E/gcc-11-11.3.0/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/gcc-11-aYxV0E/gcc-11-11.3.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.3.0 (Ubuntu 11.3.0-1ubuntu1~22.04.1)



You can also try the following OpenSwoole support channels:

* [Documentation](https://openswoole.com/docs) - Documentation for Open Swoole
* [Slack](https://goo.gl/forms/wooTTDmhbu30x4qC3) - Slack channel of Open Swoole
* [Discord](https://discord.gg/5QC57RNPpw) - Discord server of Open Swoole
doubaokun commented 1 year ago

co::run should not be used here, because coroutine is already enabled in the request callbacks.