File tree Expand file tree Collapse file tree 2 files changed +33
-2
lines changed
Expand file tree Collapse file tree 2 files changed +33
-2
lines changed Original file line number Diff line number Diff line change 66from test .support .import_helper import import_module
77from test .support .script_helper import assert_python_failure , assert_python_ok
88import pickle
9+ from threading import Thread
910import unittest
1011
1112class ListTest (list_tests .CommonTest ):
@@ -381,6 +382,30 @@ def foo(x):
381382
382383 self .assertEqual (foo (list (range (10 ))), 45 )
383384
385+ # gh-145036: race condition with list.__sizeof__()
386+ def test_list_sizeof_free_threaded_build (self ):
387+ L = []
388+
389+ def test1 ():
390+ for _ in range (100 ):
391+ L .append (1 )
392+ L .pop ()
393+
394+ def test2 ():
395+ for _ in range (100 ):
396+ L .__sizeof__ ()
397+
398+ threads = []
399+ for _ in range (4 ):
400+ threads .append (Thread (target = test1 ))
401+ threads .append (Thread (target = test2 ))
402+
403+ for t in threads :
404+ t .start ()
405+ for t in threads :
406+ t .join ()
407+
408+ self .assertEqual (len (L ), 0 )
384409
385410if __name__ == "__main__" :
386411 unittest .main ()
Original file line number Diff line number Diff line change @@ -3558,8 +3558,14 @@ list___sizeof___impl(PyListObject *self)
35583558/*[clinic end generated code: output=3417541f95f9a53e input=b8030a5d5ce8a187]*/
35593559{
35603560 size_t res = _PyObject_SIZE (Py_TYPE (self ));
3561- Py_ssize_t allocated = FT_ATOMIC_LOAD_SSIZE_RELAXED (self -> allocated );
3562- res += (size_t )allocated * sizeof (void * );
3561+ #ifdef Py_GIL_DISABLED
3562+ PyObject * * ob_item = _Py_atomic_load_ptr (& self -> ob_item );
3563+ if (ob_item != NULL ) {
3564+ res += list_capacity (ob_item ) * sizeof (PyObject * );
3565+ }
3566+ #else
3567+ res += (size_t )self -> allocated * sizeof (PyObject * );
3568+ #endif
35633569 return PyLong_FromSize_t (res );
35643570}
35653571
You can’t perform that action at this time.
0 commit comments