Skip to content

fix: restore signal handlers (SIGHUP/SIGINT/SIGTERM) to prevent 100% CPU on terminal close#114

Open
crazywhalecc wants to merge 3 commits into
masterfrom
fix/signal-handlers-sighup-stdout-guard
Open

fix: restore signal handlers (SIGHUP/SIGINT/SIGTERM) to prevent 100% CPU on terminal close#114
crazywhalecc wants to merge 3 commits into
masterfrom
fix/signal-handlers-sighup-stdout-guard

Conversation

@crazywhalecc

Copy link
Copy Markdown
Member

问题

当从 IDE 终端启动框架后直接关闭 IDE(而不先停框架),终端 PTY 被销毁。由于 libonebot 的 Worker 类重写了 installSignal()reinstallSignal() 为空操作(所有信号处理被注释),Worker 进程无法响应 SIGHUP/SIGINT/SIGTERM 信号,变成孤儿进程继续运行。配合 STDOUT/STDERR 破损导致的日志递归错误,最终造成 CPU 100% 空耗。

根因

  • installSignal() 仅启用了 SIGUSR1,其余 SIGINT/SIGTERM/SIGHUP/SIGQUIT/SIGPIPE 等全部被注释
  • reinstallSignal() 完全为空函数,子进程 fork 后不注册任何信号处理
  • runAll()installSignal() 调用被注释

修复

  1. installSignal() — 恢复完整信号注册:SIGINT(Ctrl+C 停止)、SIGTERM(终止)、SIGHUP(终端关闭)、SIGQUIT(优雅重启)、SIGUSR2(状态)、SIGIO(连接状态)、SIGPIPE(忽略防崩溃)
  2. reinstallSignal() — 子进程 fork 后通过 EventInterface 正确重装所有信号处理器,支持 Select/Event/Ev 等全部后端
  3. runAll() — 启用 installSignal() 调用,Master 进程也能响应信号
  4. 添加 Workerman\Events\EventInterface import

关联

When the IDE terminal is closed without stopping the framework first,
the PTY is destroyed and workers become orphaned with broken
STDIN/STDOUT/STDERR. Without proper signal handlers (SIGHUP/SIGINT/
SIGTERM), workers cannot shut down gracefully.

Changes:
- installSignal(): uncomment all signal handlers (SIGINT, SIGTERM,
  SIGHUP, SIGQUIT, SIGUSR2, SIGIO, SIGPIPE). This is called from
  runAll() for the master process.
- reinstallSignal(): properly reinstall signal handlers via event
  loop after fork, replacing the previous no-op implementation.
  Workers now respond to SIGHUP (terminal close) by stopping
  gracefully via Workerman::signalHandler().
- runAll(): enable installSignal() call that was previously
  commented out.
- Add Workerman\Events\EventInterface import required by the
  reinstalled signal handlers.
- Add PHP 8.5 to version constraint
- Bump php-cs-fixer from ^3.2 to ^3.64 (compatible with PHP 8.4+)
- Bump phpstan from ^1.1 to ^1.12
- Bump swoole/ide-helper from ~4.4.0 to ~5.0
- Widen symfony/var-dumper constraint to ^5.3 || ^6.0 || ^7.0
- CI: bump coding-style PHP 8.0 → 8.3
- CI: bump static-analysis PHP 7.4 → 8.3
- CI: test matrix updated to 8.1, 8.2, 8.3, 8.4 (drop EOL 7.4/8.0)
- Regenerate composer.lock with updated deps
- actions/checkout@v2 → @v4 (Node.js 20 deprecated, v4 uses Node.js 24)
- fkirc/skip-duplicate-actions@v4 → @v5 (set-output deprecated)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant