gh-127567: Avoid forceful shutting down of handlers during reconfiguration#127690
gh-127567: Avoid forceful shutting down of handlers during reconfiguration#127690Agent-Hellboy wants to merge 9 commits into
Conversation
| handler = logging.root.handlers[0] | ||
| self.addCleanup(closeFileHandler, handler, fn) | ||
|
|
||
| def test_clear_existing_handlers_preserves_active_handlers(self): |
There was a problem hiding this comment.
@Agent-Hellboy I don't understand how this test covers the problem described in the original issue... The problem in the issue is with handlers of loggers that are not supposed to be modified by new configuration, not with handlers defined in the configuration...
I would propose the following test:
def test_disable_existing_loggers(self):
fn = make_temp_file(".log", "test_logging-disable-existing-loggers-")
file_handler = logging.FileHandler(fn, mode="w")
file_handler.setFormatter(logging.Formatter("%(message)s"))
root_logger = logging.getLogger()
root_logger.addHandler(file_handler)
config = {"version": 1, "disable_existing_loggers": False}
# we have disable_existing_loggers=False,
# so, all handlers should continue working
self.apply_config(config)
msg = "test message"
logging.warning(msg)
file_handler.close()
with open(fn, encoding='utf-8') as f:
data = f.read().strip()
os.remove(fn)
self.assertEqual(data, msg)There was a problem hiding this comment.
okay, I was testing assert fileHandler in root_logger.handlers only, I have used yours test now
…python into fix-issue-127567
| @@ -0,0 +1 @@ | |||
| Avoid shutting down handlers during reconfiguration | |||
There was a problem hiding this comment.
You should clarify that this is related to the logging module.
| """Clear and close handlers that are no longer in use.""" | ||
| active_handlers = { | ||
| handler | ||
| for logger in logging.Logger.manager.loggerDict.values() |
There was a problem hiding this comment.
The reason why now the test is passing is that the root logger is not included in manager.loggerDict(). If you change the following line in the test
root_logger = logging.getLogger()to
root_logger = logging.getLogger("any-logger")the test will fail.
There was a problem hiding this comment.
sorry, I simply copied your test without giving a thought
| @@ -285,9 +285,20 @@ def _install_loggers(cp, handlers, disable_existing): | |||
|
|
|||
|
|
|||
| def _clearExistingHandlers(): | |||
There was a problem hiding this comment.
Shouldn't we call this _clearExistingHandlers() after we applied all the modifications from config, not before as it's happening now?
There was a problem hiding this comment.
I have added a check
Line 907 in 2041a95
here as well to counter it , I will do that for fileconfig as well
| @@ -285,9 +285,20 @@ def _install_loggers(cp, handlers, disable_existing): | |||
|
|
|||
|
|
|||
| def _clearExistingHandlers(): | |||
There was a problem hiding this comment.
Should we rename the method to better reflect its behaviour? To something like _clearNotUsedHandlers.
| handler.close() | ||
|
|
||
| logging._handlers.clear() | ||
| logging.shutdown(logging._handlerList[:]) |
There was a problem hiding this comment.
the logging.shutdown() method calls handler.acquire(), handler.flush() and handler.release(). Also, it iterate all these handlers in a specific order (not sure why).
Don't we need all these actions here?
There was a problem hiding this comment.
Also, it iterate all these handlers in a specific order (not sure why)
didn't get your question, it is to protect the handler in a concurrent environment. A mutex lock , acquire operation release
There was a problem hiding this comment.
My question is why your implementation in this PR doesn't used them?
|
@Agent-Hellboy I see the following issues in this PR:
|
|
This PR is stale because it has been open for 30 days with no activity. |
Uh oh!
There was an error while loading. Please reload this page.