swoole / swoole-src

🚀 Coroutine-based concurrency library for PHP
https://www.swoole.com
Apache License 2.0
18.37k stars 3.17k forks source link

Outgoing Connections blocked from within Swoole HTTP Server onRequest? #3198

Closed bramus closed 4 years ago

bramus commented 4 years ago

Please answer these questions before submitting your issue. Thanks!

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

    I have a \Swoole\HTTP\Server instance that handles HTTP requests.

    At a certain moment – inside onRequest – I am trying to store data into Google Cloud Firestore, using the google/cloud-firestore package. That package communicates to the Google servers over port 443., internally using the PHP gRPC extension.

    Here's the (slimmed down) code, which is no more than a basic \Swoole\HTTP\Server implementation:

    $server = new Swoole\HTTP\Server("0.0.0.0", 9501);
    $server->on('start', function (Swoole\Server $server){
        echo "Server Started with Manager PID {$server->manager_pid} and Master PID {$server->master_pid}" . PHP_EOL;
    });
    
    $server->on('request', function(Swoole\Http\Request $request, Swoole\Http\Response $response) {
        // Create FirestoreClient
        $firestoreclient = new \Google\Cloud\Firestore\FirestoreClient([
            'keyFilePath' => __DIR__ . '/../config/credentials.json'
        ]);
    
        // Define dummy name for document
        $name = microtime(true);
    
        // Create the document inside the collection "my-collection"
        $firestoreQuery = $firestoreclient->collection('my-collection');
        $document = $firestoreQuery->document($name);
    
        // Store its data
        $document->set([
            'foo' => 'bar',
        ]);
    
        // Output name of the document
        echo $name;
    });
    
    $server->start();   
  2. What did you expect to see?

    That, upon receiving a request through Swoole's $server, the Firestore Document were to be created + data would be set + the $name would be echod.

  3. What did you see instead?

    The code just hangs upon the $document->set call and does nothing anymore.

    When moving the whole Firestore logic to outside of $server->on('request', …) it works just fine. When moving the whole Firestore logic to inside $server->on('start', …) it works just fine.

    It's only in $server->on('request', …) that it does not work.

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

    swoole
    
    Swoole => enabled
    Author => Swoole Team <team@swoole.com>
    Version => 4.4.16
    Built => Feb 20 2020 13:55:08
    coroutine => enabled
    kqueue => enabled
    rwlock => enabled
    pcre => enabled
    zlib => 1.2.11
    brotli => E16777223/D16777223
    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 => 262144 => 262144
  5. What is your machine environment used (including version of kernel & php & gcc) ?

    I'm running macOS 10.15.3 with PHP 7.4.2

    Darwin XXX 19.3.0 Darwin Kernel Version 19.3.0: Thu Jan  9 20:58:23 PST 2020; root:xnu-6153.81.5~1/RELEASE_X86_64 x86_64
matyhtf commented 4 years ago

This package may not support coroutines. Please close the coroutines and use synchronous blocking to execute them.

$server = new Swoole\HTTP\Server("0.0.0.0", 9501);

$server->set(['enable_coroutine' => false']);

$server->on('start', function (Swoole\Server $server){
    echo "Server Started with Manager PID {$server->manager_pid} and Master PID {$server->master_pid}" . PHP_EOL;
});
gleimerm commented 4 years ago

Hi @bramus, did @matyhtf suggestion work for you? I'm having the same issue.

Thanks

brogier commented 3 years ago

Im having the same issue when connecting GRPC for AxonServer. Any ideas?

brogier commented 3 years ago

The thing is, if I use plain PHP without swoole server (same docker instance) its connects and I can push messages, but when I use it from a slimple swoole instance it keeps hanging on the connection. Hope this helps.

ganl commented 2 years ago

Im having the same issue when connecting GRPC for AxonServer. Any ideas?

Try this:

  1. update grpc to 1.34+
  2. add the following lines to php.ini file.
    
    grpc.enable_fork_support = 1

3. Disable opcahce
`opcache.enable_cli=0`
4.