Commit 79cb013
fix: allow users to avoid thread termination in scoped_released (#2657)
* Avoid thread termination in scoped_released
Do not call `PyEval_RestoreThread()` from `~gil_scoped_release()` if python runtime is finalizing, as it will result in thread termination in Python runtime newer than 3.6, as documented in https://docs.python.org/3/c-api/init.html#c.PyEval_RestoreThread
Similarly do not call `PyThreadState_DeleteCurrent` from `~gil_scoped_acquire()` if runtime is finalizing.
Discovered while debugging PyTorch crash using Python-3.9 described in pytorch/pytorch#47776
* Simplify _Py_IsFinalizing() availability check
* Fix typo
* Add version agnostic `detail::finalization_guard()`
* Move `finalization_guard` to detail/common.h
And rename it to `is_finalizing`
* Move `is_finalizing()` back to pybind11.h
* Simplify `is_finalizing()` check
One should follow documentation rather than make any assumptions
* feat: disarm
* docs: fix comment
Co-authored-by: Henry Schreiner <henryschreineriii@gmail.com>1 parent cecdfad commit 79cb013
1 file changed
+34
-4
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2113 | 2113 | | |
2114 | 2114 | | |
2115 | 2115 | | |
2116 | | - | |
| 2116 | + | |
| 2117 | + | |
2117 | 2118 | | |
2118 | 2119 | | |
2119 | 2120 | | |
2120 | 2121 | | |
2121 | 2122 | | |
| 2123 | + | |
| 2124 | + | |
| 2125 | + | |
| 2126 | + | |
| 2127 | + | |
| 2128 | + | |
| 2129 | + | |
| 2130 | + | |
| 2131 | + | |
2122 | 2132 | | |
2123 | 2133 | | |
2124 | 2134 | | |
| |||
2127 | 2137 | | |
2128 | 2138 | | |
2129 | 2139 | | |
| 2140 | + | |
2130 | 2141 | | |
2131 | 2142 | | |
2132 | 2143 | | |
| |||
2142 | 2153 | | |
2143 | 2154 | | |
2144 | 2155 | | |
| 2156 | + | |
| 2157 | + | |
| 2158 | + | |
| 2159 | + | |
| 2160 | + | |
| 2161 | + | |
| 2162 | + | |
| 2163 | + | |
| 2164 | + | |
| 2165 | + | |
2145 | 2166 | | |
2146 | 2167 | | |
2147 | 2168 | | |
2148 | | - | |
| 2169 | + | |
| 2170 | + | |
| 2171 | + | |
2149 | 2172 | | |
2150 | 2173 | | |
2151 | 2174 | | |
| |||
2154 | 2177 | | |
2155 | 2178 | | |
2156 | 2179 | | |
| 2180 | + | |
2157 | 2181 | | |
2158 | 2182 | | |
2159 | 2183 | | |
2160 | 2184 | | |
2161 | 2185 | | |
2162 | 2186 | | |
2163 | 2187 | | |
| 2188 | + | |
2164 | 2189 | | |
2165 | 2190 | | |
2166 | 2191 | | |
2167 | 2192 | | |
2168 | 2193 | | |
2169 | 2194 | | |
2170 | 2195 | | |
| 2196 | + | |
2171 | 2197 | | |
2172 | 2198 | | |
2173 | | - | |
2174 | | - | |
| 2199 | + | |
| 2200 | + | |
| 2201 | + | |
| 2202 | + | |
| 2203 | + | |
| 2204 | + | |
2175 | 2205 | | |
2176 | 2206 | | |
2177 | 2207 | | |
| |||
0 commit comments