diff --git a/changelog/4988.bugfix.rst b/changelog/4988.bugfix.rst new file mode 100644 index 000000000..8cc816ed6 --- /dev/null +++ b/changelog/4988.bugfix.rst @@ -0,0 +1 @@ +Close logging's file handler explicitly when the session finishes. diff --git a/src/_pytest/logging.py b/src/_pytest/logging.py index 22db44301..5a31dfc5c 100644 --- a/src/_pytest/logging.py +++ b/src/_pytest/logging.py @@ -577,8 +577,15 @@ class LoggingPlugin(object): if self.log_cli_handler: self.log_cli_handler.set_when("sessionfinish") if self.log_file_handler is not None: - with catching_logs(self.log_file_handler, level=self.log_file_level): - yield + try: + with catching_logs( + self.log_file_handler, level=self.log_file_level + ): + yield + finally: + # Close the FileHandler explicitly. + # (logging.shutdown might have lost the weakref?!) + self.log_file_handler.close() else: yield