File tree Expand file tree Collapse file tree 2 files changed +42
-1
lines changed Expand file tree Collapse file tree 2 files changed +42
-1
lines changed Original file line number Diff line number Diff line change @@ -599,6 +599,43 @@ def task():
599
599
600
600
self .assertEqual (os .read (r_interp , 1 ), FINISHED )
601
601
602
+ def test_subthreads_raise_exception_from_atexit (self ):
603
+ r_interp , w_interp = self .pipe ()
604
+
605
+ FINISHED = b'F'
606
+
607
+ prev_excepthook = threading .excepthook
608
+ interp = interpreters .create ()
609
+ interp .exec (f"""if True:
610
+ from io import StringIO
611
+ import os
612
+ import threading
613
+ import time
614
+
615
+ threading.excepthook = lambda args: args
616
+
617
+ done = False
618
+ def notify_fini():
619
+ global done
620
+ done = True
621
+
622
+ def raise_exception():
623
+ raise Exception('AtexitException')
624
+ threading._register_atexit(notify_fini)
625
+ threading._register_atexit(raise_exception)
626
+
627
+ def task():
628
+ while not done:
629
+ time.sleep(0.1)
630
+ os.write({ w_interp } , { FINISHED !r} )
631
+ t = threading.Thread(target=task)
632
+ t.start()
633
+ """ )
634
+ interp .close ()
635
+
636
+ self .assertEqual (threading .excepthook , prev_excepthook )
637
+ self .assertEqual (os .read (r_interp , 1 ), FINISHED )
638
+
602
639
def test_created_with_capi (self ):
603
640
script = dedent (f"""
604
641
import { interpreters .__name__ } as interpreters
Original file line number Diff line number Diff line change @@ -1546,7 +1546,11 @@ def _shutdown():
1546
1546
# Call registered threading atexit functions before threads are joined.
1547
1547
# Order is reversed, similar to atexit.
1548
1548
for atexit_call in reversed (_threading_atexits ):
1549
- atexit_call ()
1549
+ try :
1550
+ atexit_call ()
1551
+ except :
1552
+ th = current_thread ()
1553
+ th ._invoke_excepthook (th )
1550
1554
1551
1555
if _is_main_interpreter ():
1552
1556
_main_thread ._handle ._set_done ()
You can’t perform that action at this time.
0 commit comments