From 13ff396c019d548ba181cf22c6f39309a300723c Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Mon, 12 Oct 2020 00:37:20 +0200 Subject: [PATCH] bpo-41739: Fix test_logging.test_race_between_set_target_and_flush() (GH-22655) The test now waits until all threads complete to avoid leaking running threads. Also, use regular threads rather than daemon threads. --- Lib/test/test_logging.py | 21 ++++++++++++------- .../2020-10-12-00-11-47.bpo-41739.wSCc4K.rst | 2 ++ 2 files changed, 15 insertions(+), 8 deletions(-) create mode 100644 Misc/NEWS.d/next/Tests/2020-10-12-00-11-47.bpo-41739.wSCc4K.rst diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py index 4cd8c7e25da..7c98e19b740 100644 --- a/Lib/test/test_logging.py +++ b/Lib/test/test_logging.py @@ -1164,22 +1164,27 @@ class MemoryHandlerTest(BaseTest): class MockRaceConditionHandler: def __init__(self, mem_hdlr): self.mem_hdlr = mem_hdlr + self.threads = [] def removeTarget(self): self.mem_hdlr.setTarget(None) def handle(self, msg): - t = threading.Thread(target=self.removeTarget) - t.daemon = True - t.start() + thread = threading.Thread(target=self.removeTarget) + self.threads.append(thread) + thread.start() target = MockRaceConditionHandler(self.mem_hdlr) - self.mem_hdlr.setTarget(target) + try: + self.mem_hdlr.setTarget(target) - for _ in range(10): - time.sleep(0.005) - self.mem_logger.info("not flushed") - self.mem_logger.warning("flushed") + for _ in range(10): + time.sleep(0.005) + self.mem_logger.info("not flushed") + self.mem_logger.warning("flushed") + finally: + for thread in target.threads: + threading_helper.join_thread(thread) class ExceptionFormatter(logging.Formatter): diff --git a/Misc/NEWS.d/next/Tests/2020-10-12-00-11-47.bpo-41739.wSCc4K.rst b/Misc/NEWS.d/next/Tests/2020-10-12-00-11-47.bpo-41739.wSCc4K.rst new file mode 100644 index 00000000000..7aee2b94444 --- /dev/null +++ b/Misc/NEWS.d/next/Tests/2020-10-12-00-11-47.bpo-41739.wSCc4K.rst @@ -0,0 +1,2 @@ +Fix test_logging.test_race_between_set_target_and_flush(): the test now +waits until all threads complete to avoid leaking running threads.