Skip to content

Commit 5c82c77

Browse files
address #486 for colima delay for port avail for connect
1 parent fefb9d0 commit 5c82c77

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

core/testcontainers/core/container.py

+20-2
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,26 @@ def _create_instance(cls) -> "Reaper":
221221
container_host = Reaper._container.get_container_host_ip()
222222
container_port = int(Reaper._container.get_exposed_port(8080))
223223

224-
Reaper._socket = socket()
225-
Reaper._socket.connect((container_host, container_port))
224+
last_connection_exception: Optional[Exception] = None
225+
for _ in range(50):
226+
try:
227+
Reaper._socket = socket()
228+
Reaper._socket.connect((container_host, container_port))
229+
last_connection_exception = None
230+
break
231+
except (ConnectionRefusedError, OSError) as e:
232+
if Reaper._socket is not None:
233+
with contextlib.suppress(Exception):
234+
Reaper._socket.close()
235+
Reaper._socket = None
236+
last_connection_exception = e
237+
238+
from time import sleep
239+
240+
sleep(0.5)
241+
if last_connection_exception:
242+
raise last_connection_exception
243+
226244
Reaper._socket.send(f"label={LABEL_SESSION_ID}={SESSION_ID}\r\n".encode())
227245

228246
Reaper._instance = Reaper()

0 commit comments

Comments
 (0)