File tree 3 files changed +25
-1
lines changed
3 files changed +25
-1
lines changed Original file line number Diff line number Diff line change @@ -29,6 +29,9 @@ PyMongo 4.7 brings a number of improvements including:
29
29
30
30
.. _orjson : https://github.com/ijl/orjson
31
31
32
+ - Fixed a bug appearing in Python 3.12 where "RuntimeError: can't create new thread at interpreter shutdown"
33
+ could be written to stderr when a MongoClient's thread starts as the python interpreter is shutting down.
34
+
32
35
Changes in Version 4.6.1
33
36
------------------------
34
37
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
@@ -91,7 +92,15 @@ def open(self) -> None:
91
92
thread .daemon = True
92
93
self ._thread = weakref .proxy (thread )
93
94
_register_executor (self )
94
- thread .start ()
95
+ # Mitigation to RuntimeError firing when thread starts on shutdown
96
+ # https://github.com/python/cpython/issues/114570
97
+ try :
98
+ thread .start ()
99
+ except RuntimeError as e :
100
+ if "interpreter shutdown" in str (e ) or sys .is_finalizing ():
101
+ self ._thread = None
102
+ return
103
+ raise
95
104
96
105
def close (self , dummy : Any = None ) -> None :
97
106
"""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
+ " " .join (command ), shell = True , 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