@@ -1196,8 +1196,13 @@ def test_file_digest(self):
11961196 with open (os_helper .TESTFN , "wb" ) as f :
11971197 hashlib .file_digest (f , "sha256" )
11981198
1199- @unittest .skipUnless (support .check_sanitizer (thread = True ), "only meaningful on free-threading" )
1200- def test_gh_128657 (self ):
1199+ # There was a data race in py_digest_by_name() which caused tsan to
1200+ # complain and might sometimes cause an extra refcount in `PY_EVP_MD`
1201+ # structure if two threads attempted to create a `sha256()` at the same
1202+ # time. See gh-128657.
1203+ @unittest .skipUnless (support .Py_GIL_DISABLED ,
1204+ "only meaningful on free-threading" )
1205+ def test_py_digest_by_name_data_race (self ):
12011206 def test_hashlib_sha256 ():
12021207 hash_obj = hashlib .sha256 ()
12031208
@@ -1206,18 +1211,17 @@ def closure(barrier):
12061211 test_hashlib_sha256 ()
12071212
12081213 num_workers = 40
1209- num_runs = 20
1214+ barrier = threading . Barrier ( num_workers )
12101215
1211- for i in range ( num_runs ) :
1212- barrier = threading .Barrier ( num_workers )
1213- thrds = [ ]
1216+ with threading_helper . catch_threading_exception () as cm :
1217+ threads = [ threading .Thread ( target = closure , args = ( barrier ,) )
1218+ for _ in range ( num_workers ) ]
12141219
1215- for i in range (num_workers ):
1216- thrds .append (thrd := threading .Thread (target = closure , args = (barrier ,)))
1217- thrd .start ()
1220+ with threading_helper .start_threads (threads ):
1221+ pass
12181222
1219- for thrd in thrds :
1220- thrd . join ()
1223+ if cm . exc_value :
1224+ raise cm . exc_value
12211225
12221226
12231227if __name__ == "__main__" :
0 commit comments