| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent e0d6cc8 commit 6ff5471
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,5 @@ | |||
| 1 | + At Python exit, sometimes a thread holding the GIL can wait forever for a | ||
| 2 | + thread (usually a daemon thread) which requested to drop the GIL, whereas | ||
| 3 | + the thread already exited. To fix the race condition, the thread which | ||
| 4 | + requested the GIL drop now resets its request before exiting. Issue | ||
| 5 | + discovered and analyzed by Mingliang ZHAO. Patch by Victor Stinner. | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -239,6 +239,7 @@ take_gil(PyThreadState *tstate) | |||
| 239 | 239 | goto _ready; | |
| 240 | 240 | } | |
| 241 | 241 | ||
| 242 | + int drop_requested = 0; | ||
| 242 | 243 | while (_Py_atomic_load_relaxed(&gil->locked)) { | |
| 243 | 244 | unsigned long saved_switchnum = gil->switch_number; | |
| 244 | 245 | ||
@@ -254,11 +255,21 @@ take_gil(PyThreadState *tstate) | |||
| 254 | 255 | { | |
| 255 | 256 | if (tstate_must_exit(tstate)) { | |
| 256 | 257 | MUTEX_UNLOCK(gil->mutex); | |
| 258 | + // gh-96387: If the loop requested a drop request in a previous | ||
| 259 | + // iteration, reset the request. Otherwise, drop_gil() can | ||
| 260 | + // block forever waiting for the thread which exited. Drop | ||
| 261 | + // requests made by other threads are also reset: these threads | ||
| 262 | + // may have to request again a drop request (iterate one more | ||
| 263 | + // time). | ||
| 264 | + if (drop_requested) { | ||
| 265 | + RESET_GIL_DROP_REQUEST(interp); | ||
| 266 | + } | ||
| 257 | 267 | PyThread_exit_thread(); | |
| 258 | 268 | } | |
| 259 | 269 | assert(is_tstate_valid(tstate)); | |
| 260 | 270 | ||
| 261 | 271 | SET_GIL_DROP_REQUEST(interp); | |
| 272 | + drop_requested = 1; | ||
| 262 | 273 | } | |
| 263 | 274 | } | |
| 264 | 275 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments