Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] Removing handlers in Background Logger extension removes only one handler. #254

Open
kuchara opened this issue Jul 22, 2024 · 0 comments

Comments

@kuchara
Copy link

kuchara commented Jul 22, 2024

Describe the bug
setup_server_logging() @ extensions/logging/logger.py (https://github.com/sanic-org/sanic-ext/blob/main/sanic_ext/extensions/logging/logger.py#L49) mutates logger_instance.handlers list while iterating over it. This is not correct.

for handler in logger_instance.handlers:
      logger_instance.removeHandler(handler)

If there are 2 handlers, most often, only one of them is being removed.

To Reproduce
N/A

Expected behavior
Mentioned code should remove all handlers, and replace them by a QueueHandler instance.

Environment (please complete the following information):

  • OS: Ubuntu 22.04.3 LTS @ WSL2
  • Browser: N/A
  • Version: N/A

Additional context
My proposal to fix would be something like described in https://stackoverflow.com/questions/7484454/removing-handlers-from-pythons-logging-loggers :

for handler in list(logger_instance.handlers):
    logger_instance.removeHandler(handler)

Note: none of the options from the linked SO question is thread safe, but Python so far does not provide method to safely cleanup all handlers in one instruction.

See also on mutating list while iterating: https://discuss.python.org/t/how-safe-documented-is-it-to-mutate-a-list-being-iterated/18929

@kuchara kuchara changed the title [Bug] Removing handlers in Backround Logger extension removes only one handler. [Bug] Removing handlers in Background Logger extension removes only one handler. Jul 22, 2024
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

No branches or pull requests

1 participant