@@ -302,15 +302,25 @@ def keep_thread_active() -> bool:
302
302
if not client or (
303
303
hasattr (client , "_manual_cleanup" ) and client ._manual_cleanup
304
304
):
305
+ logger .debug ("Client is being cleaned up, stopping tracing thread" )
305
306
return False
306
307
if not threading .main_thread ().is_alive ():
307
308
# main thread is dead. should not be active
309
+ logger .debug ("Main thread is dead, stopping tracing thread" )
308
310
return False
309
311
310
312
if hasattr (sys , "getrefcount" ):
311
313
# check if client refs count indicates we're the only remaining
312
314
# reference to the client
313
- return sys .getrefcount (client ) > num_known_refs + len (sub_threads )
315
+ should_keep_thread = sys .getrefcount (client ) > num_known_refs + len (
316
+ sub_threads
317
+ )
318
+ if not should_keep_thread :
319
+ logger .debug (
320
+ "Client refs count indicates we're the only remaining reference "
321
+ "to the client, stopping tracing thread" ,
322
+ )
323
+ return should_keep_thread
314
324
else :
315
325
# in PyPy, there is no sys.getrefcount attribute
316
326
# for now, keep thread alive
@@ -349,7 +359,7 @@ def keep_thread_active() -> bool:
349
359
_tracing_thread_handle_batch (
350
360
client , tracing_queue , next_batch , use_multipart
351
361
)
352
- logger .debug ("Tracing control thread is shutting down" )
362
+ logger .debug ("Tracing control thread is shutting down" )
353
363
354
364
355
365
def tracing_control_thread_func_compress_parallel (
@@ -381,9 +391,11 @@ def keep_thread_active() -> bool:
381
391
if not client or (
382
392
hasattr (client , "_manual_cleanup" ) and client ._manual_cleanup
383
393
):
394
+ logger .debug ("Client is being cleaned up, stopping compression thread" )
384
395
return False
385
396
if not threading .main_thread ().is_alive ():
386
397
# main thread is dead. should not be active
398
+ logger .debug ("Main thread is dead, stopping compression thread" )
387
399
return False
388
400
if hasattr (sys , "getrefcount" ):
389
401
# check if client refs count indicates we're the only remaining
@@ -394,8 +406,13 @@ def keep_thread_active() -> bool:
394
406
active_count = sum (
395
407
1 for thread in thread_pool if thread is not None and thread .is_alive ()
396
408
)
397
-
398
- return sys .getrefcount (client ) > num_known_refs + active_count
409
+ should_keep_thread = sys .getrefcount (client ) > num_known_refs + active_count
410
+ if not should_keep_thread :
411
+ logger .debug (
412
+ "Client refs count indicates we're the only remaining reference "
413
+ "to the client, stopping compression thread" ,
414
+ )
415
+ return should_keep_thread
399
416
else :
400
417
# in PyPy, there is no sys.getrefcount attribute
401
418
# for now, keep thread alive
@@ -538,4 +555,4 @@ def _tracing_sub_thread_func(
538
555
_tracing_thread_handle_batch (
539
556
client , tracing_queue , next_batch , use_multipart
540
557
)
541
- logger .debug ("Tracing control sub-thread is shutting down" )
558
+ logger .debug ("Tracing control sub-thread is shutting down" )
0 commit comments