@@ -893,48 +893,34 @@ def test_only_active_thread(self):
893893 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
894894 sock.connect(('localhost', { port } ))
895895
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():
900897 time.sleep(10_000)
901898
902- def main_work ():
903- # Do busy work to hold the GIL
899+ def busy_thread ():
900+ # Busy loop to hold the GIL
904901 sock.sendall(b"working\\ n")
905902 count = 0
906- while count < 100000000 :
903+ while count < 100_000_000 :
907904 count += 1
908- if count % 10000000 == 0:
909- pass # Keep main thread busy
910905 sock.sendall(b"done\\ n")
911906
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()
916912
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()
926916
927917 # Signal ready to parent process
928918 sock.sendall(b"ready\\ n")
929919
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()
938924 """
939925 )
940926
@@ -956,13 +942,9 @@ def main_work():
956942 client_socket , _ = server_socket .accept ()
957943 server_socket .close ()
958944
959- # Wait for ready signal
945+ # Wait for ready signal and working signal
960946 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 :
966948 response += client_socket .recv (1024 )
967949
968950 # Get stack trace with all threads
@@ -984,8 +966,8 @@ def main_work():
984966 p .terminate ()
985967 p .wait (timeout = SHORT_TIMEOUT )
986968
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" )
989971
990972 # Verify we got exactly one thread in gil_traces
991973 self .assertEqual (len (gil_traces ), 1 , "Should have exactly one GIL holder" )
@@ -996,6 +978,5 @@ def main_work():
996978 self .assertIn (gil_thread_id , all_thread_ids ,
997979 "GIL holder should be among all threads" )
998980
999-
1000981if __name__ == "__main__" :
1001982 unittest .main ()
0 commit comments