One `python3` call through a shim runs 14 to 16 shell scripts; macOS keeps ~1 KB of kernel memory for each nested one until reboot

未关闭
#3,674 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
53/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
技术栈
python, shell
领域
cli, performance

调研方向

Start by tracing the shim call through the listed pyenv scripts: pyenv-hooks, pyenv-version-name, pyenv-version-file, pyenv-version-file-read, pyenv, pyenv-exec, pyenv-which, pyenv-prefix, and pyenv-latest. Compare the script launches for a global version and system, then look for ways to reduce launches while preserving version selection and execution behavior. Done when shim calls launch fewer scripts and the reported reproduction shows a corresponding reduction in kernel zone growth on macOS.

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

描述

Describe the bug

The root bug is in macOS: when a script launches another #! script, the kernel keeps a 1 KB block in data.kalloc.1024 that only a reboot frees (independent write-up for macOS 26). pyenv hits it hard because one shim call is a chain of scripts. On a Mac running a lot of automated work, the zone reached 20 GB after 9.4 days and the machine panicked (zone map exhausted ... [data.kalloc.1024]). In a 10-minute process sample on that Mac, pyenv's scripts were the largest source of script launches; other scripts leak too.

Expected: pyenv can't fix the kernel, but fewer script launches per shim call would cut its share in proportion. Related: #2802.

Scripts launched per call (pyenv 2.8.15, fresh clone, no extra plugins)

I added echo "${0##*/}" >> log as line 2 of each executable script in a copy of the tree and ran python3 -c pass once through the shim.

  • Global version set: the shim plus 15: pyenv-hooks x4, pyenv-version-name x2, pyenv-version-file x2, pyenv-version-file-read x2, pyenv, pyenv-exec, pyenv-which, pyenv-prefix, pyenv-latest.
  • system: the shim plus 13.
Kernel memory per call (macOS 15.7.3 24G419, Apple silicon)

2.8.15, global version set, 300 calls from a launchd job running a native binary (so no parent was a script), minus an idle window of the same length:

300 calls of python3 -I -c pass zone grew idle window net blocks per call
through the shim 5,462 909 15.2
interpreter run directly 1,022 569 1.5 (noise)

That is one clean run; two later runs were swamped by unrelated load and I discarded them. A traced copy of Homebrew pyenv 2.6.4 with system runs the same 13 scripts after the shim.

Reproduction steps
z() { zprint kalloc.1024 2>/dev/null | awk '$1 ~ /^data(_shared)?\.kalloc\.1024$/ {n+=$7} END {print n}'; }
z; for i in $(seq 300); do ~/.pyenv/shims/python3 -c pass; done; z
z; for i in $(seq 300); do "$(pyenv which python3)" -c pass; done; z

zprint worked without sudo on 15.7.3. Other processes also grow the zone, so use a quiet machine or compare with an idle window. Blocks are not returned afterwards.

Diagnostic details
  • Platform: macOS 15.7.3 (24G419)
  • Architecture: arm64
  • pyenv: 2.8.15 (clone) and 2.6.4 (Homebrew)
  • Python: 3.12.4

Measured and drafted with Claude Code.

主要语言
Shell
星标
45.2k
派生
3.3k
平均合并
2 天 3 分钟
30 天内合并 PR
32

环境准备

从这里开始

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

pyenv/pyenv 的其他 Issue

查看 pyenv/pyenv 的全部 Issue

相似的 Issue

更多 Shell/Bash Issue

把新 issue 发到你的邮箱

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