@@ -1981,17 +1981,17 @@ def _run_remote_exec_test(self, script_code, python_args=None, env=None, prologu
19811981 target = os_helper .TESTFN + '_target.py'
19821982 self .addCleanup (os_helper .unlink , target )
19831983
1984- parent_sock , child_sock = socket . socketpair ()
1984+ port = find_unused_port ()
19851985
19861986 with open (target , 'w' ) as f :
19871987 f .write (f'''
19881988import sys
19891989import time
19901990import socket
1991- import os
19921991
1993- # Get the socket from the passed file descriptor
1994- sock = socket.socket(fileno={ child_sock .fileno ()} )
1992+ # Connect to the test process
1993+ sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
1994+ sock.connect(('localhost', { port } ))
19951995
19961996# Signal that the process is ready
19971997sock.sendall(b"ready")
@@ -2017,28 +2017,34 @@ def _run_remote_exec_test(self, script_code, python_args=None, env=None, prologu
20172017 cmd .extend (python_args )
20182018 cmd .append (target )
20192019
2020+ # Create a socket server to communicate with the target process
2021+ server_socket = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
2022+ server_socket .bind (('localhost' , port ))
2023+ server_socket .settimeout (SHORT_TIMEOUT )
2024+ server_socket .listen (1 )
2025+
20202026 with subprocess .Popen (cmd ,
20212027 stdout = subprocess .PIPE ,
20222028 stderr = subprocess .PIPE ,
20232029 env = env ,
2024- pass_fds = [child_sock .fileno ()],
20252030 ) as proc :
2031+ client_socket = None
20262032 try :
2027- # Close the child socket in the parent process as it's now owned by the child
2028- child_sock .close ()
2033+ # Accept connection from target process
2034+ client_socket , _ = server_socket .accept ()
2035+ server_socket .close ()
20292036
2030- # Wait for process to be ready
2031- response = parent_sock .recv (1024 )
2037+ response = client_socket .recv (1024 )
20322038 self .assertEqual (response , b"ready" )
20332039
20342040 # Try remote exec on the target process
20352041 sys .remote_exec (proc .pid , script )
20362042
20372043 # Signal script to continue
2038- parent_sock .sendall (b"continue" )
2044+ client_socket .sendall (b"continue" )
20392045
20402046 # Wait for execution confirmation
2041- response = parent_sock .recv (1024 )
2047+ response = client_socket .recv (1024 )
20422048 self .assertEqual (response , b"executed" )
20432049
20442050 # Return output for test verification
@@ -2047,8 +2053,8 @@ def _run_remote_exec_test(self, script_code, python_args=None, env=None, prologu
20472053 except PermissionError :
20482054 self .skipTest ("Insufficient permissions to execute code in remote process" )
20492055 finally :
2050- # Wait for execution confirmation
2051- parent_sock .close ()
2056+ if client_socket is not None :
2057+ client_socket .close ()
20522058 proc .kill ()
20532059 proc .terminate ()
20542060 proc .wait (timeout = SHORT_TIMEOUT )
0 commit comments