Skip to content

Commit 4c91955

Browse files
committed
Retry connecting to make it less sensitive to order of starting the two instances.
PiperOrigin-RevId: 211514400
1 parent 71b9ea4 commit 4c91955

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

pysc2/env/lan_sc2_env.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@
3030
import struct
3131
import subprocess
3232
import threading
33+
import time
3334

35+
from future.builtins import range # pylint: disable=redefined-builtin
3436
from pysc2 import run_configs
3537
from pysc2.env import sc2_env
3638
from pysc2.lib import run_parallel
@@ -82,8 +84,15 @@ def tcp_client(tcp_addr):
8284
"""Connect to the tcp server, and return the settings."""
8385
family = socket.AF_INET6 if ":" in tcp_addr.ip else socket.AF_INET
8486
sock = socket.socket(family, socket.SOCK_STREAM, socket.IPPROTO_TCP)
85-
logging.info("Connecting to: %s", tcp_addr)
86-
sock.connect(tcp_addr)
87+
for i in range(300):
88+
logging.info("Connecting to: %s, attempt %d", tcp_addr, i)
89+
try:
90+
sock.connect(tcp_addr)
91+
break
92+
except socket.error:
93+
time.sleep(1)
94+
else:
95+
sock.connect(tcp_addr) # One last try, but don't catch this error.
8796
logging.info("Connected.")
8897

8998
map_data = read_tcp(sock)

0 commit comments

Comments
 (0)