Skip to content

Commit 49f36bb

Browse files
authored
Merge pull request #4988 from blueyed/logging-close
logging: close log_file_handler
2 parents ee96214 + 538efef commit 49f36bb

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

changelog/4988.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Close logging's file handler explicitly when the session finishes.

src/_pytest/logging.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -577,8 +577,15 @@ def pytest_sessionfinish(self):
577577
if self.log_cli_handler:
578578
self.log_cli_handler.set_when("sessionfinish")
579579
if self.log_file_handler is not None:
580-
with catching_logs(self.log_file_handler, level=self.log_file_level):
581-
yield
580+
try:
581+
with catching_logs(
582+
self.log_file_handler, level=self.log_file_level
583+
):
584+
yield
585+
finally:
586+
# Close the FileHandler explicitly.
587+
# (logging.shutdown might have lost the weakref?!)
588+
self.log_file_handler.close()
582589
else:
583590
yield
584591

0 commit comments

Comments
 (0)