From c42009d56e04cf4bd3f18cf03403caaf42883938 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Thu, 19 Nov 2020 09:10:25 -0600 Subject: [PATCH] don't quit accept prematurelly (#443) --- libp2p/transports/tcptransport.nim | 32 +++++++++++++++--------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/libp2p/transports/tcptransport.nim b/libp2p/transports/tcptransport.nim index b03dccf90..07f22eb2c 100644 --- a/libp2p/transports/tcptransport.nim +++ b/libp2p/transports/tcptransport.nim @@ -159,9 +159,21 @@ method stop*(t: TcpTransport) {.async, gcsafe.} = finally: t.running = false -template withTransportErrors(body: untyped): untyped = +method accept*(t: TcpTransport): Future[Connection] {.async, gcsafe.} = + ## accept a new TCP connection + ## + + if not t.running: + raise newTransportClosedError() + try: - body + let transp = await t.server.accept() + return await t.connHandler(transp, Direction.In) + except TransportOsError as exc: + # TODO: it doesn't sound like all OS errors + # can be ignored, we should re-raise those + # that can't. + debug "OS Error", exc = exc.msg except TransportTooManyError as exc: warn "Too many files opened", exc = exc.msg except TransportUseClosedError as exc: @@ -171,17 +183,6 @@ template withTransportErrors(body: untyped): untyped = trace "Unexpected error creating connection", exc = exc.msg raise exc -method accept*(t: TcpTransport): Future[Connection] {.async, gcsafe.} = - ## accept a new TCP connection - ## - - if not t.running: - raise newTransportClosedError() - - withTransportErrors: - let transp = await t.server.accept() - return await t.connHandler(transp, Direction.In) - method dial*(t: TcpTransport, address: MultiAddress): Future[Connection] {.async, gcsafe.} = @@ -190,9 +191,8 @@ method dial*(t: TcpTransport, trace "Dialing remote peer", address = $address - withTransportErrors: - let transp = await connect(address) - return await t.connHandler(transp, Direction.Out) + let transp = await connect(address) + return await t.connHandler(transp, Direction.Out) method handles*(t: TcpTransport, address: MultiAddress): bool {.gcsafe.} = if procCall Transport(t).handles(address):