@@ -888,54 +888,54 @@ def test_only_active_thread(self):
888888 script = textwrap .dedent (
889889 f"""\
890890 import time, sys, socket, threading
891-
891+
892892 # Connect to the test process
893893 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
894894 sock.connect(('localhost', { port } ))
895-
895+
896896 def worker_thread(name, barrier, ready_event):
897897 barrier.wait() # Synchronize thread start
898898 ready_event.wait() # Wait for main thread signal
899899 # Sleep to keep thread alive
900900 time.sleep(10_000)
901-
901+
902902 def main_work():
903903 # Do busy work to hold the GIL
904904 count = 0
905905 while count < 100000000:
906906 count += 1
907907 if count % 10000000 == 0:
908908 pass # Keep main thread busy
909-
909+
910910 # Create synchronization primitives
911911 num_threads = 3
912912 barrier = threading.Barrier(num_threads + 1) # +1 for main thread
913913 ready_event = threading.Event()
914-
914+
915915 # Start worker threads
916916 threads = []
917917 for i in range(num_threads):
918918 t = threading.Thread(target=worker_thread, args=(f"Worker-{{i}}", barrier, ready_event))
919919 t.start()
920920 threads.append(t)
921-
921+
922922 # Wait for all threads to be ready
923923 barrier.wait()
924-
924+
925925 # Signal ready to parent process
926926 sock.sendall(b"ready\\ n")
927-
927+
928928 # Signal threads to start waiting
929929 ready_event.set()
930-
930+
931931 # Give threads time to start sleeping
932932 time.sleep(0.1)
933-
933+
934934 # Now do busy work to hold the GIL
935935 main_work()
936936 """
937937 )
938-
938+
939939 with os_helper .temp_dir () as work_dir :
940940 script_dir = os .path .join (work_dir , "script_pkg" )
941941 os .mkdir (script_dir )
@@ -953,23 +953,23 @@ def main_work():
953953 p = subprocess .Popen ([sys .executable , script_name ])
954954 client_socket , _ = server_socket .accept ()
955955 server_socket .close ()
956-
956+
957957 # Wait for ready signal
958958 response = b""
959959 while b"ready" not in response :
960960 response += client_socket .recv (1024 )
961-
961+
962962 # Give threads a moment to start their busy work
963963 time .sleep (0.1 )
964-
964+
965965 # Get stack trace with all threads
966966 unwinder_all = RemoteUnwinder (p .pid , all_threads = True )
967967 all_traces = unwinder_all .get_stack_trace ()
968-
968+
969969 # Get stack trace with only GIL holder
970970 unwinder_gil = RemoteUnwinder (p .pid , only_active_thread = True )
971971 gil_traces = unwinder_gil .get_stack_trace ()
972-
972+
973973 except PermissionError :
974974 self .skipTest (
975975 "Insufficient permissions to read the stack trace"
@@ -980,17 +980,17 @@ def main_work():
980980 p .kill ()
981981 p .terminate ()
982982 p .wait (timeout = SHORT_TIMEOUT )
983-
983+
984984 # Verify we got multiple threads in all_traces
985985 self .assertGreater (len (all_traces ), 1 , "Should have multiple threads" )
986-
986+
987987 # Verify we got exactly one thread in gil_traces
988988 self .assertEqual (len (gil_traces ), 1 , "Should have exactly one GIL holder" )
989-
989+
990990 # The GIL holder should be in the all_traces list
991991 gil_thread_id = gil_traces [0 ][0 ]
992992 all_thread_ids = [trace [0 ] for trace in all_traces ]
993- self .assertIn (gil_thread_id , all_thread_ids ,
993+ self .assertIn (gil_thread_id , all_thread_ids ,
994994 "GIL holder should be among all threads" )
995995
996996
0 commit comments