roadrunner-server / roadrunner

🤯 High-performance PHP application server, process manager written in Go and powered with plugins
https://docs.roadrunner.dev
MIT License
7.92k stars 411 forks source link

Unpack error for BotMan telegram bot #364

Closed veleton777 closed 4 years ago

veleton777 commented 4 years ago

`[2020-09-27 16:22:13] local.ERROR: unpack(): Type C: not enough input, need 1, have 0 {"exception":"[object] (ErrorException(code: 0): unpack(): Type C: not enough input, need 1, have 0 at /Users/veleton777/sites/php/roadrunner/laravel-roadrunner/vendor/spiral/goridge/src/StreamRelay.php:152) [stacktrace]

0 [internal function]: Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(2, 'unpack(): Type ...', '/Users/veleton7...', 152, Array)

1 /Users/veleton777/sites/php/roadrunner/laravel-roadrunner/vendor/spiral/goridge/src/StreamRelay.php(152): unpack('Cflags/Psize/Jr...', '')

2 /Users/veleton777/sites/php/roadrunner/laravel-roadrunner/vendor/spiral/goridge/src/StreamRelay.php(118): Spiral\Goridge\StreamRelay->fetchPrefix()

3 /Users/veleton777/sites/php/roadrunner/laravel-roadrunner/vendor/spiral/roadrunner/src/Worker.php(54): Spiral\Goridge\StreamRelay->receiveSync(NULL)

4 /Users/veleton777/sites/php/roadrunner/laravel-roadrunner/vendor/spiral/roadrunner/src/Worker.php(59): Spiral\RoadRunner\Worker->receive(Array)

5 /Users/veleton777/sites/php/roadrunner/laravel-roadrunner/vendor/spiral/roadrunner/src/HttpClient.php(39): Spiral\RoadRunner\Worker->receive(Array)

6 /Users/veleton777/sites/php/roadrunner/laravel-roadrunner/vendor/spiral/roadrunner/src/PSR7Client.php(74): Spiral\RoadRunner\HttpClient->acceptRequest()

7 /Users/veleton777/sites/php/roadrunner/laravel-roadrunner/vendor/spiral/roadrunner-laravel/src/Worker.php(61): Spiral\RoadRunner\PSR7Client->acceptRequest()

8 /Users/veleton777/sites/php/roadrunner/laravel-roadrunner/vendor/spiral/roadrunner-laravel/bin/rr-worker(77): Spiral\RoadRunnerLaravel\Worker->start(false)

9 {main}

"}`

get requests work, but post requests from telegrams do not What could be the reason for this error?

rustatian commented 4 years ago

@veleton777 Seems, that smt with your payload. Looks like the issue from PHP, @wolfy-j could you please take a look?

wolfy-j commented 4 years ago

Most likely something echoed to the STDOUT, check your Loggers or switch to Unix sockets.

veleton777 commented 4 years ago

2020-09-28 19:18:49 [52.0.253.201][-][-][error][Spiral\Goridge\Exceptions\PrefixException] Spiral\Goridge\Exceptions\PrefixException: unable to read prefix from socket: Undefined error: 0 in /Users/veleton777/sites/php/services-site/yii-bot/vendor/spiral/goridge/src/SocketRelay.php:305

rr config

env:
  YII_DEBUG: true
  YII_ENV: dev
  YII_ALIAS_WEBROOT: ./web/
  YII_ALIAS_WEB: 'http://127.0.0.1:8080/'

http:
  address: 0.0.0.0:8080

  workers:
    command:  "php roadrunner.php"
    # connection method (pipes, tcp://:9000, unix://socket.unix). default "pipes"
    relay:    "unix://socket.unix"

roadrunner.php

$worker = new \Spiral\RoadRunner\Worker(
    new \Spiral\Goridge\SocketRelay("socket.unix", null, \Spiral\Goridge\SocketRelay::SOCK_UNIX)
);

roadrunner.php

<?php

ini_set('display_errors', 'stderr');

ini_set('use_cookies', 'false');
ini_set('use_only_cookies', 'true');

defined('YII_DEBUG') or define('YII_DEBUG', \getenv('YII_DEBUG'));
defined('YII_ENV') or define('YII_ENV', \getenv('YII_ENV'));

// Load your vendor dependencies and Yii2 autoloader
require_once __DIR__ .  '/vendor/autoload.php';
require_once __DIR__ .  '/vendor/yiisoft/yii2/Yii.php';

$worker = new \Spiral\RoadRunner\Worker(
    new \Spiral\Goridge\SocketRelay("socket.unix", null, \Spiral\Goridge\SocketRelay::SOCK_UNIX)
);

$psr7 = new \Spiral\RoadRunner\PSR7Client($worker);

// Load your configuration file
$config = require_once __DIR__ . '/config/web.php';

$application = (new \yii\Psr7\web\Application($config));

// Handle each request in a loop
while ($request = $psr7->acceptRequest()) {
    try {
        $response = $application->handle($request);
        $psr7->respond($response);
    } catch (\Throwable $e) {
        // \yii\Psr7\web\ErrorHandler should handle any exceptions
        // however you should implement your custom error handler should anything slip past.
        $psr7->getWorker()->error((string)$e);
    }

    // Workers will steadily grow in memory with each request until PHP memory_limit is reached, resulting in a worker crash.
    // With RoadRunner, you can tell the worker to shutdown if it approaches 10% of the maximum memory limit, allowing you to achieve better uptime.
    if ($application->clean()) {
        $psr7->getWorker()->stop();
        return;
    }
}

now an error of a different type

rustatian commented 4 years ago

@veleton777 I can guess, that there is no data in the socket $prefixBody === null || $prefixLength !== 17, but, we definetely should make error messages more user friendly.

rustatian commented 4 years ago

@veleton777 Could you please check the data availability in the socket?

veleton777 commented 4 years ago

Как мне можно проверить наличие данных в сокете?

veleton777 commented 4 years ago

Через сервер workerman сокеты работают

rustatian commented 4 years ago

Как мне можно проверить наличие данных в сокете?

Не мог бы ты писать на английском, пожалуйста, так как данная проблема может возникнуть у тех, кто не говорит по русски.

  1. How could I check the data in the UNIX socket. I am not very familiar with PHP, but I found this function in PHP stdlib (I guess) link.
  2. Socket works as expected with workerman. Could you please provide the following:
    • Operating system type (Linux (Ubuntu, Arch, etc) or Windows).
    • PHP, spiral, and RR versions.
    • Steps to reproduce this issue (with a minimal sample of code). With commands which you executed. Thanks.
veleton777 commented 4 years ago

OS: mac os catalina php 7.4 laravel 7 roadrunner 1.83 using the library https://github.com/Hunternnm/laravel-roadrunner

veleton777 commented 4 years ago

psr-worker.php

<?php

use Hunternnm\LaravelRoadrunner\RoadrunnerLaravelBridge;
use Illuminate\Foundation\Application;
use Illuminate\Http\Request;
use Spiral\Goridge;
use Spiral\RoadRunner;
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
use Laminas\Diactoros\ResponseFactory;
use Laminas\Diactoros\ServerRequestFactory;
use Laminas\Diactoros\StreamFactory;
use Laminas\Diactoros\UploadedFileFactory;

ini_set('display_errors', 'stderr');
require 'vendor/autoload.php';

$worker = new RoadRunner\Worker(
    new Goridge\SocketRelay("rr.sock", null, Goridge\SocketRelay::SOCK_UNIX)
);

$psr7 = new RoadRunner\PSR7Client($worker);
$httpFoundationFactory = new HttpFoundationFactory();

/** @var Application $app */
$app = require __DIR__ . '/bootstrap/app.php';

$rr = new RoadrunnerLaravelBridge($app, __DIR__);

$psr7factory = new PsrHttpFactory(
    new ServerRequestFactory(),
    new StreamFactory(),
    new UploadedFileFactory(),
    new ResponseFactory()
);

while ($req = $psr7->acceptRequest()) {
    try {
        $symfonyRequest = $httpFoundationFactory->createRequest($req);
        $request = Request::createFromBase($symfonyRequest);

        $response = $rr->request($request);

        $psr7response = $psr7factory->createResponse($response);
        $psr7->respond($psr7response);
    } catch (Throwable $e) {
        $psr7->getWorker()->error((string)$e);
    }
}

.rr.yaml

http:
  address: 0.0.0.0:8080
  workers:
    command: "php psr-worker.php"
    relay:   "unix://rr.sock"
    pool:
      numWorkers: 4
      destroyTimeout: 3
      maxJobs:  0
rpc:
  enable: true
  listen: tcp://:6001
veleton777 commented 4 years ago
[2020-10-01 19:57:16] local.ERROR: unable to read prefix from socket: Undefined error: 0 {"exception":"[object] (Spiral\\Goridge\\Exceptions\\PrefixException(code: 0): unable to read prefix from socket: Undefined error: 0 at /Users/veleton777/sites/php/swoole/laravel-swoole/vendor/spiral/goridge/src/SocketRelay.php:305)
[stacktrace]
#0 /Users/veleton777/sites/php/swoole/laravel-swoole/vendor/spiral/goridge/src/SocketRelay.php(184): Spiral\\Goridge\\SocketRelay->fetchPrefix()
#1 /Users/veleton777/sites/php/swoole/laravel-swoole/vendor/spiral/roadrunner/src/Worker.php(54): Spiral\\Goridge\\SocketRelay->receiveSync(NULL)
#2 /Users/veleton777/sites/php/swoole/laravel-swoole/vendor/spiral/roadrunner/src/Worker.php(59): Spiral\\RoadRunner\\Worker->receive(Array)
#3 /Users/veleton777/sites/php/swoole/laravel-swoole/vendor/spiral/roadrunner/src/HttpClient.php(39): Spiral\\RoadRunner\\Worker->receive(Array)
#4 /Users/veleton777/sites/php/swoole/laravel-swoole/vendor/spiral/roadrunner/src/PSR7Client.php(74): Spiral\\RoadRunner\\HttpClient->acceptRequest()
#5 /Users/veleton777/sites/php/swoole/laravel-swoole/psr-worker.php(43): Spiral\\RoadRunner\\PSR7Client->acceptRequest()
#6 {main}
"} 

this error appears when running roadrunner

./rr serve

veleton777 commented 4 years ago

on ubuntu the same error

rustatian commented 4 years ago

Ok, thanks a lot. @Hunternnm, could you please take a look?

veleton777 commented 4 years ago

I solved the error, the Viber library code contained the file_get_contents ('php://input'); I replaced it with fetching via laravel requests