`ray.init()` fails with `Operation timed out` from redis connection
I am using Avast VPN on my iMac running Catalina 10.15.2.
If the VPN is in use when I run the following code, the initialization attempt fails --- it times out after about 30 seconds
import ray
print(f'Attempting to initialize Ray')
ray.init()
print(f'Ray initialized')
with the following output:
Attempting to initialize Ray
2019-12-21 10:35:36,660 INFO resource_spec.py:216 -- Starting Ray with 3.52 GiB memory available for workers and up to 1.77 GiB for objects. You can adjust these settings with ray.init(memory=<bytes>, object_store_memory=<bytes>).
Traceback (most recent call last):
File "/Users/bob/Dropbox/GitHub-repos/Ray-experiments/venv/lib/python3.7/site-packages/redis/connection.py", line 539, in connect
sock = self._connect()
File "/Users/bob/Dropbox/GitHub-repos/Ray-experiments/venv/lib/python3.7/site-packages/redis/connection.py", line 596, in _connect
raise err
File "/Users/bob/Dropbox/GitHub-repos/Ray-experiments/venv/lib/python3.7/site-packages/redis/connection.py", line 584, in _connect
sock.connect(socket_address)
TimeoutError: [Errno 60] Operation timed out
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/bob/Dropbox/GitHub-repos/Ray-experiments/src/first-try.py", line 26, in <module>
ray.init()
File "/Users/bob/Dropbox/GitHub-repos/Ray-experiments/venv/lib/python3.7/site-packages/ray/worker.py", line 734, in init
ray_params=ray_params)
File "/Users/bob/Dropbox/GitHub-repos/Ray-experiments/venv/lib/python3.7/site-packages/ray/node.py", line 168, in __init__
self.start_head_processes()
File "/Users/bob/Dropbox/GitHub-repos/Ray-experiments/venv/lib/python3.7/site-packages/ray/node.py", line 593, in start_head_processes
self.start_redis()
File "/Users/bob/Dropbox/GitHub-repos/Ray-experiments/venv/lib/python3.7/site-packages/ray/node.py", line 447, in start_redis
include_java=self._ray_params.include_java)
File "/Users/bob/Dropbox/GitHub-repos/Ray-experiments/venv/lib/python3.7/site-packages/ray/services.py", line 712, in start_redis
primary_redis_client.set("NumRedisShards", str(num_redis_shards))
File "/Users/bob/Dropbox/GitHub-repos/Ray-experiments/venv/lib/python3.7/site-packages/redis/client.py", line 1519, in set
return self.execute_command('SET', *pieces)
File "/Users/bob/Dropbox/GitHub-repos/Ray-experiments/venv/lib/python3.7/site-packages/redis/client.py", line 836, in execute_command
conn = self.connection or pool.get_connection(command_name, **options)
File "/Users/bob/Dropbox/GitHub-repos/Ray-experiments/venv/lib/python3.7/site-packages/redis/connection.py", line 1073, in get_connection
connection.connect()
File "/Users/bob/Dropbox/GitHub-repos/Ray-experiments/venv/lib/python3.7/site-packages/redis/connection.py", line 544, in connect
raise ConnectionError(self._error_message(e))
redis.exceptions.ConnectionError: Error 60 connecting to 100.98.84.194:30561. Operation timed out.
Process finished with exit code 143 (interrupted by signal 15: SIGTERM)
If I turn off the VPN and re-run my code it initializes and runs the rest of my test code (wonderfully I should add --- thank you for this product!).
It's easy enough for me to disable the VPN on my machine when I want to use Ray, but I hope to distribute code that uses Ray to many other users and I don't want to require them to turn off their VPN (Avast may be special, so this problem might be peculiar to that product --- note to self: try NordVPN).
- Has anyone else experienced this problem?
- If this is a general problem with VPN usage, how can I detect the problem early (I know that I could use a try/except around the ray.init() statement so that I can give a more meaningful error message to my users, but waiting 30 seconds is troublesome)
Follow-up:
I tried NordVPN to see if the ray.init() timeout was peculiar to the Avast VPN implementation --- the same timeout occurs with it.
I also tried
ray.init(redis_address='localhost:6379')
ray.init(redis_address='127.0.0.1:6379')
while either the Avast VPN or the NordVPN was active --- the timeout after 30 seconds persisted.
It happens with Encrypt.me, too, apparently. MaxOS Catalina.
Some investigation (also posted in a Ray Slack #general thread).
I added print statements to the redis/connection.py file in the Redis client library, specifically in the get_connection method around line 1063, to print the list of available connections, and when empty, to print that it’s “making a connection”, then the connection made. Here’s what I get:
2020-03-07 07:24:00,335 DEBUG services.py:509 -- Waiting for redis server at 127.0.0.1:40902 to respond...
connections: []
Making a connection...
Made connection Connection<host=127.0.0.1,port=40902,db=0>
connections: []
Making a connection...
Made connection Connection<host=127.0.0.1,port=40902,db=0>
connections: [Connection<host=127.0.0.1,port=40902,db=0>]
connections: [Connection<host=127.0.0.1,port=40902,db=0>]
connections: [Connection<host=127.0.0.1,port=40902,db=0>]
connections: [Connection<host=127.0.0.1,port=40902,db=0>]
connections: [Connection<host=127.0.0.1,port=40902,db=0>]
connections: []
Making a connection...
Made connection Connection<host=10.137.24.26,port=40902,db=0>
Traceback (most recent call last):
File "/Users/deanwampler/anaconda3/envs/ray-dev/lib/python3.7/site-packages/redis/connection.py", line 539, in connect
sock = self._connect()
File "/Users/deanwampler/anaconda3/envs/ray-dev/lib/python3.7/site-packages/redis/connection.py", line 596, in _connect
raise err
File "/Users/deanwampler/anaconda3/envs/ray-dev/lib/python3.7/site-packages/redis/connection.py", line 584, in _connect
sock.connect(socket_address)
TimeoutError: [Errno 60] Operation timed out
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/deanwampler/projects/ray/ray-deanw-git/python/ray/worker.py", line 745, in init
ray_params=ray_params)
File "/Users/deanwampler/projects/ray/ray-deanw-git/python/ray/node.py", line 164, in __init__
self.start_head_processes()
File "/Users/deanwampler/projects/ray/ray-deanw-git/python/ray/node.py", line 623, in start_head_processes
self.start_redis()
File "/Users/deanwampler/projects/ray/ray-deanw-git/python/ray/node.py", line 446, in start_redis
fate_share=self.kernel_fate_share)
File "/Users/deanwampler/projects/ray/ray-deanw-git/python/ray/services.py", line 727, in start_redis
primary_redis_client.set("NumRedisShards", str(num_redis_shards))
File "/Users/deanwampler/anaconda3/envs/ray-dev/lib/python3.7/site-packages/redis/client.py", line 1519, in set
return self.execute_command('SET', *pieces)
File "/Users/deanwampler/anaconda3/envs/ray-dev/lib/python3.7/site-packages/redis/client.py", line 836, in execute_command
conn = self.connection or pool.get_connection(command_name, **options)
File "/Users/deanwampler/anaconda3/envs/ray-dev/lib/python3.7/site-packages/redis/connection.py", line 1076, in get_connection
connection.connect()
File "/Users/deanwampler/anaconda3/envs/ray-dev/lib/python3.7/site-packages/redis/connection.py", line 544, in connect
raise ConnectionError(self._error_message(e))
redis.exceptions.ConnectionError: Error 60 connecting to 10.X.Y.Z:40902. Operation timed out.
Where 10.X.Y.Z is my obfuscated IP address for my VPN connection. ;)
So, this pathway is traversed eight times (I'm running locally, i.e., calling ray.init()). For three passes, the connections list is empty. Twice it correctly constructs a connection for 127.0.0.1, but the last time it uses the VPN address and fails to connect.
I figured out where the problem occurs. In services.py, method get_node_ip_address opens a socket to the Google DNS resolver to get what it thinks is the correct routable IP address for the node (at least I think that's why this is done...), rather than "accidentally" using the loopback address. When you're using a VPN, the IP address discovered appears to be the address given by your VPN, rather than the IP address on your LAN (e.g., 192.168.1.X), so the VPN address is returned. But when you're just running Ray locally, that address is not what you want when connecting to Redis. You want your LAN address.
Once the exception is thrown, before I exit the Python interpreter and effectively kill Redis, I can telnet to the open Redis port using the LAN IP address or 127.0.0.1, but not using the VPN IP address.
Paging: @edoakes @pcmoritz
I think in general we want the local LAN address, right? We don't want or need an internet connection in most situations. I can look into getting that address if you'd like, let me know. (Note that this in general is ill-defined, since there can be multiple such addresses, but with some heuristics we can usually find something useful.)
Lots of suggestions, including the one we're using, here: https://stackoverflow.com/questions/24196932/how-can-i-get-the-ip-address-from-nic-in-python Most aren't portable.
Also, the workaround for now is to use ray.init(node_ip_address='…') where the value is your LAN address for the node, often something like 192.168.X.Y or 10.X.Y.Z
(Edit: Didn't realize we're already avoiding a network hit.)
We might want to parse the ARP table (arp -a), though I have to try it out and see if it might miss edge cases. I can try to find a solution and post it here.
There's also the routing table, maybe that's better. (netstat -r -n is the CLI.)
I whipped something up. Would someone like to try this out and let me know if it detects your IP correctly?
(It's a bit hacky, but I'm just trying to see if using the routing table will work. We can polish it and use a better method to access the routing table if it actually works.)
import os
import subprocess
import socket
import sys
def read_netstat(fname=None):
if fname is None:
netstat = "netstat"
if sys.platform.startswith("win32"):
netstat += ".exe"
system_root = os.getenv("SystemRoot")
if system_root is not None:
netstat = os.path.join(system_root, "System32", netstat)
elif sys.platform.startswith("linux"):
netstat = os.path.join(os.sep, "bin", netstat)
elif sys.platform.startswith("darwin"):
netstat = os.path.join(os.sep, "usr", "sbin", netstat)
output = subprocess.check_output([netstat, "-r", "-n"])
else:
with open(fname, "rb") as f: # for testing only
output = f.read()
return output
def get_netstat_entries(fname=None):
headers = None
for line in read_netstat(fname).decode("ascii").splitlines():
if sys.platform.startswith("win32"):
line = line.replace("Network Destination", "Destination")
split_line = line.split()
if len(split_line) > 0 and "Gateway" in split_line:
headers = split_line
elif headers is not None and 4 <= len(split_line) <= len(headers):
yield dict(map(lambda pair: (pair[1], split_line[pair[0]] if pair[0] < len(split_line) else None), enumerate(headers)))
def get_lan_routes(fname=None):
for entry in get_netstat_entries(fname):
destination = entry["Destination"]
gateway = entry["Gateway"]
interface = entry.get("Iface", entry.get("Netif", entry.get("Interface", entry.get("If"))))
if not (interface.startswith("127.") or interface.startswith("lo") or interface.startswith("utun")):
if "link" not in gateway.lower() and gateway not in ("0.0.0.0", "127.0.0.1", "::1"):
yield (destination, gateway, interface)
def get_my_ips(fname=None):
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
try:
for (destination, gateway, interface) in get_lan_routes(fname):
if ":" not in gateway:
sock.connect((gateway, 53))
yield sock.getsockname()[0]
finally:
sock.close()
print(set(get_my_ips()))
Hey @mehrdadn, your script works in getting the IP address.
Here is my case: I am using my work laptop, a Macbook Pro. In my home wifi, init() works fine; If I turn off the wifi as suggested in #4931, init() works fine.
In a public wifi, init() throws:
ConnectionError: Error 60 connecting to 172.26.144.214:29568. Operation timed out.
So I agree with your sentiment above:
I think in general we want the local LAN address, right? We don't want or need an internet connection in most situations.
Related to this ticket, I use PulseSecure VPN to access my company's intranet. Under the VPN, init() will not work on my laptop, but it will work when I remote into my desktop at work. @deanwampler's suggested workaround did not work for me, regardless of VPN usage.
In summary, we should move towards making internet connection a non-requirement. But in the short term, is there any other workaround I should try when using public wifi? This part of the doc makes me think there really isn't..
@yutaizhou For a workaround, if my script is getting your IP address correctly when you're VPN-connected, you can try using it via doing import ray.services and then monkeypatching
ray.services.get_node_ip_address = lambda *args: next(get_my_ips())
before you call ray.init(), and seeing if that helps.
One thing for us to consider here in the VPN case is which IP users are normally interested in: the remote (but private) IP address that is seen by computers on the VPN, or the local IP address that we have, which is routed before the VPN. My guess is users would prefer the remote IP address, but I don't know. This gets more complicated with multiple VPN layers, but I'm not sure how much we need to worry about that.
@deanwampler do you have any thoughts?
@mehrdadn it didn't work unfortunately. Sorry but can you help me understand conceptually why init() wouldn't work in my VPN case? I would be happy to supply you with more context if you need it.
Do I need to pass into init() the IP that the VPN assigns me? In that case, your script provides me with the wifi IP address, which is not what init() needs.
I see this same issue when using Catalina's built-in VPN with the following config:

@yutaizhou You shouldn't pass this to init(), no. As far as I can see, the reason init() is not working is that it gives you the IP address on your remote network, not on your local network. I think that's my script mistakenly did too. What's difficult to me is, I can imagine this being the desired behavior for many people—it depends on what they're using the VPN for. If they're just using it as a proxy, they probably want the behavior you're looking for. But if they're genuinely using it to connect to a remote network, they might want to connect to a remote cluster that's behind a VPN. We might have to eventually try both cases.
Anyway, here's a possible fix. Could you try replacing these two functions in my script, then running ray.init(node_ip_address=next(get_my_ips()) + ":" + "53")?
def get_lan_routes(fname=None):
for entry in get_netstat_entries(fname):
destination = entry["Destination"]
gateway = entry["Gateway"]
interface = entry.get("Iface", entry.get("Netif", entry.get("Interface", entry.get("If"))))
if ("." in interface or ":" in interface) and not (interface.startswith("127.") or interface.startswith("lo") or interface.startswith("utun")):
if gateway not in ("0.0.0.0", "127.0.0.1", "::1"):
yield (destination, gateway, interface)
def get_my_ips(fname=None):
seen = set()
for (destination, gateway, interface) in get_lan_routes(fname):
if interface not in seen:
seen.add(interface)
yield interface
Hey @mehrdadn, unfortunately, get_my_ips() didn't return anything so I just got StopIteration error
@yutaizhou Oh wow, that's strange, sorry about that. If you'd like, feel free to share what your netstat -r -n returns so I can see if I can debug it. (But if you do, don't forget to redact any information that might be private, such as your MAC addresses, public IP address, etc.)
I have the exact same timeout issue while trying to run ray.init() on a laptop connecting to NordVPN.
A quick get around I've found is to simply pass 127.0.0.1 to get_node_ip_address to force it returns the loopback address.
import ray
ray.services.get_node_ip_address = lambda: '127.0.0.1'
ray.init()
In home wifi, both IPv4 and IPv6 are enabled and ray.init() has failed with OSError: [Errno 49] error while attempting to bind on address ('2600:1700:4570:7ce0::1', 8265, 0, 0): can't assign requested address
I followed @iamWing: suggested workaround and able to start without any issues.
I was trying to get the above workaround to work, and it does so fine when running in non-local mode but fails when I run:
ray.init(local_mode=True, ...)
tune.run(...)
I'm using Globalconnect VPN and having the same issue. None of the solutions here worked for me.