gemini-code-assist[bot] commented on code in PR #19736:
URL: https://github.com/apache/tvm/pull/19736#discussion_r3399100952


##########
python/tvm/testing/disco.py:
##########
@@ -0,0 +1,136 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""Testing utilities for the Disco distributed runtime."""
+
+# Defer annotation evaluation: `tvm.runtime.disco` is None on builds without
+# the disco runtime, and this module must still be importable there.
+from __future__ import annotations
+
+import socket
+import subprocess
+import sys
+import threading
+
+from tvm.runtime import disco as di
+
+_SOCKET_SESSION_TESTER = None
+
+
+def _get_free_port() -> int:
+    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+    sock.bind(("", 0))
+    port = sock.getsockname()[1]
+    sock.close()
+    return port
+
+
+class SocketSessionTester:
+    """Run a disco SocketSession with one local node and remote nodes.
+
+    Each remote node is a `tvm.exec.disco_remote_socket_session` subprocess
+    launched with the current Python interpreter.
+    """
+
+    def __init__(self, num_workers, num_nodes=2, num_groups=1):
+        # Initialize the attributes used by __del__ first, so that teardown is
+        # safe even when __init__ raises below.
+        self.sess = None
+        self.remote_nodes = []
+        assert num_workers % num_nodes == 0
+        num_workers_per_node = num_workers // num_nodes
+        server_host = "localhost"
+        server_port = _get_free_port()
+        server_exc = []
+
+        def start_server():
+            try:
+                self.sess = di.SocketSession(
+                    num_nodes, num_workers_per_node, num_groups, server_host, 
server_port
+                )
+            except Exception as exc:  # pylint: disable=broad-except
+                server_exc.append(exc)
+
+        thread = threading.Thread(target=start_server)
+        thread.start()
+
+        cmd = "tvm.exec.disco_remote_socket_session"
+        for _i in range(num_nodes - 1):
+            self.remote_nodes.append(
+                subprocess.Popen(
+                    [
+                        sys.executable,
+                        "-m",
+                        cmd,
+                        server_host,
+                        str(server_port),
+                        str(num_workers_per_node),
+                    ],
+                    stdout=sys.stdout,
+                    stderr=sys.stderr,
+                )
+            )
+
+        thread.join()
+        if server_exc:
+            raise server_exc[0]

Review Comment:
   ![high](https://www.gstatic.com/codereviewagent/high-priority.svg)
   
   If any of the remote node subprocesses fail to start or crash immediately 
(e.g., due to import errors or missing dependencies), the `SocketSession` 
server will block indefinitely waiting for connections. A bare `thread.join()` 
will then hang the entire test suite. Adding a timeout to `thread.join()` and 
cleaning up the launched subprocesses on timeout prevents indefinite hangs in 
CI.
   
   ```python
           thread.join(timeout=60)
           if thread.is_alive():
               for node in self.remote_nodes:
                   node.kill()
                   node.wait()
               raise RuntimeError(
                   f'SocketSession server thread timed out after 60 seconds 
waiting for '
                   f'{num_nodes - 1} remote node(s) to connect.'
               )
           if server_exc:
               raise server_exc[0]
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to