From a8ad93f7e2ce1744ff00258cd051154a1602bc42 Mon Sep 17 00:00:00 2001 From: shreeraj Date: Fri, 20 Nov 2026 13:53:45 +0530 Subject: [PATCH] Keep every consumer thread when thread > 1 Client.__init__ reset self.consumers to an empty list on each pass of the loop that starts the consumer threads, so with thread=N only the last consumer stayed in the list. join() and shutdown() pause and join the consumers in that list, which meant the other N-1 threads were never stopped and kept running after shutdown. Create the list once, before the loop, and add a regression test that checks all consumers are kept and are stopped by shutdown(). --- segment/analytics/client.py | 2 +- segment/analytics/test/test_client.py | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/segment/analytics/client.py b/segment/analytics/client.py index 50630814..c04ad330 100644 --- a/segment/analytics/client.py +++ b/segment/analytics/client.py @@ -120,8 +120,8 @@ def __init__( # to call flush(). if send: atexit.register(self.join) + self.consumers = [] for _ in range(thread): - self.consumers = [] consumer = Consumer( self.queue, write_key, diff --git a/segment/analytics/test/test_client.py b/segment/analytics/test/test_client.py index 68d6bd78..cb163622 100644 --- a/segment/analytics/test/test_client.py +++ b/segment/analytics/test/test_client.py @@ -266,6 +266,17 @@ def test_shutdown(self): for consumer in client.consumers: self.assertFalse(consumer.is_alive()) + def test_thread_option_keeps_every_consumer(self): + client = Client("testsecret", thread=3, on_error=self.fail) + try: + self.assertEqual(len(client.consumers), 3) + self.assertTrue(all(consumer.is_alive() for consumer in client.consumers)) + finally: + client.shutdown() + # shutdown() has to stop every consumer thread, not only the last one + for consumer in client.consumers: + self.assertFalse(consumer.is_alive()) + def test_synchronous(self): client = Client("testsecret", sync_mode=True)