File tree Expand file tree Collapse file tree 2 files changed +22
-1
lines changed Expand file tree Collapse file tree 2 files changed +22
-1
lines changed Original file line number Diff line number Diff line change 1616
1717from __future__ import annotations
1818
19+ import sys
1920import threading
2021import time
2122import weakref
@@ -92,7 +93,15 @@ def open(self) -> None:
9293 thread .daemon = True
9394 self ._thread = weakref .proxy (thread )
9495 _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
96105
97106 def close (self , dummy : Any = None ) -> None :
98107 """Stop. To restart, call open().
Original file line number Diff line number Diff line change 1616from __future__ import annotations
1717
1818import gc
19+ import subprocess
1920import sys
2021from functools import partial
2122
@@ -79,6 +80,17 @@ def test_cleanup_executors_on_client_close(self):
7980 for executor in executors :
8081 wait_until (lambda : executor ._stopped , f"closed executor: { executor ._name } " , timeout = 5 )
8182
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+
8294
8395if __name__ == "__main__" :
8496 unittest .main ()
You can’t perform that action at this time.
0 commit comments