Debian Codegraph @

Modules/_testinternalcapi/test_critical_sections.c

raw
1 /*
2 * C Extension module to test pycore_critical_section.h API.
3 */
4
5 #include "parts.h"
6 #include "pycore_critical_section.h"
7 #include "pycore_pystate.h"
8 #include "pycore_pythread.h"
9
10 #ifdef MS_WINDOWS
11 # include <windows.h> // Sleep()
12 #endif
13
14 #ifdef Py_GIL_DISABLED
15 #define assert_nogil assert
16 #define assert_gil(x)
17 #else
18 #define assert_gil assert
19 #define assert_nogil(x)
20 #endif
21
22
23 static PyObject *
24 test_critical_sections(PyObject *self, PyObject *Py_UNUSED(args))
25 {
26 PyObject *d1 = PyDict_New();
27 assert(d1 != NULL);
28
29 PyObject *d2 = PyDict_New();
30 assert(d2 != NULL);
31
32 // Beginning a critical section should lock the associated object and
33 // push the critical section onto the thread's stack (in Py_GIL_DISABLED builds).
34 Py_BEGIN_CRITICAL_SECTION(d1);
35 assert_nogil(PyMutex_IsLocked(&d1->ob_mutex));
36 assert_nogil(_PyCriticalSection_IsActive(PyThreadState_GET()->critical_section));
37 assert_gil(PyThreadState_GET()->critical_section == 0);
38 Py_END_CRITICAL_SECTION();
39 assert_nogil(!PyMutex_IsLocked(&d1->ob_mutex));
40
41 assert_nogil(!PyMutex_IsLocked(&d1->ob_mutex));
42 assert_nogil(!PyMutex_IsLocked(&d2->ob_mutex));
43 Py_BEGIN_CRITICAL_SECTION2(d1, d2);
44 assert_nogil(PyMutex_IsLocked(&d1->ob_mutex));
45 assert_nogil(PyMutex_IsLocked(&d2->ob_mutex));
46 Py_END_CRITICAL_SECTION2();
47 assert_nogil(!PyMutex_IsLocked(&d1->ob_mutex));
48 assert_nogil(!PyMutex_IsLocked(&d2->ob_mutex));
49
50 // Passing the same object twice should work (and not deadlock).
51 assert_nogil(!PyMutex_IsLocked(&d2->ob_mutex));
52 Py_BEGIN_CRITICAL_SECTION2(d2, d2);
53 assert_nogil(PyMutex_IsLocked(&d2->ob_mutex));
54 Py_END_CRITICAL_SECTION2();
55 assert_nogil(!PyMutex_IsLocked(&d2->ob_mutex));
56
57 Py_DECREF(d2);
58 Py_DECREF(d1);
59 Py_RETURN_NONE;
60 }
61
62 static void
63 lock_unlock_object(PyObject *obj, int recurse_depth)
64 {
65 Py_BEGIN_CRITICAL_SECTION(obj);
66 if (recurse_depth > 0) {
67 lock_unlock_object(obj, recurse_depth - 1);
68 }
69 Py_END_CRITICAL_SECTION();
70 }
71
72 static void
73 lock_unlock_two_objects(PyObject *a, PyObject *b, int recurse_depth)
74 {
75 Py_BEGIN_CRITICAL_SECTION2(a, b);
76 if (recurse_depth > 0) {
77 lock_unlock_two_objects(a, b, recurse_depth - 1);
78 }
79 Py_END_CRITICAL_SECTION2();
80 }
81
82
83 // Test that nested critical sections do not deadlock if they attempt to lock
84 // the same object.
85 static PyObject *
86 test_critical_sections_nest(PyObject *self, PyObject *Py_UNUSED(args))
87 {
88 PyObject *a = PyDict_New();
89 assert(a != NULL);
90 PyObject *b = PyDict_New();
91 assert(b != NULL);
92
93 // Locking an object recursively with this API should not deadlock.
94 assert_nogil(!PyMutex_IsLocked(&a->ob_mutex));
95 Py_BEGIN_CRITICAL_SECTION(a);
96 assert_nogil(PyMutex_IsLocked(&a->ob_mutex));
97 lock_unlock_object(a, 10);
98 assert_nogil(PyMutex_IsLocked(&a->ob_mutex));
99 Py_END_CRITICAL_SECTION();
100 assert_nogil(!PyMutex_IsLocked(&a->ob_mutex));
101
102 // Same test but with two objects.
103 Py_BEGIN_CRITICAL_SECTION2(b, a);
104 lock_unlock_two_objects(a, b, 10);
105 assert_nogil(PyMutex_IsLocked(&a->ob_mutex));
106 assert_nogil(PyMutex_IsLocked(&b->ob_mutex));
107 Py_END_CRITICAL_SECTION2();
108
109 Py_DECREF(b);
110 Py_DECREF(a);
111 Py_RETURN_NONE;
112 }
113
114 // Test that a critical section is suspended by a Py_BEGIN_ALLOW_THREADS and
115 // resumed by a Py_END_ALLOW_THREADS.
116 static PyObject *
117 test_critical_sections_suspend(PyObject *self, PyObject *Py_UNUSED(args))
118 {
119 PyObject *a = PyDict_New();
120 assert(a != NULL);
121
122 Py_BEGIN_CRITICAL_SECTION(a);
123 assert_nogil(PyMutex_IsLocked(&a->ob_mutex));
124
125 // Py_BEGIN_ALLOW_THREADS should suspend the active critical section
126 Py_BEGIN_ALLOW_THREADS
127 assert_nogil(!PyMutex_IsLocked(&a->ob_mutex));
128 Py_END_ALLOW_THREADS;
129
130 // After Py_END_ALLOW_THREADS the critical section should be resumed.
131 assert_nogil(PyMutex_IsLocked(&a->ob_mutex));
132 Py_END_CRITICAL_SECTION();
133
134 Py_DECREF(a);
135 Py_RETURN_NONE;
136 }
137
138 #ifdef Py_CAN_START_THREADS
139 struct test_data {
140 PyObject *obj1;
141 PyObject *obj2;
142 PyObject *obj3;
143 Py_ssize_t countdown;
144 PyEvent done_event;
145 };
146
147 static void
148 thread_critical_sections(void *arg)
149 {
150 const Py_ssize_t NUM_ITERS = 200;
151 struct test_data *test_data = arg;
152 PyGILState_STATE gil = PyGILState_Ensure();
153
154 for (Py_ssize_t i = 0; i < NUM_ITERS; i++) {
155 Py_BEGIN_CRITICAL_SECTION(test_data->obj1);
156 Py_END_CRITICAL_SECTION();
157
158 Py_BEGIN_CRITICAL_SECTION(test_data->obj2);
159 lock_unlock_object(test_data->obj1, 1);
160 Py_END_CRITICAL_SECTION();
161
162 Py_BEGIN_CRITICAL_SECTION2(test_data->obj3, test_data->obj1);
163 lock_unlock_object(test_data->obj2, 2);
164 Py_END_CRITICAL_SECTION2();
165
166 Py_BEGIN_CRITICAL_SECTION(test_data->obj3);
167 Py_BEGIN_ALLOW_THREADS
168 Py_END_ALLOW_THREADS
169 Py_END_CRITICAL_SECTION();
170 }
171
172 PyGILState_Release(gil);
173 if (_Py_atomic_add_ssize(&test_data->countdown, -1) == 1) {
174 // last thread to finish sets done_event
175 _PyEvent_Notify(&test_data->done_event);
176 }
177 }
178
179 static PyObject *
180 test_critical_sections_threads(PyObject *self, PyObject *Py_UNUSED(args))
181 {
182 const Py_ssize_t NUM_THREADS = 4;
183 struct test_data test_data = {
184 .obj1 = PyDict_New(),
185 .obj2 = PyDict_New(),
186 .obj3 = PyDict_New(),
187 .countdown = NUM_THREADS,
188 };
189 assert(test_data.obj1 != NULL);
190 assert(test_data.obj2 != NULL);
191 assert(test_data.obj3 != NULL);
192
193 for (Py_ssize_t i = 0; i < NUM_THREADS; i++) {
194 PyThread_start_new_thread(&thread_critical_sections, &test_data);
195 }
196 PyEvent_Wait(&test_data.done_event);
197
198 Py_DECREF(test_data.obj3);
199 Py_DECREF(test_data.obj2);
200 Py_DECREF(test_data.obj1);
201 Py_RETURN_NONE;
202 }
203
204 static void
205 pysleep(int ms)
206 {
207 #ifdef MS_WINDOWS
208 Sleep(ms);
209 #else
210 usleep(ms * 1000);
211 #endif
212 }
213
214 struct test_data_gc {
215 PyObject *obj;
216 Py_ssize_t num_threads;
217 Py_ssize_t id;
218 Py_ssize_t countdown;
219 PyEvent done_event;
220 PyEvent ready;
221 };
222
223 static void
224 thread_gc(void *arg)
225 {
226 struct test_data_gc *test_data = arg;
227 PyGILState_STATE gil = PyGILState_Ensure();
228
229 Py_ssize_t id = _Py_atomic_add_ssize(&test_data->id, 1);
230 if (id == test_data->num_threads - 1) {
231 _PyEvent_Notify(&test_data->ready);
232 }
233 else {
234 // wait for all test threads to more reliably reproduce the issue.
235 PyEvent_Wait(&test_data->ready);
236 }
237
238 if (id == 0) {
239 Py_BEGIN_CRITICAL_SECTION(test_data->obj);
240 // pause long enough that the lock would be handed off directly to
241 // a waiting thread.
242 pysleep(5);
243 PyGC_Collect();
244 Py_END_CRITICAL_SECTION();
245 }
246 else if (id == 1) {
247 pysleep(1);
248 Py_BEGIN_CRITICAL_SECTION(test_data->obj);
249 pysleep(1);
250 Py_END_CRITICAL_SECTION();
251 }
252 else if (id == 2) {
253 // sleep long enough so that thread 0 is waiting to stop the world
254 pysleep(6);
255 Py_BEGIN_CRITICAL_SECTION(test_data->obj);
256 pysleep(1);
257 Py_END_CRITICAL_SECTION();
258 }
259
260 PyGILState_Release(gil);
261 if (_Py_atomic_add_ssize(&test_data->countdown, -1) == 1) {
262 // last thread to finish sets done_event
263 _PyEvent_Notify(&test_data->done_event);
264 }
265 }
266
267 static PyObject *
268 test_critical_sections_gc(PyObject *self, PyObject *Py_UNUSED(args))
269 {
270 // gh-118332: Contended critical sections should not deadlock with GC
271 const Py_ssize_t NUM_THREADS = 3;
272 struct test_data_gc test_data = {
273 .obj = PyDict_New(),
274 .countdown = NUM_THREADS,
275 .num_threads = NUM_THREADS,
276 };
277 assert(test_data.obj != NULL);
278
279 for (Py_ssize_t i = 0; i < NUM_THREADS; i++) {
280 PyThread_start_new_thread(&thread_gc, &test_data);
281 }
282 PyEvent_Wait(&test_data.done_event);
283 Py_DECREF(test_data.obj);
284 Py_RETURN_NONE;
285 }
286
287 #endif
288
289 #ifdef Py_GIL_DISABLED
290
291 static PyObject *
292 test_critical_section1_reacquisition(PyObject *self, PyObject *Py_UNUSED(args))
293 {
294 PyObject *a = PyDict_New();
295 assert(a != NULL);
296
297 PyCriticalSection cs1, cs2;
298 // First acquisition of critical section on object locks it
299 PyCriticalSection_Begin(&cs1, a);
300 assert(PyMutex_IsLocked(&a->ob_mutex));
301 assert(_PyCriticalSection_IsActive(PyThreadState_GET()->critical_section));
302 assert(_PyThreadState_GET()->critical_section == (uintptr_t)&cs1);
303 // Attempting to re-acquire critical section on same object which
304 // is already locked by top-most critical section is a no-op.
305 PyCriticalSection_Begin(&cs2, a);
306 assert(PyMutex_IsLocked(&a->ob_mutex));
307 assert(_PyCriticalSection_IsActive(PyThreadState_GET()->critical_section));
308 assert(_PyThreadState_GET()->critical_section == (uintptr_t)&cs1);
309 // Releasing second critical section is a no-op.
310 PyCriticalSection_End(&cs2);
311 assert(PyMutex_IsLocked(&a->ob_mutex));
312 assert(_PyCriticalSection_IsActive(PyThreadState_GET()->critical_section));
313 assert(_PyThreadState_GET()->critical_section == (uintptr_t)&cs1);
314 // Releasing first critical section unlocks the object
315 PyCriticalSection_End(&cs1);
316 assert(!PyMutex_IsLocked(&a->ob_mutex));
317
318 Py_DECREF(a);
319 Py_RETURN_NONE;
320 }
321
322 static PyObject *
323 test_critical_section2_reacquisition(PyObject *self, PyObject *Py_UNUSED(args))
324 {
325 PyObject *a = PyDict_New();
326 assert(a != NULL);
327 PyObject *b = PyDict_New();
328 assert(b != NULL);
329
330 PyCriticalSection2 cs;
331 // First acquisition of critical section on objects locks them
332 PyCriticalSection2_Begin(&cs, a, b);
333 assert(PyMutex_IsLocked(&a->ob_mutex));
334 assert(PyMutex_IsLocked(&b->ob_mutex));
335 assert(_PyCriticalSection_IsActive(PyThreadState_GET()->critical_section));
336 assert((_PyThreadState_GET()->critical_section &
337 ~_Py_CRITICAL_SECTION_MASK) == (uintptr_t)&cs);
338
339 // Attempting to re-acquire critical section on either of two
340 // objects already locked by top-most critical section is a no-op.
341
342 // Check re-acquiring on first object
343 PyCriticalSection a_cs;
344 PyCriticalSection_Begin(&a_cs, a);
345 assert(PyMutex_IsLocked(&a->ob_mutex));
346 assert(PyMutex_IsLocked(&b->ob_mutex));
347 assert(_PyCriticalSection_IsActive(PyThreadState_GET()->critical_section));
348 assert((_PyThreadState_GET()->critical_section &
349 ~_Py_CRITICAL_SECTION_MASK) == (uintptr_t)&cs);
350 // Releasing critical section on either object is a no-op.
351 PyCriticalSection_End(&a_cs);
352 assert(PyMutex_IsLocked(&a->ob_mutex));
353 assert(PyMutex_IsLocked(&b->ob_mutex));
354 assert(_PyCriticalSection_IsActive(PyThreadState_GET()->critical_section));
355 assert((_PyThreadState_GET()->critical_section &
356 ~_Py_CRITICAL_SECTION_MASK) == (uintptr_t)&cs);
357
358 // Check re-acquiring on second object
359 PyCriticalSection b_cs;
360 PyCriticalSection_Begin(&b_cs, b);
361 assert(PyMutex_IsLocked(&a->ob_mutex));
362 assert(PyMutex_IsLocked(&b->ob_mutex));
363 assert(_PyCriticalSection_IsActive(PyThreadState_GET()->critical_section));
364 assert((_PyThreadState_GET()->critical_section &
365 ~_Py_CRITICAL_SECTION_MASK) == (uintptr_t)&cs);
366 // Releasing critical section on either object is a no-op.
367 PyCriticalSection_End(&b_cs);
368 assert(PyMutex_IsLocked(&a->ob_mutex));
369 assert(PyMutex_IsLocked(&b->ob_mutex));
370 assert(_PyCriticalSection_IsActive(PyThreadState_GET()->critical_section));
371 assert((_PyThreadState_GET()->critical_section &
372 ~_Py_CRITICAL_SECTION_MASK) == (uintptr_t)&cs);
373
374 // Releasing critical section on both objects unlocks them
375 PyCriticalSection2_End(&cs);
376 assert(!PyMutex_IsLocked(&a->ob_mutex));
377 assert(!PyMutex_IsLocked(&b->ob_mutex));
378
379 Py_DECREF(a);
380 Py_DECREF(b);
381 Py_RETURN_NONE;
382 }
383
384 #endif // Py_GIL_DISABLED
385
386 #ifdef Py_CAN_START_THREADS
387
388 // gh-144513: Test that critical sections don't deadlock with stop-the-world.
389 // This test is designed to deadlock (timeout) on builds without the fix.
390 struct test_data_stw {
391 PyObject *obj;
392 Py_ssize_t num_threads;
393 Py_ssize_t started;
394 PyEvent ready;
395 };
396
397 static void
398 thread_stw(void *arg)
399 {
400 struct test_data_stw *test_data = arg;
401 PyGILState_STATE gil = PyGILState_Ensure();
402
403 if (_Py_atomic_add_ssize(&test_data->started, 1) == test_data->num_threads - 1) {
404 _PyEvent_Notify(&test_data->ready);
405 }
406
407 // All threads: acquire critical section and hold it long enough to
408 // trigger TIME_TO_BE_FAIR_NS (1 ms), which causes direct handoff on unlock.
409 Py_BEGIN_CRITICAL_SECTION(test_data->obj);
410 pysleep(10); // 10 ms = 10 x TIME_TO_BE_FAIR_NS
411 Py_END_CRITICAL_SECTION();
412
413 PyGILState_Release(gil);
414 }
415
416 static PyObject *
417 test_critical_sections_stw(PyObject *self, PyObject *Py_UNUSED(args))
418 {
419 // gh-144513: Test that critical sections don't deadlock during STW.
420 //
421 // The deadlock occurs when lock ownership is handed off (due to fairness
422 // after TIME_TO_BE_FAIR_NS) to a thread that has already suspended for
423 // stop-the-world. The STW requester then cannot acquire the lock.
424 //
425 // With the fix, the STW requester detects world_stopped and skips locking.
426
427 #define STW_NUM_THREADS 2
428 struct test_data_stw test_data = {
429 .obj = PyDict_New(),
430 .num_threads = STW_NUM_THREADS,
431 };
432 if (test_data.obj == NULL) {
433 return NULL;
434 }
435
436 PyThread_handle_t handles[STW_NUM_THREADS];
437 PyThread_ident_t idents[STW_NUM_THREADS];
438 for (Py_ssize_t i = 0; i < STW_NUM_THREADS; i++) {
439 PyThread_start_joinable_thread(&thread_stw, &test_data,
440 &idents[i], &handles[i]);
441 }
442
443 // Wait for threads to start, then let them compete for the lock
444 PyEvent_Wait(&test_data.ready);
445 pysleep(5);
446
447 // Request stop-the-world and try to acquire the critical section.
448 // Without the fix, this may deadlock.
449 PyInterpreterState *interp = PyInterpreterState_Get();
450 _PyEval_StopTheWorld(interp);
451
452 Py_BEGIN_CRITICAL_SECTION(test_data.obj);
453 Py_END_CRITICAL_SECTION();
454
455 _PyEval_StartTheWorld(interp);
456
457 for (Py_ssize_t i = 0; i < STW_NUM_THREADS; i++) {
458 PyThread_join_thread(handles[i]);
459 }
460 #undef STW_NUM_THREADS
461 Py_DECREF(test_data.obj);
462 Py_RETURN_NONE;
463 }
464
465 #endif // Py_CAN_START_THREADS
466
467 static PyMethodDef test_methods[] = {
468 {"test_critical_sections", test_critical_sections, METH_NOARGS},
469 {"test_critical_sections_nest", test_critical_sections_nest, METH_NOARGS},
470 {"test_critical_sections_suspend", test_critical_sections_suspend, METH_NOARGS},
471 #ifdef Py_GIL_DISABLED
472 {"test_critical_section1_reacquisition", test_critical_section1_reacquisition, METH_NOARGS},
473 {"test_critical_section2_reacquisition", test_critical_section2_reacquisition, METH_NOARGS},
474 #endif
475 #ifdef Py_CAN_START_THREADS
476 {"test_critical_sections_threads", test_critical_sections_threads, METH_NOARGS},
477 {"test_critical_sections_gc", test_critical_sections_gc, METH_NOARGS},
478 {"test_critical_sections_stw", test_critical_sections_stw, METH_NOARGS},
479 #endif
480 {NULL, NULL} /* sentinel */
481 };
482
483 int
484 _PyTestInternalCapi_Init_CriticalSection(PyObject *mod)
485 {
486 if (PyModule_AddFunctions(mod, test_methods) < 0) {
487 return -1;
488 }
489 return 0;
490 }