Skip to content

Remove the query logger when the query_logger() block raises - #1373

Merged
elprans merged 1 commit into
MagicStack:masterfrom
kratos0718:fix-query-logger-on-error
Sep 29, 2026
Merged

elprans merged 1 commit into
MagicStack:masterfrom
kratos0718:fix-query-logger-on-error

Conversation

@kratos0718

Copy link
Copy Markdown
Contributor

Connection.query_logger() removed its callback after a bare yield, so if anything inside the with block raises, for example a query that fails, the callback stays registered on the connection. With a pool, that connection is handed to other code later and the logger keeps receiving those unrelated queries, and it is never released.

The fix moves remove_query_logger() into a finally. I added test_logging_context_removed_on_error to tests/test_logging.py. It fails on master with the logger still attached (1 != 0) and passes with the change. tests.test_logging and flake8 pass locally.

query_logger() removed its callback after a bare yield, so any exception
in the with-block, such as a failing query, left the logger attached to
the connection. Pooled connections then kept reporting later, unrelated
queries to it.

@elprans elprans left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@elprans
elprans merged commit 6a7fbd3 into MagicStack:master Sep 29, 2026
53 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants