@@ -642,18 +642,6 @@ def append(self, thread):
642
642
with self ._lock :
643
643
super ().append (thread )
644
644
645
- def remove (self , thread ):
646
- with self ._lock :
647
- # should not happen, but safe to ignore
648
- with contextlib .suppress (ValueError ):
649
- super ().remove (thread )
650
-
651
- def remove_current (self ):
652
- """Remove a current non-daemon thread."""
653
- thread = threading .current_thread ()
654
- if not thread .daemon :
655
- self .remove (thread )
656
-
657
645
def pop_all (self ):
658
646
with self ._lock :
659
647
self [:], result = [], self [:]
@@ -663,6 +651,14 @@ def join(self):
663
651
for thread in self .pop_all ():
664
652
thread .join ()
665
653
654
+ def reap (self ):
655
+ with self ._lock :
656
+ dead = [thread for thread in self if not thread .is_alive ()]
657
+ for thread in dead :
658
+ # should not happen, but safe to ignore
659
+ with contextlib .suppress (ValueError ):
660
+ self .remove (thread )
661
+
666
662
667
663
class _NoThreads :
668
664
"""
@@ -674,7 +670,7 @@ def append(self, thread):
674
670
def join (self ):
675
671
pass
676
672
677
- def remove_current (self ):
673
+ def reap (self ):
678
674
pass
679
675
680
676
@@ -701,15 +697,13 @@ def process_request_thread(self, request, client_address):
701
697
except Exception :
702
698
self .handle_error (request , client_address )
703
699
finally :
704
- try :
705
- self .shutdown_request (request )
706
- finally :
707
- self ._threads .remove_current ()
700
+ self .shutdown_request (request )
708
701
709
702
def process_request (self , request , client_address ):
710
703
"""Start a new thread to process the request."""
711
704
if self .block_on_close :
712
705
vars (self ).setdefault ('_threads' , _Threads ())
706
+ self ._threads .reap ()
713
707
t = threading .Thread (target = self .process_request_thread ,
714
708
args = (request , client_address ))
715
709
t .daemon = self .daemon_threads
0 commit comments