add_hookcall_monitoring undo breaks when called out of LIFO order

Đang mở
#815 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

@AlvaroBalbin đang làm issue này rồi.

Từ ngày 3/11/2026.

  • #816 của @mikegit123git — đã đóng, không merge
  • #825 của @AlvaroBalbin — đang mở

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
55/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
python
Lĩnh vực
tooling

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

🤖 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.

Ngôn ngữ chính
Python
Star
1.7k
Fork
200
Merge trung bình
10 giờ 4 phút
Pull request đã merge (30 ngày)
13

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. 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.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của pytest-dev/pluggy

Tất cả issue của pytest-dev/pluggy

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.