Skip to content

Commit 109571b

Browse files
committed
fix: pre-probe MQTT port for clear error on blocked networks (#107)
When port 8883 is blocked by campus VPNs or firewalls (e.g. eduroam), the MQTT client would hang for 60s then crash with a raw traceback. Now pre-probes the port with a 5-second socket connection test: - On failure: prints a clear error message suggesting the network may be blocking the port, then exits gracefully. - Also fixes a crash where setsockopt was called on a None socket after a failed MQTT connect (the except block continued execution instead of exiting). Closes #107
1 parent b3d61f6 commit 109571b

1 file changed

Lines changed: 18 additions & 1 deletion

File tree

arena/arena_mqtt.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,27 @@ def __init__(
225225
else:
226226
self.transport.tls_set_context(ssl._create_unverified_context())
227227
self.transport.tls_insecure_set(True)
228+
# pre-probe MQTT port to fail fast with a clear error on blocked networks
229+
probe_timeout = 5 # seconds
230+
try:
231+
probe = socket.create_connection((self.mqtt_host, port), timeout=probe_timeout)
232+
probe.close()
233+
except (socket.timeout, OSError) as err:
234+
print(f"\n{'='*60}")
235+
print(f"ERROR: Cannot reach MQTT broker at {self.mqtt_host}:{port}")
236+
print(f" Connection failed: {err}")
237+
print(f"")
238+
print(f" This commonly happens when your network blocks port {port}")
239+
print(f" (e.g. campus VPN, eduroam, corporate firewall).")
240+
print(f" Try connecting from a different network.")
241+
print(f"{'='*60}\n")
242+
sys.exit(1)
243+
228244
try:
229245
self.transport.connect(self.mqtt_host, port=port, keepalive=60)
230246
except Exception as err:
231-
print(f'MQTT connect error to {self.mqtt_host}, port={port}: Result Code={err}')
247+
print(f"MQTT connect error to {self.mqtt_host}, port={port}: {err}")
248+
sys.exit(1)
232249
self.transport.socket().setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 2048)
233250

234251
def generate_client_id(self):

0 commit comments

Comments
 (0)