2025.06.04 | vance | 197次围观
背景: php 升级从 7.2.11 到 7.4.21
问题1 、 swoole 4.3.1 版本不变时,docker build 镜像编译失败。
Dockerfile
FROM php:7.4.21-cli
RUN pecl install swoole-4.3.5 && docker-php-ext-enable swoole
报错信息
/tmp/pear/temp/swoole/swoole_runtime.cc:66:1: error: invalid conversion from 'size_t (*)(php_stream*, const char*, size_t)' {aka 'long unsigned int (*)(_php_stream*, const char*, long unsigned int)'} to 'ssize_t (*)(php_stream*, const char*, size_t)' {aka 'long int (*)(_php_stream*, const char*, long unsigned int)'} [-fpermissive]
};
^
/tmp/pear/temp/swoole/swoole_runtime.cc:66:1: error: invalid conversion from 'size_t (*)(php_stream*, char*, size_t)' {aka 'long unsigned int (*)(_php_stream*, char*, long unsigned int)'} to 'ssize_t (*)(php_stream*, char*, size_t)' {aka 'long int (*)(_php_stream*, char*, long unsigned int)'} [-fpermissive]
In file included from /tmp/pear/temp/swoole/swoole_runtime.cc:116:
/tmp/pear/temp/swoole/thirdparty/plain_wrapper.c:189:1: error: invalid conversion from 'size_t (*)(php_stream*, const char*, size_t)' {aka 'long unsigned int (*)(_php_stream*, const char*, long unsigned int)'} to 'ssize_t (*)(php_stream*, const char*, size_t)' {aka 'long int (*)(_php_stream*, const char*, long unsigned int)'} [-fpermissive]
};
^
/tmp/pear/temp/swoole/thirdparty/plain_wrapper.c:189:1: error: invalid conversion from 'size_t (*)(php_stream*, char*, size_t)' {aka 'long unsigned int (*)(_php_stream*, char*, long unsigned int)'} to 'ssize_t (*)(php_stream*, char*, size_t)' {aka 'long int (*)(_php_stream*, char*, long unsigned int)'} [-fpermissive]
/tmp/pear/temp/swoole/thirdparty/plain_wrapper.c:823:1: error: invalid conversion from 'size_t (*)(php_stream*, char*, size_t)' {aka 'long unsigned int (*)(_php_stream*, char*, long unsigned int)'} to 'ssize_t (*)(php_stream*, char*, size_t)' {aka 'long int (*)(_php_stream*, char*, long unsigned int)'} [-fpermissive]
};
^
make: *** [Makefile:396: swoole_runtime.lo] Error 1
ERROR: `make' failed
原因:
PHP 在版本迭代过程中,其内部 API(包括 stream 相关的 API)可能会发生一些细微的调整和类型签名的变化。PHP 7.4 相对于 Swoole 4.3.5 发布时的 PHP 版本(主要是 PHP 7.1, 7.2, 7.3)可能在这些方面有所更新。Swoole 4.3.5 的代码是针对较早 PHP 7.x 版本的 API 编写的,当它尝试在 PHP 7.4 环境下编译时,编译器发现了这些类型不匹配,因此报错。
解决方案:
你有以下几个选择:
升级 Swoole 版本 (推荐):
这是最推荐的解决方案。Swoole 的后续版本(如 Swoole 4.4.x, 4.5.x, 4.8.x 或更新的 5.x)已经对 PHP 7.4 提供了更好的兼容性。
你可以尝试安装一个较新的、明确支持 PHP 7.4 的 Swoole 版本。例如,Swoole 4.4.16 或更高版本通常与 PHP 7.4 兼容良好。
修改你的 Dockerfile 指令:
Dockerfile
- 尝试一个较新的 Swoole 版本,例如 4.8.x (一个长期支持版本)
RUN pecl install swoole-4.8.13 && docker-php-ext-enable swoole - 或者尝试更接近你需求的版本,比如 4.4 系列的较新版
RUN pecl install swoole-4.4.26 && docker-php-ext-enable swoole
你可以在 PECL Swoole 页面 查看所有可用版本及其发布日期,通常较新的版本会有更好的 PHP 版本兼容性。
问题2、4.3.1 升级到 4.4.26 后, docker 容器主进程 process.php 启动异常
升级 swoole 4.3.* 到 4.4.* 区别
向后不兼容的更改 (Backward Incompatible Changes):
- PHP 版本要求:Swoole 4.4 不再支持 PHP 7.0。你需要使用 PHP 7.1 或更高版本。
- Runtime::enableCoroutine 行为变更:一旦启用了协程运行时 (Runtime::enableCoroutine),所有的阻塞操作都必须在协程内调用。这与之前的版本行为有所不同,需要特别注意。
- Coroutine\MySQL 客户端驱动:引入了新的 Coroutine\MySQL 客户端驱动,底层设计更加规范,但可能存在一些轻微的向下不兼容的变更。
问题现象:
启动容器php process.php
为主进程,process.php 内部启动了一个新的 子进程 swoole_process处理业务逻辑。
swoole 是 4.3.1 版本时。
主进程正常驻守运行,升级到 4.4.26后,发现主进程无法驻守进程,容器启动后,马上就变成 Exit的状态。
在process.php 末尾加上 swoole_process::wait();
可正常驻守运行。
发表评论