mirror of
https://github.com/python/cpython.git
synced 2025-09-19 23:20:25 +00:00
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.
This commit is contained in:
parent
47ecfd8030
commit
13ff396c01
2 changed files with 15 additions and 8 deletions
|
@ -1164,22 +1164,27 @@ class MemoryHandlerTest(BaseTest):
|
||||||
class MockRaceConditionHandler:
|
class MockRaceConditionHandler:
|
||||||
def __init__(self, mem_hdlr):
|
def __init__(self, mem_hdlr):
|
||||||
self.mem_hdlr = mem_hdlr
|
self.mem_hdlr = mem_hdlr
|
||||||
|
self.threads = []
|
||||||
|
|
||||||
def removeTarget(self):
|
def removeTarget(self):
|
||||||
self.mem_hdlr.setTarget(None)
|
self.mem_hdlr.setTarget(None)
|
||||||
|
|
||||||
def handle(self, msg):
|
def handle(self, msg):
|
||||||
t = threading.Thread(target=self.removeTarget)
|
thread = threading.Thread(target=self.removeTarget)
|
||||||
t.daemon = True
|
self.threads.append(thread)
|
||||||
t.start()
|
thread.start()
|
||||||
|
|
||||||
target = MockRaceConditionHandler(self.mem_hdlr)
|
target = MockRaceConditionHandler(self.mem_hdlr)
|
||||||
self.mem_hdlr.setTarget(target)
|
try:
|
||||||
|
self.mem_hdlr.setTarget(target)
|
||||||
|
|
||||||
for _ in range(10):
|
for _ in range(10):
|
||||||
time.sleep(0.005)
|
time.sleep(0.005)
|
||||||
self.mem_logger.info("not flushed")
|
self.mem_logger.info("not flushed")
|
||||||
self.mem_logger.warning("flushed")
|
self.mem_logger.warning("flushed")
|
||||||
|
finally:
|
||||||
|
for thread in target.threads:
|
||||||
|
threading_helper.join_thread(thread)
|
||||||
|
|
||||||
|
|
||||||
class ExceptionFormatter(logging.Formatter):
|
class ExceptionFormatter(logging.Formatter):
|
||||||
|
|
|
@ -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.
|
Loading…
Add table
Add a link
Reference in a new issue