View Issue Details

IDProjectCategoryView StatusLast Update
0002444XdebugCode Coveragepublic2026-11-19 02:31
Reporteryezefeng123 Assigned To 
PrioritynormalSeveritycrashReproducibilityalways
Status newResolutionopen 
Summary0002444: Swoole 6.1.13 / 6.2.6 + Xdebug 3.5.3 SIGSEGV(signal 11)
Description

跟进 #6428:Swoole 6.1.13 / 6.2.6 + Xdebug 3.5.3 仍然 SIGSEGV(signal 11)

跟进 https://github.com/swoole/swoole-src/issues/6428

#6428 报的是 Swoole 5.1.4。维护者回复「更新到 6.1、6.2 版本,已兼容 xdebug」,并关闭了 issue。同一环境继续测了 6.1 最新稳定版 6.1.13 和 6.2 最新稳定版 6.2.6(PECL 上 6.3.0 仍是 RC1,没有测)。swoole.enable_fiber_mock=On 已经打开。两个版本在第一个 HTTP 请求上仍然是 signal 11,gdb 落点相同。

结论

崩溃不是 Linux 内核问题,也不是 boost fcontext 把 C 栈写坏。

Xdebug 3.5.3 的 xdebug_execute_user_code_end() 在 observer fcall_end 里取函数栈顶。当时 Xdebug 的 per-fiber 栈 count == 0,XDEBUG_VECTOR_TAIL() 返回 NULL,下一行读 fse->code_coverage_init 空指针解引用,SIGSEGV。

这条 observer 回调发生在 Swoole 协程里:Swoole\Coroutine\Http\Server 的 onAccept(内部函数)调用用户回调,用户回调里 Swoole\Coroutine::create() 再开一个协程。enable_fiber_mock 会把这次切换通知给 Xdebug,但通知发生时,外层用户帧已经压在另一条 Xdebug 栈上。切回来之后,当前栈是空的,用户函数返回就崩。

6.1.13 和 6.2.6 的调用链一致,只是行号不同。

环境

项 值
宿主机 Apple Silicon,Docker Desktop
容器内核 Linux 7.0.14-linuxkit #1 SMP PREEMPT Fri Oct 30 13:45:20 UTC 2026 aarch64
发行版 Debian GNU/Linux 13 (trixie)
PHP 8.2.35 NTS,官方 php:8.2.35-fpm
GCC 14.2.0
Xdebug 3.5.3,mode=debug
Swoole 6.1.13(镜像内 pecl,Built Nov 18 2026 07:53:28),以及容器内源码编译的 6.2.6(Built Nov 18 2026 08:26:46)
协程上下文 boost asm context(make_arm64_aapcs_elf_gas.S)
OpenSSL 6.2.6 的 php --ri swoole 显示 OpenSSL 3.5.7 9 Jun 2026,http2 / brotli / dtls 开启

Zend 模块:Xdebug、Zend OPcache。

php.ini

swoole.enable_fiber_mock = On

zend_extension=xdebug
xdebug.mode=debug
xdebug.start_with_request=yes
xdebug.client_host=192.168.65.254
xdebug.client_port=9003
xdebug.idekey=PHPSTORM
xdebug.connect_timeout_ms=2000

6.2 已经没有 swoole.enable_coroutine 这个 ini。php --ri swoole 里和 fiber 相关的是:

swoole.enable_fiber_mock => On
coroutine => enabled with boost asm context

6.2.6 的 configure(与当时 6.1.13 的链接库对齐,brotli 开,其余 pecl 选项保持默认 no):

./configure --with-php-config=/usr/local/bin/php-config \
  --enable-sockets=no --with-openssl-dir=no --enable-mysqlnd=no \
  --enable-swoole-curl=no --enable-cares=no --enable-brotli=yes \
  --with-brotli-dir=no --enable-zstd=no --enable-swoole-pgsql=no \
  --with-swoole-odbc=no --with-swoole-oracle=no --enable-swoole-sqlite=no \
  --with-swoole-firebird=no --enable-swoole-thread=no --enable-iouring=no \
  --with-liburing-dir=no --enable-uring-socket=no --with-swoole-ssh2=no \
  --enable-swoole-ftp=no

php --ri 仍显示 openssl / http2 / dtls / brotli 为 enabled。

最小复现(6.2.6 上已跑通)

不需要 ThinkPHP。Xdebug 模式用环境变量打开,避免 ini 里的 XDEBUG_MODE 把模式关掉。IDE 不需要真的连上:client_port=9,连接失败即可。

<?php
use Swoole\Coroutine;
use Swoole\Coroutine\Http\Server;
use Swoole\Http\Request;
use Swoole\Http\Response;

class App {
    public function onRequest(Response $res): void
    {
        Coroutine::create(function () use ($res) {
            Coroutine::sleep(0.05);
            $res->end("ok");
        });
    }
}

Coroutine\run(function () {
    $app = new App();
    $server = new Server("127.0.0.1", 19502, false, true);
    $server->handle("/", function (Request $req, Response $res) use ($app) {
        $app->onRequest($res);
    });
    $server->start();
});
XDEBUG_MODE=debug php \
  -d xdebug.start_with_request=yes \
  -d xdebug.client_host=127.0.0.1 \
  -d xdebug.client_port=9 \
  -d xdebug.connect_timeout_ms=200 \
  repro.php

curl -v http://127.0.0.1:19502/

结果:curl: (52) Empty reply from server。PHP 进程直接死掉,没有 Swoole 的 “A process crash occurred” 文本(单进程,不是 ProcessPool worker)。core 的信号是 SIGSEGV,栈和下面业务进程一致,正在返回的函数就是 onRequest,Xdebug 栈 count=0。

对照:同样的 Server,handle 闭包里直接 Coroutine::create() 并立刻 $res->end("ok"),且 xdebug.start_with_request=no,请求返回 HTTP 200 / body ok,不崩。也就是说,只加载 Xdebug、或者子协程不让出,不一定崩;调试器启动(start_with_request=yes)加上「用户方法里 Coroutine::create(),子协程里 Coroutine::sleep()」会崩。

业务侧同样的路径

ThinkPHP topthink/think-swoole 的 HTTP worker 就是这个结构。每个 worker 跑 Swoole\Coroutine\Http\Server,handle 闭包再调用 onRequest(),onRequest() 里 Coroutine::create():

// vendor/topthink/think-swoole/src/concerns/InteractsWithHttp.php
$server = new Server($host, $port, false, true);
$server->handle('/', function (Request $req, Response $res) {
    if ($this->wsEnable && $this->isWebsocketRequest($req)) {
        $this->onHandShake($req, $res);
    } else {
        $this->onRequest($req, $res);   // 第 51 行,opcode ZEND_DO_FCALL
    }
});

public function onRequest($req, $res)
{
    Coroutine::create(function () use ($req, $res) {
        $this->runInSandbox(function (...) use ($req, $res) {
            // 真正的 HTTP 处理
        });
    });
}   // 崩溃时 opline 在第 141 行,函数正在返回

worker 数是 swoole_cpu_num()。请求:

curl http://127.0.0.1:9501/adminapi/setting/config/getAdminBase

不加载 Xdebug 时,这条请求正常返回业务的 HTTP 401(未带 JWT)。加载 Xdebug 后连接被重置。

6.1.13

[2026-11-18 08:04:04 #7.0]  WARNING  Worker::report_error(): worker(pid=14, id=6) abnormal exit, status=0, signal=11
A process crash occurred in Swoole-v6.1.13. Please report this issue.

OS: Linux 7.0.14-linuxkit #1 SMP PREEMPT Fri Oct 30 13:45:20 UTC 2026 aarch64
GCC_VERSION: 14.2.0
PHP_VERSION : 8.2.35

curl:Empty reply from server。core:/buffhub/public/core,133MB,swoole: http server #6。

6.2.6

替换 swoole.so 并重启 worker 后再打同一条请求:

[2026-11-18 08:27:40 #52333.0]  WARNING  Worker::report_error(): worker(pid=52340, id=6) abnormal exit, status=0, signal=11
A process crash occurred in Swoole-v6.2.6. Please report this issue.

OS: Linux 7.0.14-linuxkit #1 SMP PREEMPT Fri Oct 30 13:45:20 UTC 2026 aarch64
GCC_VERSION: 14.2.0
OPENSSL_VERSION: OpenSSL 3.5.7 9 Jun 2026
PHP_VERSION : 8.2.35

[2026-11-18 08:27:41 #52333.0]  WARNING  Worker::report_error(): worker(pid=52341, id=7) abnormal exit, status=0, signal=11

连续两次请求打挂两个 worker。core 135MB。

gdb

两份 core 都用 gdb 16.3 看过。扩展带 debug info,未 strip。PHP 本身是 NTS non-debug。

6.2.6(业务 worker,以及上面的最小脚本,栈相同)

Program terminated with signal SIGSEGV, Segmentation fault.
#0  xdebug_execute_user_code_end (execute_data=0xffff9ff78240, retval=0xaaaad6497118)
      at xdebug/src/base/base.c:811
#1  xdebug_execute_end                                      base.c:1053
#2  zend_observer_fcall_end
#3  execute_ex
#6  zend_call_function
#7  sw_zend_call_function_ex                                php_swoole_private.h:915
#8  zend::function::call                                    php_swoole_cxx.cc:111
      enable_coroutine=false, argc=2
#10 zim_swoole_http_server_coro_onAccept                    swoole_http_server_coro.cc:701
#12 swoole::PHPCoroutine::main_func                         swoole_coroutine.cc:796
#14 swoole::coroutine::Context::context_func                src/coroutine/context.cc:149
#15 swoole_make_fcontext                                    thirdparty/boost/asm/make_arm64_aapcs_elf_gas.S:94
Backtrace stopped: previous frame identical to this frame (corrupt stack?)

#15 那句 corrupt stack? 是 gdb 无法展开 boost fcontext 的汇编帧。崩溃指令的 sp 是正常的,见下面寄存器。不是栈被写坏。

6.1.13(同一请求)

行号不同,函数相同:

#0  xdebug_execute_user_code_end                            base.c:811
#7  sw_zend_call_function_ex                                php_swoole_private.h:909
#8  zend::function::call                                    php_swoole_cxx.cc:111
#10 zim_swoole_http_server_coro_onAccept                    swoole_http_server_coro.cc:703
#12 swoole::PHPCoroutine::main_func                         swoole_coroutine.cc:778
#15 swoole_make_fcontext                                    make_arm64_aapcs_elf_gas.S:94

5.1.4(#6428 里那份 core)也是这条链:onAccept → PHPCoroutine::main_func → swoole_make_fcontext,死在 xdebug_execute_user_code_end。

正在返回的 PHP 帧(6.1.13 与 6.2.6 一致)

帧 函数 位置
当前 onRequest InteractsWithHttp.php:141
prev think\swoole\concerns\{closure} 同文件第 51 行,opcode ZEND_DO_FCALL(60)
prev2 内部函数 onAccept 无 filename

最小脚本的 core 里,当前函数名同样是 onRequest。

出错指令

xdebug_execute_user_code_end+76:

ldrb w0, [x19, #95]     ; x19 == 0

6.2.6 业务 core 和最小脚本 core 都是:

x19 = 0
XG_BASE(stack) count = 0

6.1.13 业务 core 额外看过 vector 内容:

vector = 0xaaaacfdaea80
count  = 0
element size = 240
data   = 0xaaaacfba2fa0

x19 是 function_stack_entry *fse。反汇编对应 vector.h 的 XDEBUG_VECTOR_TAIL:count == 0 时走 mov x19, #0。随后的 ldrb 是在读 fse->code_coverage_init(该字段在结构体偏移 95)。故障地址是 0x5f,不是一个被破坏的堆指针。

gdb 把这条指令标在 base.c:811,是编译器把 809–811 行收成了一条空指针加载。源码如下(Xdebug 3.5.3 src/base/base.c):

static void xdebug_execute_user_code_end(zend_execute_data *execute_data, zval *retval)
{
    zend_op_array *op_array = &(execute_data->func->op_array);
    function_stack_entry *fse;

    fse = XDEBUG_VECTOR_TAIL(XG_BASE(stack));   /* count == 0 时为 NULL */

    if (XDEBUG_MODE_IS(XDEBUG_MODE_PROFILING)) {
        xdebug_profiler_execute_ex_end(fse);
    }

    if (fse->code_coverage_init) {              /* 这里 SIGSEGV */
        xdebug_coverage_execute_ex_end(fse, op_array, ...);
    }
    ...
}

调用方 xdebug_execute_end() 只判断了栈对象非空,没有判断栈深度:

static void xdebug_execute_end(zend_execute_data *execute_data, zval *retval)
{
    if (!XG_BASE(stack)) {
        return;
    }
    if (should_run_user_handler(execute_data)) {
        xdebug_execute_user_code_end(execute_data, retval);
    }
}

onRequest 是普通用户方法,should_run_user_handler() 为 true,所以一定会走进去。fcall_begin 时压进去的那一帧不在这条 count == 0 的栈上。

Swoole 6.2.6 里和这次栈切换对应的代码

ext-src/swoole_coroutine.cc。

协程入口只有在入口函数是用户函数时才通知 fiber observer。Coroutine\Http\Server 的 accept 协程入口是内部函数 onAccept(zim_swoole_http_server_coro_onAccept),这个分支不会走:

void PHPCoroutine::main_func(void *_args) {
    ...
    if (UNEXPECTED(ctx->fiber_context && ctx->fci_cache.function_handler->type == ZEND_USER_FUNCTION)) {
        zend_execute_data *tmp = EG(current_execute_data);
        zend_execute_data call = {};
        EG(current_execute_data) = &call;
        EG(current_execute_data)->opline = ctx->fci_cache.function_handler->op_array.opcodes;
        call.func = ctx->fci_cache.function_handler;
        fiber_context_switch_try_notify(get_origin_context(ctx), ctx);
        EG(current_execute_data) = tmp;
    }

    zend_call_function(&ctx->fci, &ctx->fci_cache);   /* 796 行,崩溃栈在这之下 */
    ...
}

gdb 里 onAccept 调用用户回调时 enable_coroutine=false。也就是说 handle 闭包和 onRequest() 跑在 accept 协程里面,不是再包一层协程。Xdebug 的 fcall_begin 把 onRequest 压进当时的 XG_BASE(stack)。

随后 onRequest() 里的 Coroutine::create() 才会进入一个用户函数协程。那个协程的 main_func 会调用 fiber_context_switch_try_notify()。第一次通知时,目标 fiber 的 status 被强制改成 INIT:

void PHPCoroutine::fiber_context_switch_notify(const PHPContext *from, PHPContext *to) {
    zend_fiber_context *from_context = from->fiber_context;
    zend_fiber_context *to_context = to->fiber_context;

    from_context->status = fiber_get_status(from);
    to_context->status = fiber_get_status(to);

    if (!to->fiber_init_notified) {
        to_context->status = ZEND_FIBER_STATUS_INIT;
        zend_observer_fiber_switch_notify(from_context, to_context);
        to_context->status = fiber_get_status(to);
        to->fiber_init_notified = true;
    } else {
        zend_observer_fiber_switch_notify(from_context, to_context);
    }
}

Xdebug 3.5.3 xdebug_fiber_switch_observer() 看到 to->status == ZEND_FIBER_STATUS_INIT 就新建一条空的函数栈,替换 XG_BASE(stack),再压一个伪造的 fiber 帧:

if (to->status == ZEND_FIBER_STATUS_INIT) {
    current_stack = create_stack_for_fiber(to_key, to);
} else {
    current_stack = find_stack_for_fiber(to_key, to);
}
XG_BASE(stack) = current_stack;

if (to->status == ZEND_FIBER_STATUS_INIT) {
    add_fiber_main(to_key, to);
}

它不会把切走之前那条栈(上面有 onRequest 的帧)保存回 from 的 fiber。accept 协程因为入口是内部函数,之前也没有作为 to 被登记过。子协程 sleep 让出再切回来时,当前 Xdebug 栈已经不是 fcall_begin 用的那条。onRequest 返回时 count == 0,fcall_end 空指针。

6.1.13 的 fiber_context_switch_notify 反汇编是同一段逻辑:第一次切入时 str wzr, [fiber+40] 把 status 写成 0,再 bl zend_observer_fiber_switch_notify。

期望

swoole.enable_fiber_mock=On 时,上面的最小脚本和 ThinkPHP 的 Coroutine\Http\Server 请求都不要 SIGSEGV。Xdebug 可以连上调试器,也可以只是 start_with_request=yes 但客户端连不上。

不加载 Xdebug 时,同一条业务请求返回 HTTP 401,进程不退出。

实际

版本 enable_fiber_mock 结果
5.1.4(#6428) On signal 11,xdebug_execute_user_code_end
6.1.13 On 同样
6.2.6 On 同样,最小脚本也能复现

以下是经过修复的补丁
xdebug-3.5.3-empty-stack.patch
--- a/src/base/base.c
+++ b/src/base/base.c
@@ -803,6 +803,11 @@
function_stack_entry *fse;

fse = XDEBUG_VECTOR_TAIL(XG_BASE(stack));
  • /* TAIL is NULL when the fiber stack has no frames. A coroutine library can
    • deliver fcall_end against that stack; dereferencing fse is a SIGSEGV. */
  • if (!fse) {
  • return;
  • }

    if (XDEBUG_MODE_IS(XDEBUG_MODE_PROFILING)) {
    xdebug_profiler_execute_ex_end(fse);
    @@ -976,6 +981,9 @@
    /* Re-acquire the tail as nested calls through

    • xdebug_old_execute_internal() might have reallocated the vector */
      fse = XDEBUG_VECTOR_TAIL(XG_BASE(stack));
  • if (!fse) {
  • return;
  • }

    if (XDEBUG_MODE_IS(XDEBUG_MODE_PROFILING)) {
    xdebug_profiler_execute_internal_end(fse);
    @@ -1150,7 +1158,13 @@
    tmp->function.object_class = NULL;
    tmp->function.scope_class = NULL;
    tmp->function.function = zend_string_copy(fiber_key);

  • tmp->filename = zend_string_copy(zend_get_executed_filename_ex());
  • /* Internal coroutine entries have no user frame, so the filename is NULL.
    • zend_string_copy() would dereference it. The stack-entry dtor already
    • allows a NULL filename. */
  • {
  • zend_string *filename = zend_get_executed_filename_ex();
  • tmp->filename = filename ? zend_string_copy(filename) : NULL;
  • }
    tmp->lineno = zend_get_executed_lineno();

    tmp->prev_memory = XG_BASE(prev_memory);

TagsNo tags attached.
Operating System
PHP Version8.2.20-8.2.29

Activities

There are no notes attached to this issue.