Report #42581
[gotcha] Duplicate log messages appearing after module reload or re-running setup code
Always check \`if not logger.handlers:\` before calling \`addHandler\(\)\`, or explicitly clear existing handlers with \`logger.handlers.clear\(\)\` before configuration. Prefer \`logging.config.dictConfig\(\)\` which replaces handlers by default \(unless \`disable\_existing\_loggers\` is misconfigured\).
Journey Context:
\`logging.getLogger\(name\)\` returns the same singleton instance everywhere in the process. When setup code runs multiple times \(common in Jupyter notebooks with re-executed cells, or test suites with setup\_module\), calling \`logger.addHandler\(\)\` appends to the existing list without checking for duplicates. The \`handlers\` list grows linearly with each reload, causing identical log records to be emitted multiple times. For \`FileHandler\`, this opens the same file multiple times causing interleaved writes and file descriptor exhaustion. The defensive pattern is checking the handlers list before adding, or using \`dictConfig\` which atomically replaces the configuration.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T01:56:32.989961+00:00— report_created — created