From 1d1e710187f2e16dee26c61069fb8ebe7a7dd4a9 Mon Sep 17 00:00:00 2001 From: Min RK Date: Thu, 28 Nov 2024 09:48:12 +0100 Subject: [PATCH] suppress exceptions when closing handlers during `__del__` when `__del__` is called during process teardown, any amount of things may already be torn down and fail currently seeing this with `typing.cast` being None, preventing access of `self.log` (or any trait) from succeeding --- traitlets/config/application.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/traitlets/config/application.py b/traitlets/config/application.py index b01a11e5..d2ba8c87 100644 --- a/traitlets/config/application.py +++ b/traitlets/config/application.py @@ -1062,7 +1062,11 @@ def exit(self, exit_status: int | str | None = 0) -> None: sys.exit(exit_status) def __del__(self) -> None: - self.close_handlers() + # __del__ may be called during process teardown, + # at which point any fraction of attributes and modules may have been cleared, + # e.g. even _accessing_ self.log may fail. + with suppress(Exception): + self.close_handlers() @classmethod def launch_instance(cls, argv: ArgvType = None, **kwargs: t.Any) -> None: