python / python/cpython

Code object comparison could lead to monitoring issue

Offen
#136,223 2 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

interpreter-core type-bug
Vorherrschende Sprache
Python
Sterne
77.2k
Forks
35.9k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Bug report

Bug description:

Currently we compare code objects with a lot of its features, but not the file it is defined in - that might make some sense because the two code objects are "equivalent" as long as all their features match. However, the two code objects could still be two different ones - for example, if they are two exact same functions defined in two different files (this is a real case for me and it took me a while to figure it out).

The problem is, code objects can be used as keys. When we have "equal but not the same" code objects, it could affect monitoring. We thought we were monitoring one code object while we are actually monitoring another one.

The code was like this:

@cache
def get_code_lineno(code):
    ...
    return {code: [...]}

assert code1 == code2  # two code objects in two different files

for c, linenos in get_code_lineno(code1):
    sys.monitoring.set_local_events(tool, c, E.LINE)

for c, linenos in get_code_lineno(code2):
    # This does not instrument code2! This instruments code1 because of the cache!
    sys.monitoring.set_local_events(tool, c, E.LINE)

Do we consider this an expected behavior? Should we just "live with it" as it's not that common? (Again, this is a real issue I had when I was working on a project).

@markshannon

CPython versions tested on:

3.12, CPython main branch, 3.13, 3.14

Operating systems tested on:

No response

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Der Bericht nennt keine Quelldatei und keinen Test. Beginne damit, die zwischengespeicherte Suche mit äquivalenten Code-Objekten aus verschiedenen Dateien zu reproduzieren, und untersuche anschließend den Vergleich von Code-Objekten sowie den Einstiegspunkt sys.monitoring.set_local_events. Das beabsichtigte Verhalten muss festgelegt werden, bevor ein Regressionstest verifizieren kann, dass das Monitoring nicht auf das falsche Code-Objekt zielt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
devtools
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Muss geklärt werden
Anfängerfreundlichkeit
30/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.