Skip to content

Commit e65a83b

Browse files
authored
Merge pull request #9 from Unity-Technologies/socket-timeout-fix
fix on the socket timeout error
2 parents f29336c + 23a3514 commit e65a83b

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

python/unityagents/environment.py

+14-16
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import os
88
import socket
99
import subprocess
10-
import signal
1110

1211
from .brain import BrainInfo, BrainParameters
1312
from .exception import UnityEnvironmentException, UnityActionException
@@ -76,28 +75,27 @@ def __init__(self, file_name, worker_id=0,
7675
except os.error:
7776
self.close()
7877
raise UnityEnvironmentException("Couldn't launch new environment. "
79-
"Provided filename does not match any \environments in {}."
80-
.format(cwd))
78+
"Provided filename does not match any \environments in {}."
79+
.format(cwd))
8180

82-
def timeout_handler():
83-
raise UnityEnvironmentException(
81+
self._socket.settimeout(30)
82+
try:
83+
try:
84+
self._socket.listen(1)
85+
self._conn, _ = self._socket.accept()
86+
self._conn.setblocking(1)
87+
p = self._conn.recv(self._buffer_size).decode('utf-8')
88+
p = json.loads(p)
89+
except socket.timeout as e:
90+
raise UnityEnvironmentException(
8491
"The Unity environment took too long to respond. Make sure {} does not need user interaction to launch "
85-
"and that the Academy and the external Brain(s) scripts are attached to objects in the Scene.".format(
92+
"and that the Academy and the external Brain(s) are attached to objects in the Scene.".format(
8693
str(file_name)))
87-
88-
old_handler = signal.signal(signal.SIGALRM, timeout_handler)
89-
signal.alarm(30) # trigger alarm in x seconds
90-
try:
91-
self._socket.listen(1)
92-
self._conn, _ = self._socket.accept()
93-
p = self._conn.recv(self._buffer_size).decode('utf-8')
94-
p = json.loads(p)
9594
except UnityEnvironmentException:
9695
proc1.kill()
9796
self.close()
9897
raise
99-
signal.signal(signal.SIGALRM, old_handler)
100-
signal.alarm(0)
98+
10199

102100
self._data = {}
103101
self._global_done = None

0 commit comments

Comments
 (0)