Context manager for running a target process with socket sync.
(self, script_body)
| 2706 | |
| 2707 | @contextmanager |
| 2708 | def _target_process(self, script_body): |
| 2709 | """Context manager for running a target process with socket sync.""" |
| 2710 | port = find_unused_port() |
| 2711 | script = f"""\ |
| 2712 | import socket |
| 2713 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 2714 | sock.connect(('localhost', {port})) |
| 2715 | {textwrap.dedent(script_body)} |
| 2716 | """ |
| 2717 | |
| 2718 | with os_helper.temp_dir() as work_dir: |
| 2719 | script_dir = os.path.join(work_dir, "script_pkg") |
| 2720 | os.mkdir(script_dir) |
| 2721 | |
| 2722 | server_socket = _create_server_socket(port) |
| 2723 | script_name = _make_test_script(script_dir, "script", script) |
| 2724 | client_socket = None |
| 2725 | |
| 2726 | try: |
| 2727 | with _managed_subprocess([sys.executable, script_name]) as p: |
| 2728 | client_socket, _ = server_socket.accept() |
| 2729 | server_socket.close() |
| 2730 | server_socket = None |
| 2731 | |
| 2732 | def make_unwinder(cache_frames=True): |
| 2733 | return RemoteUnwinder( |
| 2734 | p.pid, all_threads=True, cache_frames=cache_frames |
| 2735 | ) |
| 2736 | |
| 2737 | yield p, client_socket, make_unwinder |
| 2738 | finally: |
| 2739 | _cleanup_sockets(client_socket, server_socket) |
| 2740 | |
| 2741 | def _get_frames_with_retry(self, unwinder, required_funcs): |
| 2742 | """Get frames containing required_funcs, with retry for transient errors.""" |
no test coverage detected