File tree 2 files changed +22
-1
lines changed
2 files changed +22
-1
lines changed Original file line number Diff line number Diff line change 16
16
17
17
from __future__ import annotations
18
18
19
+ import sys
19
20
import threading
20
21
import time
21
22
import weakref
@@ -92,7 +93,15 @@ def open(self) -> None:
92
93
thread .daemon = True
93
94
self ._thread = weakref .proxy (thread )
94
95
_register_executor (self )
95
- thread .start ()
96
+ # Mitigation to RuntimeError firing when thread starts on shutdown
97
+ # https://github.com/python/cpython/issues/114570
98
+ try :
99
+ thread .start ()
100
+ except RuntimeError as e :
101
+ if "interpreter shutdown" in str (e ) or sys .is_finalizing ():
102
+ self ._thread = None
103
+ return
104
+ raise
96
105
97
106
def close (self , dummy : Any = None ) -> None :
98
107
"""Stop. To restart, call open().
Original file line number Diff line number Diff line change 16
16
from __future__ import annotations
17
17
18
18
import gc
19
+ import subprocess
19
20
import sys
20
21
from functools import partial
21
22
@@ -79,6 +80,17 @@ def test_cleanup_executors_on_client_close(self):
79
80
for executor in executors :
80
81
wait_until (lambda : executor ._stopped , f"closed executor: { executor ._name } " , timeout = 5 )
81
82
83
+ def test_no_thread_start_runtime_err_on_shutdown (self ):
84
+ """Test we silence noisy runtime errors fired when the MongoClient spawns a new thread
85
+ on process shutdown."""
86
+ command = [sys .executable , "-c" , "from pymongo import MongoClient; c = MongoClient()" ]
87
+ completed_process : subprocess .CompletedProcess = subprocess .run (
88
+ command , capture_output = True
89
+ )
90
+
91
+ self .assertFalse (completed_process .stderr )
92
+ self .assertFalse (completed_process .stdout )
93
+
82
94
83
95
if __name__ == "__main__" :
84
96
unittest .main ()
You can’t perform that action at this time.
0 commit comments