Skip to content

Commit 2d7b24d

Browse files
committed
tests(k8s): handle port not being open when proxying into k8s (#1414)
The best fix is to make the SocketProxy more robust so that it doesn't crash when a connection attempt fails. By catching the expected BrokenPipeError, the proxy can simply discard the failed connection and continue listening for the next attempt from the Wait.until loop. This turns your test from a "hope it works" scenario into a reliable polling check.
1 parent 5a36337 commit 2d7b24d

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

tests/containers/socket_proxy.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,19 @@ def listen_and_serve_until_canceled(self):
9090
# ISSUE-922: socket.accept() blocks, so if cancel() did not come very fast, we'd loop over and block
9191
if self.server_socket in readable:
9292
client_socket, addr = self.server_socket.accept()
93-
logging.info(f"Accepted connection from {addr[0]}:{addr[1]}")
94-
# handle client synchronously, which means that there can be at most one at a time
95-
self._handle_client(client_socket)
93+
logging.info(f"Proxy accepted connection from {addr[0]}:{addr[1]}")
94+
try:
95+
# handle client synchronously, which means that there can be at most one at a time
96+
self._handle_client(client_socket)
97+
except (BrokenPipeError, ConnectionResetError) as e:
98+
# BrokenPipeError happens when the proxy connects to the pod, but the service inside is not yet listening.
99+
# The client (Wait.until) will retry.
100+
logging.info(f"Proxy connection to remote failed, likely due to service not being ready: {e}")
101+
# The client_socket is closed by the `with` statement in `_handle_client`.
102+
# We continue the loop to accept the next connection attempt.
103+
continue
96104
except Exception as e:
97-
logging.exception("Proxying failed to listen", exc_info=e)
105+
logging.exception("Proxying failed with an unhandled exception", exc_info=e)
98106
raise
99107
finally:
100108
self.server_socket.close()

0 commit comments

Comments
 (0)