ext/pcntl (ZTS): a process-directed signal delivered to a non-PHP thread crashes in pcntl_signal_handler
維護者通常 1 天內回覆
還沒有人認領這個 Issue。
評估
研究方向
Start with ext/pcntl's pcntl_signal_handler() and compare its ZTS behavior with zend_signal_handler_defer(), which the issue identifies as checking tsrm_is_managed_thread(). The issue proposes forwarding signals from unmanaged threads to the thread that installed the handler and mentions a deterministic test using a raw pthread through a new zend_test helper. Done means the ZTS crash is prevented and that test covers signal delivery to an unmanaged thread.
由索引模型根據 Issue 內容生成。
描述
Description
On a ZTS build configured with --disable-zend-signals (as Homebrew's php-zts is), pcntl_signal_handler() is installed directly with sigaction() and reads PCNTL_G(...) without checking that it is running on a PHP thread. (With zend signals enabled, zend_signal_handler_defer() already checks tsrm_is_managed_thread() before anything reaches pcntl.) A process-directed signal (kill(), alarm()) may be delivered to any thread that does not block it. On macOS, every PHP process has libdispatch worker threads. When the signal lands on one of those threads, the handler dereferences that thread's (absent) TSRM globals.
<?php
// ZTS only: SIGALRM delivered to a non-PHP thread crashes in pcntl_signal_handler.
pcntl_async_signals(true);
pcntl_signal(SIGALRM, static function (): void {});
function weigh(string $s): int { return strlen($s) % 7; }
$pid = getmypid();
$killer = proc_open(['/bin/sh', '-c', "while kill -ALRM $pid 2>/dev/null; do :; done"], [], $pipes);
$end = hrtime(true) + 20_000_000_000;
while (hrtime(true) < $end) {
$rows = [];
for ($i = 0; $i < 200; $i++) { $rows[] = str_repeat('x', $i % 13); }
usort($rows, static fn (string $a, string $b): int => weigh($a) <=> weigh($b));
}
pcntl_signal(SIGALRM, SIG_IGN);
proc_terminate($killer, 9);
echo "survived\n";
Resulted in this output (exit 139, intermittent: about 1 to 2 runs in 6 under a 20 s storm):
* thread #2, stop reason = EXC_BAD_ACCESS (code=1, address=0x0)
frame #0: php`pcntl_signal_handler + 32
frame #1: libsystem_platform.dylib`_sigtramp + 56
thread #1: php`zend_array_destroy + 164, queue = 'com.apple.main-thread'
But I expected this output instead:
survived
In a larger application, the same race shows up as zend_mm_heap corrupted (exit 134) or as a hash lookup that never ends (zend_hash_find spinning). Both appear when a single pcntl_alarm() handler fires while the main thread is busy.
The NTS build of the same version never crashes (0 of 6 runs; also 0 of 6 on an NTS debug build), which is consistent with NTS globals being process-wide.
A storm makes it likely because pcntl_signal_dispatch() blocks every signal on the main thread while it runs handlers, so the kernel has to pick another thread for a process-directed signal that arrives in that window.
A fix along the lines of zend_signal_handler_defer(): under ZTS, when !tsrm_is_managed_thread(), don't touch PCNTL_G(), and forward the signal with pthread_kill() to the thread that installed the handler. I have a PR for this, with a deterministic test that raises the signal on a raw pthread through a new zend_test helper.
PHP Version
PHP 8.5.12 (cli) (ZTS), Homebrew php-zts (NTS 8.5.11 for comparison)
Operating System
macOS (arm64)
- 主要語言
- C
- 星號
- 41.8k
- 分支
- 8.6k
- 平均合併
- 2 天 15 小時
- 30 天內合併 PR
- 125
環境準備
- 沒有 Dockerfile 或 Docker Compose 檔案
- 沒有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
php/php-src 的其他 Issue
-
難度 2/5 1-3 小時 新手友好度 72/100
維護者通常 1 天內回覆
-
ext/openssl: 8.2.31+/8.3.31+ fail to build against OpenSSL 1.0.2 (ASN1_STRING_get0_data in xp_ssl.c)未關閉Extension: openssl Status: Needs Triage
難度 2/5 1-3 小時 新手友好度 79/100
維護者通常 1 天內回覆
-
Bug Status: Needs Triage
難度 2/5 1-3 小時 新手友好度 68/100
維護者通常 1 天內回覆
-
SNMP accepts malformed remote ports可能已有人在做 @somethingwithproof 於 21 天前認領。 未關閉
難度 2/5 半天 新手友好度 62/100
維護者通常 1 天內回覆
-
Bug Status: Needs Triage
難度 2/5 1-3 小時 新手友好度 64/100
維護者通常 1 天內回覆
相似的 Issue
-
bug
難度 2/5 1-3 小時 新手友好度 72/100
維護者通常 1 天內回覆
-
ZIP/ZIPS decode dereferences NULL when the cached libdeflate decompressor can't be allocated (3.5 regression)可能已有人在做 關聯的 PR 仍在進行中或已合併。 未關閉
難度 2/5 1-3 小時 新手友好度 74/100
AcademySoftwareFoundation/openexr#2884 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 82/100
python-pillow/Pillow#10590 ·
維護者通常 1 天內回覆
-
status: needs-triage type: bug
難度 2/5 1-3 小時 新手友好度 62/100
-
bug
難度 2/5 1-3 小時 新手友好度 62/100
openwrt/packages#31407 · 1 則留言 ·
維護者通常 1 天內回覆