add_hookcall_monitoring undo breaks when called out of LIFO order

Aperta
#815 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@AlvaroBalbin ci sta già lavorando.

Dal 3/11/2026.

  • #816 di @mikegit123git — chiusa senza merge
  • #825 di @AlvaroBalbin — aperta

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
55/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
tooling

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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

Lingua principale
Python
Stelle
1.7k
Fork
200
Merge medio
10h 4m
PR unite (30g)
13

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di pytest-dev/pluggy

Tutte le issue di pytest-dev/pluggy

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.