Skip to content

Commit 33057c7

Browse files
bpo-41739: Fix test_logging.test_race_between_set_target_and_flush() (GH-22655) (GH-22656) (GH-22662)
The test now waits until all threads complete to avoid leaking running threads. Also, use regular threads rather than daemon threads. (cherry picked from commit 13ff396) (cherry picked from commit f5393dc) Co-authored-by: Victor Stinner <[email protected]> Co-authored-by: Victor Stinner <[email protected]>
1 parent 1006f63 commit 33057c7

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

‎Lib/test/test_logging.py‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,22 +1177,27 @@ def test_race_between_set_target_and_flush(self):
11771177
class MockRaceConditionHandler:
11781178
def __init__(self, mem_hdlr):
11791179
self.mem_hdlr = mem_hdlr
1180+
self.threads = []
11801181

11811182
def removeTarget(self):
11821183
self.mem_hdlr.setTarget(None)
11831184

11841185
def handle(self, msg):
1185-
t = threading.Thread(target=self.removeTarget)
1186-
t.daemon = True
1187-
t.start()
1186+
thread = threading.Thread(target=self.removeTarget)
1187+
self.threads.append(thread)
1188+
thread.start()
11881189

11891190
target = MockRaceConditionHandler(self.mem_hdlr)
1190-
self.mem_hdlr.setTarget(target)
1191+
try:
1192+
self.mem_hdlr.setTarget(target)
11911193

1192-
for _ in range(10):
1193-
time.sleep(0.005)
1194-
self.mem_logger.info("not flushed")
1195-
self.mem_logger.warning("flushed")
1194+
for _ in range(10):
1195+
time.sleep(0.005)
1196+
self.mem_logger.info("not flushed")
1197+
self.mem_logger.warning("flushed")
1198+
finally:
1199+
for thread in target.threads:
1200+
support.join_thread(thread)
11961201

11971202

11981203
class ExceptionFormatter(logging.Formatter):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix test_logging.test_race_between_set_target_and_flush(): the test now
2+
waits until all threads complete to avoid leaking running threads.

0 commit comments

Comments
 (0)