From 7f40ab0082af65aa3041acf0d3c1e92e4f4021c7 Mon Sep 17 00:00:00 2001 From: Christian Ulrich Date: Wed, 29 Jul 2020 20:15:01 +0200 Subject: [PATCH] source port may take a bit to become available again, so retry binding three times --- examples/app/app.nim | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/examples/app/app.nim b/examples/app/app.nim index 361a2fb..114c239 100644 --- a/examples/app/app.nim +++ b/examples/app/app.nim @@ -166,10 +166,18 @@ proc punchHole(punchdConn: PunchdConnection, serverConn: ServerConnection, proc initServerConnection(serverHostname: string, serverPort: Port, probingPort: Port): Future[ServerConnection] {.async.} = - for i in 0 .. 1: + var failCount = 0 + while result.probedPorts.len < 2: # FIXME: error handling let sock = newAsyncSocket() - sock.bindAddr(probingPort) + try: + sock.bindAddr(probingPort) + except OSError as e: + if failCount == 3: + raise e + failCount.inc + await sleepAsync(1000) + continue await sock.connect(serverHostname, serverPort) let id = rand(uint32) await sock.send(&"get-endpoint|{id}\n")