add_hookcall_monitoring undo breaks when called out of LIFO order

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

维护者通常 1 天内回复

@AlvaroBalbin 已经在做这个了。

开始于 2026年11月3日。

  • #816 来自 @mikegit123git —— 已关闭,未合并
  • #825 来自 @AlvaroBalbin —— 未关闭

评估

难度
3/5
预计耗时
1-2 天
新手友好度
55/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
python
领域
tooling

调研方向

Start in src/pluggy/_manager.py, at add_hookcall_monitoring() and its undo(), which restores self._inner_hookexec to a saved value. Run the reproducer from the issue, which adds monitors A and B, undoes A, then B, and prints the recorded events. enable_tracing() is built on the same function, so check it too. Done means undoing in any order removes only that monitor, repeated undo is harmless, and in-flight calls keep their original chain. Add a test for out-of-order undo next to the existing monitoring tests.

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

描述

🤖 Written by Claude Opus 5.5 via Claude Code for the pluggy maintainers; I prompted it, it did the work, I read it.

add_hookcall_monitoring() returns an undo that restores the _inner_hookexec it saw at add time (src/pluggy/_manager.py, undo() sets self._inner_hookexec = oldcall). So undo only works in strict LIFO order:

  • undoing an older monitor also removes every monitor added after it
  • a later undo can bring back a monitor that was already removed

This applies to enable_tracing() too, since it is built on add_hookcall_monitoring().

Reproducer:

import pluggy

hs = pluggy.HookspecMarker("x")
hi = pluggy.HookimplMarker("x")

class S:
    @hs
    def h(self): ...

class P:
    @hi
    def h(self): return 1

pm = pluggy.PluginManager("x")
pm.add_hookspecs(S)
pm.register(P())
ev = []

def mon(label):
    return pm.add_hookcall_monitoring(lambda *a: ev.append(label), lambda *a: None)

undo_a = mon("A")
undo_b = mon("B")

undo_a(); pm.hook.h(); print(ev)  # [] -- expected ['B']
ev.clear()
undo_b(); pm.hook.h(); print(ev)  # ['A'] -- expected []

The same thing happens on current main. pytest's own uses (--debug, pytester's HookRecorder) undo in LIFO order, so as far as we know nobody has hit this in practice.

Possible fix: keep the active (before, after) pairs in a list on the PluginManager. Each undo removes its own entry by identity, which makes repeated undo harmless, and then rebuilds _inner_hookexec from the list. Calls already in flight keep the chain they started with.

Found while reviewing #814, which fixed this with a much larger change and was closed.

主要语言
Python
星标
1.7k
派生
200
平均合并
10 小时 4 分钟
30 天内合并 PR
13

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

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

pytest-dev/pluggy 的其他 Issue

查看 pytest-dev/pluggy 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

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