@@ -893,48 +893,34 @@ def test_only_active_thread(self):
893
893
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
894
894
sock.connect(('localhost', { port } ))
895
895
896
- def worker_thread(name, barrier, ready_event):
897
- barrier.wait() # Synchronize thread start
898
- ready_event.wait() # Wait for main thread signal
899
- # Sleep to keep thread alive
896
+ def sleeping_thread():
900
897
time.sleep(10_000)
901
898
902
- def main_work ():
903
- # Do busy work to hold the GIL
899
+ def busy_thread ():
900
+ # Busy loop to hold the GIL
904
901
sock.sendall(b"working\\ n")
905
902
count = 0
906
- while count < 100000000 :
903
+ while count < 100_000_000 :
907
904
count += 1
908
- if count % 10000000 == 0:
909
- pass # Keep main thread busy
910
905
sock.sendall(b"done\\ n")
911
906
912
- # Create synchronization primitives
913
- num_threads = 3
914
- barrier = threading.Barrier(num_threads + 1) # +1 for main thread
915
- ready_event = threading.Event()
907
+ # Start two sleeping threads
908
+ t1 = threading.Thread(target=sleeping_thread)
909
+ t2 = threading.Thread(target=sleeping_thread)
910
+ t1.start()
911
+ t2.start()
916
912
917
- # Start worker threads
918
- threads = []
919
- for i in range(num_threads):
920
- t = threading.Thread(target=worker_thread, args=(f"Worker-{{i}}", barrier, ready_event))
921
- t.start()
922
- threads.append(t)
923
-
924
- # Wait for all threads to be ready
925
- barrier.wait()
913
+ # Start busy thread
914
+ t3 = threading.Thread(target=busy_thread)
915
+ t3.start()
926
916
927
917
# Signal ready to parent process
928
918
sock.sendall(b"ready\\ n")
929
919
930
- # Signal threads to start waiting
931
- ready_event.set()
932
-
933
- # Give threads time to start sleeping
934
- time.sleep(0.1)
935
-
936
- # Now do busy work to hold the GIL
937
- main_work()
920
+ # Wait for threads to finish
921
+ t1.join()
922
+ t2.join()
923
+ t3.join()
938
924
"""
939
925
)
940
926
@@ -956,13 +942,9 @@ def main_work():
956
942
client_socket , _ = server_socket .accept ()
957
943
server_socket .close ()
958
944
959
- # Wait for ready signal
945
+ # Wait for ready signal and working signal
960
946
response = b""
961
- while b"ready" not in response :
962
- response += client_socket .recv (1024 )
963
-
964
- # Wait for the main thread to start its busy work
965
- while b"working" not in response :
947
+ while b"ready" not in response or b"working" not in response :
966
948
response += client_socket .recv (1024 )
967
949
968
950
# Get stack trace with all threads
@@ -984,8 +966,8 @@ def main_work():
984
966
p .terminate ()
985
967
p .wait (timeout = SHORT_TIMEOUT )
986
968
987
- # Verify we got multiple threads in all_traces
988
- self .assertGreater (len (all_traces ), 1 , "Should have multiple threads" )
969
+ # Verify we got 3 threads in all_traces
970
+ self .assertEqual (len (all_traces ), 4 , "Should have exactly 4 threads" )
989
971
990
972
# Verify we got exactly one thread in gil_traces
991
973
self .assertEqual (len (gil_traces ), 1 , "Should have exactly one GIL holder" )
@@ -996,6 +978,5 @@ def main_work():
996
978
self .assertIn (gil_thread_id , all_thread_ids ,
997
979
"GIL holder should be among all threads" )
998
980
999
-
1000
981
if __name__ == "__main__" :
1001
982
unittest .main ()
0 commit comments