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 commit13ff396c01
) (cherry picked from commitf5393dc2a0
) Co-authored-by: Victor Stinner <vstinner@python.org> Co-authored-by: Victor Stinner <vstinner@python.org>
This commit is contained in:
parent
1006f63a8b
commit
33057c7092
|
@ -1177,22 +1177,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:
|
||||
support.join_thread(thread)
|
||||
|
||||
|
||||
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…
Reference in New Issue