python / python/cpython

`logging.log` doesn't naively work as audit hook for `sys.addaudithook`

Offen
#98,105 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

stdlib type-bug
Vorherrschende Sprache
Python
Sterne
77.2k
Forks
36k
Ø Merge
1 T. 9 Std.
Gemergte PRs (30 T.)
558

Beschreibung

Bug report

Documentation for sys.addaudithook states "Hooks can then log the event [...]".
This works when I don't log, but instead print:

sys.addaudithook(print)
id(0)  # printed

However, nothing gets logged when I follow the suggestion to log, like:

sys.addaudithook(functools.partial(logging.warning, 'Audit hook %s %s'))
id(0)  # NOT logged

The cause is that logging.Logger._log (called by logging.warning) tries to find the caller (to populate module, lineno, funcName log format fields). For this, logging.Logger._log itself then causes audit events, which again call the audit hook. That results in infinite recursion, i.e. a RecursionError exception, which aborts the audit hook, and silences logging.

Workaround

By (temporarily) modifying logging._srcfile, the attempt to find the caller is skipped (and the log format fields are instead populated as "unknown"), infinite recursion and exception are prevented, and logging does happen:

sys.addaudithook(functools.partial(logging.warning, 'Audit hook %s %s'))
logging._srcfile = None  # prevent finding caller
id(0)  # logged

This is existing functionality, only (?) documented in source (file logging.__init__, line 188: "Setting _srcfile to None will prevent findCaller() from being called. This way, you can avoid the overhead of fetching caller information"). Though it works, the workaround is hard to find and not ideal.

Solutions

  • Ideally, logging should not attempt to gather caller info in an audit hook (however, this requires tight coupling of the logging and sys modules, so they understand each others' internals)
  • Equally user-friendly, audit events would not be generated within audit hooks (that risks incompleteness of auditing, but so does silencing logging)
  • Less user-friendly, a keyword parameter (e.g. find_caller: bool = True) could be added to the signature of each log function, logging.Logger._log can inspect it, and the documentation for sys.addaudithook should recommend to use find_caller=False for logging in audit hooks
# logging.Logger._log
if logging._srcfile and find_caller:
    ...

# to use
sys.addaudithook(functools.partial(logging.warning, 'Audit hook %s %s', find_caller=False))
id(0)  # logged
  • At the minimum, the documentation for sys.addaudithook should recommend above workaround, i.e. temporarily set logging._srcfile = None
def my_log(event, args):
    save_srcfile = logging._srcfile
    logging._srcfile = None
    logging.warning('Audit hook %s %s', event, args)
    logging._srcfile = save_srcfile

sys.addaudithook(my_log)
id(0)  # logged

Environment

The above issue exists in all Python versions supporting sys.addaudithook, i.e. 3.8, 3.9, 3.10, and 3.11.
I tested on 3.8.10, 3.9.13, 3.10.4, and 3.110rc2 on Windows 11 64 bit.

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

Reproduziere die Rekursion anhand der Beispiele sys.addaudithook und logging.warning und lies anschließend logging.init, insbesondere Logger._log, findCaller() und den _srcfile-Hinweis, zusammen mit der Dokumentation zu sys.addaudithook. Ermittle, welches der vorgeschlagenen Verhaltensweisen oder welche Dokumentationsänderung akzeptabel ist, und überprüfe, dass die Audit-Hook-Protokollierung nicht mehr rekursiv aufgerufen wird oder stillschweigend fehlschlägt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
documentation, security
Issue-Typ
Bug
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

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