php / php/php-src

Disable SHM reattachment

未关闭
#20,470 1 条评论 2 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Feature Status: Needs Triage
主要语言
C
星标
40.4k
派生
8.1k
平均合并
2 天 13 小时
30 天内合并 PR
96

描述

Description

I'm opening this issue to restart the discussion started by @cmb69 in https://github.com/php/php-src/pull/17838 and https://github.com/php/php-src/pull/7865.

SHM reattachment leads to a number of problems, and likely fails often in practice, so I would like to disable it.

The reason we have SHM reattachment is that Windows doesn't support fork(), so multi-process SAPIs spawn independent processes. Therefore each process needs to reattach the Opcache SHM of previous processes.

This comes with a number of problems:

  1. The SHM must be attached at the same address in all processes. If the address is not free in one process, reattachment can not happen.
  2. When ASLR is enabled, some addresses referenced by SHM are invalid (such as opcode handler addresses stored in op arrays). Therefore, reattachment fails when ASLR is enabled (source).
  3. The memory layout of each process can differ regardless of ASLR, for example when DLLs were loaded in a different order. Therefore, SHM can not usually reference addresses out of SHM. This leads to various Windows-specific workarounds and special cases in the code base. For example, the inheritance cache is disabled for sub-classes of internal classes.

Additionally, this makes preloading incompatible with Windows, which blocks https://github.com/php/php-src/pull/18204.

In cases 1 and 2, the process fails with a fatal error, or fallsback to file_cache_only mode (when enabled), which is slower. So processes can randomly fail or be slower.

Case 2 implies that reattachment fails most of the times, now that ASLR is enabled by default.

Because of these, I believe that it would be beneficial to disable reattachment entirely.

Here is the impact on existing ways to deploy PHP, and what changes are required before we can disable reattachment:

IIS + FastCGI

In this configuration IIS itself manages PHP processes. Unfortunately this enforces a process-based model, and reattachment is required so that concurrent requests share the same cache.

I suggest that we recommend other configurations for performance-sensitive deployments. Disabling reattachment wouldn't impact development setups, so we can continue to support this configuration without reattachment.

Nginx + FastCGI

In this configuration, php-cgi manages child processes itself. Therefore we can switch this SAPI to threads in ZTS builds so that reattachment is not necessary anymore. This would likely make the SAPI more resource-efficient on Windows as well.

Apache + mod_php

Uses mpm_winnt, which spawns a single process and multiple threads.

Therefore, reattachment should not be necessary.

See https://github.com/php/php-src/pull/7865.

Additionally, @cmb69 started the implementation of an isapi-based SAPI:

IIS + isapi

Threaded, doesn't need reattachment

See https://github.com/php/php-src/pull/17838.

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 ext/opcache/shared_alloc_win32.c 开始,并阅读相关的 pull request #17838 和 #7865。审查 SHM 重新附加对 IIS、Nginx、Apache 以及提议的 ISAPI SAPI 有何影响,然后定义所需的部署变更。当达成一致的设计并完成实现,在不破坏受支持的线程配置的情况下禁用重新附加时,即视为完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
c, php
领域
backend, operating-systems
Issue 类型
功能
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
需要澄清
新手友好度
25/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。