Thread reboots leak static TLS when OPcache is loaded but disabled (opcache.enable=0): extensions stop loading
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 45/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Khá rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Lĩnh vực
- backend, infrastructure
Hướng nghiên cứu
Start by tracing rebootAllThreads() and frankenphp_new_main_thread, then compare PHP module startup and shutdown when OPcache is disabled versus enabled. The report provides a Docker reproduction using intl and worker restarts; reproduce the static TLS allocation failure and verify whether the change prevents extensions from disappearing after repeated reboots.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
What happened?
Prepared with an AI assistant (Claude), which ran the investigation and the reproduction below on my machine; I asked for this report.
With OPcache loaded but disabled (opcache.enable=0), every reboot of FrankenPHP's PHP threads (a worker restart through the admin API, or a file change with watch) leaks static TLS. After enough reboots, extensions can no longer be loaded at the next PHP startup:
PHP Warning: PHP Startup: Unable to load dynamic library 'intl' (tried: …/intl (…: cannot open shared object file: No such file or directory), …/no-debug-zts-20240924/intl.so (…/intl.so: cannot allocate memory in static TLS block)) in Unknown on line 0
From then on the extension is missing in every request until the process is restarted (Class "Locale" not found, …). In our app (about 30 extensions, watch in development) this happened after about 95 saved files; with imagick the victim was libgomp.so.1: cannot allocate memory in static TLS block.
Minimal reproduction (official image, only intl)
Dockerfile:
FROM dunglas/frankenphp:1.13.1-php8.4-trixie
RUN install-php-extensions intl
Caddyfile:
{
frankenphp {
worker /app/public/worker.php 2
php_ini opcache.enable 0
}
}
:80 {
root * /app/public
php_server
}
public/worker.php:
<?php
while (frankenphp_handle_request(static function (): void {
echo 'worker';
})) {
gc_collect_cycles();
}
public/check.php:
<?php
echo extension_loaded('intl') ? 'intl loaded' : 'intl MISSING';
docker build -t fp-tls .
docker run -d --name fp-tls -v "$PWD/Caddyfile:/etc/frankenphp/Caddyfile:ro" -v "$PWD/public:/app/public:ro" fp-tls
for i in $(seq 1 1000); do
docker exec fp-tls curl -s -X POST localhost:2019/frankenphp/workers/restart >/dev/null
docker logs fp-tls 2>&1 | grep -q 'static TLS' && break
done
echo "after $i restarts: $(docker exec fp-tls curl -s localhost/check.php)"
Results (x86_64):
| Setup | Result |
|---|---|
php_ini opcache.enable 0 (OPcache loaded, disabled) |
intl fails to load after 215 restarts |
OPcache enabled (the same without the php_ini line) |
no failure in 1000 restarts |
OPcache not loaded (rm /usr/local/etc/php/conf.d/docker-php-ext-opcache.ini) |
no failure in 600 restarts |
Our app's image, ~30 extensions, opcache.enable 0 |
fails after 95 restarts |
The same, without the php_ini opcache.enable 0 line |
no failure in 150 restarts |
Likely cause
When OPcache is enabled, accel_startup() sets extension->handle = 0 ("Prevent unloading", ext/opcache/ZendAccelerator.c), so opcache.so stays loaded. When it is disabled, accel_startup() returns before that, so opcache.so is unloaded at module shutdown and loaded again by the php_module_startup() that rebootAllThreads() runs (frankenphp_new_main_thread). It looks like glibc does not give back the static TLS of the unloaded ZTS module, so every reboot takes more of the surplus until another extension no longer fits. This is our reading of the source and the measurements; we have not traced glibc's allocator.
Why opcache.enable=0, and workarounds
We disabled OPcache because of the OPcache races under ZTS (php-src GH-8739, #23637, #24319; see also #2714, #2715): in worker mode, restarting workers under load crashed the process with zend_mm_heap corrupted about once in 20 to 50 restarts on PHP 8.4.26.
What worked for us:
- Not loading OPcache instead of disabling it (what we use now).
- Raising glibc's reserve with
GLIBC_TUNABLES=glibc.rtld.optional_static_tls=1048576: 400 reboots without failure. Note that glibc ignoresGLIBC_TUNABLESfor the image's/usr/local/bin/frankenphp, because itscap_net_bind_servicefile capability puts it in secure-execution mode. A copy of the binary without the capability honoured it.
Perhaps worth a note in the docs (disable OPcache by not loading it, not with opcache.enable=0). Alternatively, keep extensions loaded across a thread reboot, as an enabled OPcache does for itself. You know the trade-offs there better than we do.
Build Type
Docker (Debian Trixie)
Worker Mode
Yes
Operating System
GNU/Linux
CPU Architecture
x86_64
PHP configuration
phpinfo() output
FrankenPHP v1.13.1 PHP 8.4.26 Caddy v2.11.7 (dunglas/frankenphp:1.13.1-php8.4-trixie)
Thread Safety => enabled
glibc 2.41 (Debian 13), host kernel Linux 7.2 x86_64, Docker
Extensions: the image's defaults + intl (installed with install-php-extensions)
php_ini opcache.enable 0
Relevant log output
Relevant log output
(after many "rebooting all PHP threads" / "workers restarted from admin api")
{"level":"warn","ts":1792408364.6600497,"logger":"frankenphp","msg":"PHP Warning: PHP Startup: Unable to load dynamic library 'intl' (tried: /usr/local/lib/php/extensions/no-debug-zts-20240924/intl (/usr/local/lib/php/extensions/no-debug-zts-20240924/intl: cannot open shared object file: No such file or directory), /usr/local/lib/php/extensions/no-debug-zts-20240924/intl.so (/usr/local/lib/php/extensions/no-debug-zts-20240924/intl.so: cannot allocate memory in static TLS block)) in Unknown on line 0","syslog_level":"warning"}
- Ngôn ngữ chính
- Go
- Star
- 11.4k
- Fork
- 499
- Merge trung bình
- 2 ngày 9 giờ
- Pull request đã merge (30 ngày)
- 40
Chuẩn bị môi trường
- Có Dockerfile hoặc tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của php/frankenphp
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 90/100
php/frankenphp#2671 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Include frankenphp version in logsCó thể làm lại được Pull request cho issue này đã bị đóng mà không được merge. Đang mởenhancement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
php/frankenphp#2483 · 3 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
suggestion about compile.mdCó thể làm lại được Pull request cho issue này đã bị đóng mà không được merge. Đang mởenhancement
Độ khó 1/5 1-3 giờ Mức phù hợp với người mới 68/100
php/frankenphp#601 · 5 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 52/100
php/frankenphp#2748 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
enhancement
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 43/100
php/frankenphp#2747 ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của php/frankenphp
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 67/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 64/100
FiloSottile/age#758 ·
-
nginx configĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
Maintainer thường phản hồi trong vòng 2 ngày
-
fix: the buttons mock cannot take back the match point -- its undo looks only for the running matchĐang mởbug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
stuttgart-things/zaehlwerk#168 ·
Maintainer thường phản hồi trong vòng 1 ngày