Repository: ray-project/ray

Issue:
Cannot run Ray in two separate interpreters with Python 2.
To reproduce this problem, run the following in two separate interpreters.

```python
import ray
ray.init()
```

In the second one, I see the following error.

```
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-2-daa1e6a285d0> in <module>()
----> 1 ray.init()

/home/ubuntu/ray/python/ray/worker.pyc in init(redis_address, node_ip_address, object_id_seed, num_workers, driver_mode, redirect_output, num_cpus, num_gpus, num_custom_resource, num_redis_shards)
   1325                  redirect_output=redirect_output, num_cpus=num_cpus,
   1326                  num_gpus=num_gpus, num_custom_resource=num_custom_resource,
-> 1327                  num_redis_shards=num_redis_shards)
   1328 
   1329 

/home/ubuntu/ray/python/ray/worker.pyc in _init(address_info, start_ray_local, object_id_seed, num_workers, num_local_schedulers, object_store_memory, driver_mode, redirect_output, start_workers_from_local_scheduler, num_cpus, num_gpus, num_custom_resource, num_redis_shards)
   1225             num_gpus=num_gpus,
   1226             num_custom_resource=num_custom_resource,
-> 1227             num_redis_shards=num_redis_shards)
   1228     else:
   1229         if redis_address is None:

/home/ubuntu/ray/python/ray/services.pyc in start_ray_head(address_info, node_ip_address, redis_port, num_workers, num_local_schedulers, object_store_memory, worker_path, cleanup, redirect_output, start_workers_from_local_scheduler, num_cpus, num_gpus, num_custom_resource, num_redis_shards)
   1121         num_gpus=num_gpus,
   1122         num_custom_resource=num_custom_resource,
-> 1123         num_redis_shards=num_redis_shards)
   1124 
   1125 

/home/ubuntu/ray/python/ray/services.pyc in start_ray_processes(address_info, node_ip_address, redis_port, num_workers, num_local_schedulers, object_store_memory, num_redis_shards, worker_path, cleanup, redirect_output, include_global_scheduler, include_log_monitor, include_webui, start_workers_from_local_scheduler, num_cpus, num_gpus, num_custom_resource)
    991             "webui", redirect_output=True)
    992         start_ui(redis_address, stdout_file=ui_stdout_file,
--> 993                  stderr_file=ui_stderr_file, cleanup=cleanup)
    994 
    995     # Return the addresses of the relevant processes.

/home/ubuntu/ray/python/ray/services.pyc in start_ui(redis_address, stdout_file, stderr_file, cleanup)
    476         try:
    477             port_test_socket = socket.socket()
--> 478             port_test_socket.bind(("127.0.0.1", port))
    479             port_test_socket.close()
    480             break

/home/ubuntu/anaconda2/lib/python2.7/socket.pyc in meth(name, self, *args)
    226 
    227 def meth(name,self,*args):
--> 228     return getattr(self._sock,name)(*args)
    229 
    230 for _m in _socketmethods:

error: [Errno 98] Address already in use
```

It looks like the problem is that the exceptions raised by `bind` in Python 2 and Python 3 are different.

cc @alanamarzoev


Discussion:

Resolving patch:
diff --git a/python/ray/services.py b/python/ray/services.py
--- a/python/ray/services.py
+++ b/python/ray/services.py
@@ -478,7 +478,7 @@ def start_ui(redis_address, stdout_file=None, stderr_file=None, cleanup=True):
             port_test_socket.bind(("127.0.0.1", port))
             port_test_socket.close()
             break
-        except OSError:
+        except socket.error:
             port += 1
     new_env = os.environ.copy()
     new_env["REDIS_ADDRESS"] = redis_address


Test patch: