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

Critical error when trying to build all php extensions using --build-micro #468

Closed
HallexCosta opened this issue May 31, 2024 · 12 comments
Closed
Labels
bug Something isn't working kind/php-and-sapi Issues related to php source and SAPI

Comments

@HallexCosta
Copy link

Version PHP: 8.1
Command used: bin/spc build --build-micro "apcu,bcmath,calendar,ctype,curl,dba,dom,exif,fileinfo,filter,gd,iconv,intl,mbregex,mbstring,mysqli,mysqlnd,opcache,openssl,pcntl,pdo,phar,posix,readline,redis,session,simplexml,sockets,sodium,sqlite3,swoole,swoole-hook-mysql,swoole-hook-pgsql,swoole-hook-sqlite,tokenizer,xml,xmlreader,xmlwriter,xsl,zip,zlib" --debug

Context description: I want to generate a binary file for a hyper application, but I am receiving this critical error that prevents me from using the command bin/spc micro:combine ../bin/hyperf.php

image

If I'm doing something wrong or does anyone know of an article or post explaining how I can generate a binary for a hyperf application, I'll be grateful :)

@jingjingxyk
Copy link
Contributor

  1. 推荐在容器里构建
    building in container
    https://static-php.dev/en/guide/manual-build.html#use-docker

  2. swoole need sapi_module.name=micro
    more info: https://github.com/swoole/swoole-src/pull/5340/files

@HallexCosta
Copy link
Author

  1. 推荐在容器里构建
    building in container
    https://static-php.dev/en/guide/manual-build.html#use-docker
  2. swoole need sapi_module.name=micro
    more info: https://github.com/swoole/swoole-src/pull/5340/files

@jingjingxyk Thanks for your answer!

Building with container and define sapi_module.name=micro work for me
image

although now receive this error when execute the binary from application:
image

try get around the problem replacing the Swoole\Constant use in config/autoload/server.php but it didn't work very well 😅

<?php

declare(strict_types=1);

use Hyperf\Server\Event;
use Hyperf\Server\Server;
use Swoole\Constant;

return [
    'mode' => SWOOLE_PROCESS,
    'servers' => [
        [
            'name' => 'http',
            'type' => Server::SERVER_HTTP,
            'host' => '0.0.0.0',
            'port' => 9501,
            'sock_type' => SWOOLE_SOCK_TCP,
            'callbacks' => [
                Event::ON_REQUEST => [Hyperf\HttpServer\Server::class, 'onRequest'],
            ],
            'options' => [
                // Whether to enable request lifecycle event
                'enable_request_lifecycle' => false,
            ],
        ],
    ],
    'settings' => [
        Constant::OPTION_ENABLE_COROUTINE => true,
        Constant::OPTION_WORKER_NUM => swoole_cpu_num(),
        Constant::OPTION_PID_FILE => BASE_PATH . '/runtime/hyperf.pid',
        Constant::OPTION_OPEN_TCP_NODELAY => true,
        Constant::OPTION_MAX_COROUTINE => 100000,
        Constant::OPTION_OPEN_HTTP2_PROTOCOL => true,
        Constant::OPTION_MAX_REQUEST => 100000,
        Constant::OPTION_SOCKET_BUFFER_SIZE => 2 * 1024 * 1024,
        Constant::OPTION_BUFFER_OUTPUT_SIZE => 2 * 1024 * 1024,
    ],
    'callbacks' => [
        Event::ON_WORKER_START => [Hyperf\Framework\Bootstrap\WorkerStartCallback::class, 'onWorkerStart'],
        Event::ON_PIPE_MESSAGE => [Hyperf\Framework\Bootstrap\PipeMessageCallback::class, 'onPipeMessage'],
        Event::ON_WORKER_EXIT => [Hyperf\Framework\Bootstrap\WorkerExitCallback::class, 'onWorkerExit'],
    ],
];

for this:

<?php
declare(strict_types=1);

use Hyperf\Server\Event;
use Hyperf\Server\Server;
use Swoole\Constant;

return [
    'mode' => SWOOLE_PROCESS,
    'servers' => [
        [
            'name' => 'http',
            'type' => Server::SERVER_HTTP,
            'host' => '0.0.0.0',
            'port' => 9501,
            'sock_type' => SWOOLE_SOCK_TCP,
            'callbacks' => [
                Event::ON_REQUEST => [Hyperf\HttpServer\Server::class, 'onRequest'],
            ],
            'options' => [
                // Whether to enable request lifecycle event
                'enable_request_lifecycle' => false,
            ],
        ],
    ],
    'settings' => [
        'enable_coroutine' => true,
        'worker_num' => swoole_cpu_num(),
        'pid_file' => BASE_PATH . '/runtime/hyperf.pid',
        'open_tcp_nodelay' => true,
        'max_coroutine' => 100000,
        'open_http2_protocol' => true,
        'max_request' => 100000,
        'socket_buffer_size' => 2 * 1024 * 1024,
        'buffer_output_size' => 2 * 1024 * 1024
    ],
    'callbacks' => [
        Event::ON_WORKER_START => [Hyperf\Framework\Bootstrap\WorkerStartCallback::class, 'onWorkerStart'],
        Event::ON_PIPE_MESSAGE => [Hyperf\Framework\Bootstrap\PipeMessageCallback::class, 'onPipeMessage'],
        Event::ON_WORKER_EXIT => [Hyperf\Framework\Bootstrap\WorkerExitCallback::class, 'onWorkerExit'],
    ],
];

image after replace Swoole\Constant
image

@jingjingxyk
Copy link
Contributor

jingjingxyk commented Jun 1, 2024

2. https://github.com/swoole/swoole-src/pull/5340/files

swoole v5.1.2 之前的版本,需要修改swoole 源码,手动添加sapi_module.name=micro ,详见: https://github.com/swoole/swoole-src/blob/16a9122aec63c3b8c80874ec547f7ac39ff58dcf/ext-src/php_swoole.cc#L224

Before version 5.1.2 of SWOOLE, it is necessary to modify the SWOOLE source code and manually add sapi_module. name=micro . info: https://github.com/swoole/swoole-src/blob/16a9122aec63c3b8c80874ec547f7ac39ff58dcf/ext-src/php_swoole.cc#L224

v5.1.2 版本
在 697 行添加如下代码
line number 697
add code example : https://github.com/swoole/swoole-src/pull/5340/files

|| strcmp("micro", sapi_module.name) == 0

v5.1.2:
https://github.com/swoole/swoole-src/blob/2dcfef90b3dbcfc08783747ea9abf6ebaf7eedb8/ext-src/php_swoole.cc#L697

@HallexCosta
Copy link
Author

HallexCosta commented Jun 1, 2024

  1. https://github.com/swoole/swoole-src/pull/5340/files

swoole v5.1.2 之前的版本,需要修改swoole 源码,手动添加sapi_module.name=micro ,详见: https://github.com/swoole/swoole-src/blob/16a9122aec63c3b8c80874ec547f7ac39ff58dcf/ext-src/php_swoole.cc#L224

Before version 5.1.2 of SWOOLE, it is necessary to modify the SWOOLE source code and manually add sapi_module. name=micro . info: https://github.com/swoole/swoole-src/blob/16a9122aec63c3b8c80874ec547f7ac39ff58dcf/ext-src/php_swoole.cc#L224

v5.1.2 版本 在 697 行添加如下代码 line number 697 add code example : https://github.com/swoole/swoole-src/pull/5340/files

|| strcmp("micro", sapi_module.name) == 0

v5.1.2: https://github.com/swoole/swoole-src/blob/2dcfef90b3dbcfc08783747ea9abf6ebaf7eedb8/ext-src/php_swoole.cc#L697

Hmm, now I understand, this change is made directly in the swoole source code... Thanks for explanation!

Now the server is working!
Although the worker die upon receiving a request from the browser. Apparently when I use an HTTP Client it works normally.

I found an open issue in the swoole-src repository with a similar problem.
swoole/swoole-src#5324

image

@crazywhalecc crazywhalecc added bug Something isn't working kind/php-and-sapi Issues related to php source and SAPI labels Jun 2, 2024
@crazywhalecc
Copy link
Owner

@HallexCosta

For your first build issue, building different combination of extensions before, you may need to delete buildroot and source dir first. Because some combination has alternative libs, and if you compile them first, and second time build without it, you will get linking error like yours.

Swoole currently does not support micro SAPI officially, you can modify swoole-src like @jingjingxyk said, or build with --with-micro-fake-cli, makes micro pretend to be cli.

I need to re-produce hyperf bug on my own computers. But you can try the latest phpmicro, the author of phpmicro has updated and fixed several bugs: bin/spc del-download micro && bin/spc download micro.

For every different build combination, remember rm -rf buildroot source.

@crazywhalecc crazywhalecc added question Further information is requested need response and removed bug Something isn't working labels Jun 4, 2024
@HallexCosta
Copy link
Author

HallexCosta commented Jun 5, 2024

@HallexCosta

For your first build issue, building different combination of extensions before, you may need to delete buildroot and source dir first. Because some combination has alternative libs, and if you compile them first, and second time build without it, you will get linking error like yours.

Swoole currently does not support micro SAPI officially, you can modify swoole-src like @jingjingxyk said, or build with --with-micro-fake-cli, makes micro pretend to be cli.

I need to re-produce hyperf bug on my own computers. But you can try the latest phpmicro, the author of phpmicro has updated and fixed several bugs: bin/spc del-download micro && bin/spc download micro.

For every different build combination, remember rm -rf buildroot source.

Sorry for the delay of the answer.

I tested both solutions and they both seem to have worked fine, I don't know what could have happened but delete buildroot and source, define less compilation extensions like "bcmath,calendar,ctype,curl,dba,dom,exif,filter,fileinfo,iconv,mbstring,mbregex,openssl,pcntl,pdo,swoole,phar,posix,readline,simplexml,sockets, sqlite3,tokenizer,xml,xmlreader,xmlwriter,zip,zlib,sodium", and rebuild using bin/spc build --build-micro --with-micro-fake-cli "$EXTENSIONS" or directly modify the swoole-src now worked 😁.

Now, I'm trying to compile my hyperf application into a .phar file and then build it into a binary file using spc, but I'm having new challenges when building hyperf app on .phar

@HallexCosta
Copy link
Author

HallexCosta commented Jun 7, 2024

Another problem I'm facing is when building a binary from a .phar file.
Using swoole-cli hyperf.phar start the app works normally, but when building it in static binary and running it the console prints some error messages

Command used for build is the same from above comment: set EXTENSIONS "bcmath,calendar,ctype,curl,dba,dom,exif,filter,fileinfo,iconv,mbstring,mbregex,openssl,pcntl,pdo,swoole,phar,posix,readline,simplexml,sockets,sqlite3,tokenizer,xml,xmlreader,xmlwriter,zip,zlib,sodium" && spc build --build-micro --with-micro-fake-cli "$EXTENSIONS"
Comand used to combine: spc micro:combine hyperf.phar -O bin/phar-static-binary -M ../../crazywhalecc/static-php-cli/buildroot/bin/micro.sfx

Message error when execute the binary generated using a .phar file
image

The same phar file running using swoole-cli
image

@crazywhalecc
Copy link
Owner

Using the same spc compilation mode, after adding --build-cli, can it be run using buildroot/bin/php your-hyperf.phar start?

@jingjingxyk
Copy link
Contributor

jingjingxyk commented Jun 7, 2024

swoole support micro on v5.1.3
https://github.com/swoole/swoole-src/releases/tag/v5.1.3
https://github.com/swoole/swoole-src/pull/5340/files

@HallexCosta
Copy link
Author

Using the same spc compilation mode, after adding --build-cli, can it be run using buildroot/bin/php your-hyperf.phar start?

Yes, with build --build-cli and running buildroot/bin/php my-hyperf.phar works
image

@crazywhalecc crazywhalecc added bug Something isn't working and removed need response labels Jun 10, 2024
@crazywhalecc
Copy link
Owner

crazywhalecc commented Jun 10, 2024

This problem is related to phpmicro itself. See #476 . Will fix phpmicro upstream

@crazywhalecc crazywhalecc removed the question Further information is requested label Jun 10, 2024
@crazywhalecc
Copy link
Owner

phpmicro has fixed phar issue, remember cleaning buildroot and source, and bin/spc del-download micro && bin/spc download micro to download the latest micro repo to build.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working kind/php-and-sapi Issues related to php source and SAPI
Projects
None yet
Development

No branches or pull requests

3 participants