diff --git a/libp2p/protocols/connectivity/relay/client.nim b/libp2p/protocols/connectivity/relay/client.nim index 8eeeeb0ee..601e29cd3 100644 --- a/libp2p/protocols/connectivity/relay/client.nim +++ b/libp2p/protocols/connectivity/relay/client.nim @@ -292,7 +292,9 @@ proc new*(T: typedesc[RelayClient], canHop: bool = false, maxCircuitPerPeer: maxCircuitPerPeer, msgSize: msgSize, isCircuitRelayV1: circuitRelayV1) - proc handleStream(conn: Connection, proto: string) {.async.} = + proc handleStream( + conn: Connection, + proto: string) {.async: (raises: [CancelledError]).} = try: case proto: of RelayV1Codec: await cl.handleStreamV1(conn) diff --git a/libp2p/protocols/connectivity/relay/relay.nim b/libp2p/protocols/connectivity/relay/relay.nim index b078cdb46..6c7c7bbd5 100644 --- a/libp2p/protocols/connectivity/relay/relay.nim +++ b/libp2p/protocols/connectivity/relay/relay.nim @@ -336,7 +336,9 @@ proc new*(T: typedesc[Relay], msgSize: msgSize, isCircuitRelayV1: circuitRelayV1) - proc handleStream(conn: Connection, proto: string) {.async.} = + proc handleStream( + conn: Connection, + proto: string) {.async: (raises: [CancelledError]).} = try: case proto: of RelayV2HopCodec: await r.handleHopStreamV2(conn) diff --git a/libp2p/protocols/identify.nim b/libp2p/protocols/identify.nim index 392eb9111..33621cd5e 100644 --- a/libp2p/protocols/identify.nim +++ b/libp2p/protocols/identify.nim @@ -1,5 +1,5 @@ # Nim-LibP2P -# Copyright (c) 2023 Status Research & Development GmbH +# Copyright (c) 2023-2024 Status Research & Development GmbH # Licensed under either of # * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) # * MIT license ([LICENSE-MIT](LICENSE-MIT)) @@ -151,14 +151,16 @@ proc new*( identify method init*(p: Identify) = - proc handle(conn: Connection, proto: string) {.async.} = + proc handle( + conn: Connection, + proto: string) {.async: (raises: [CancelledError]).} = try: trace "handling identify request", conn var pb = encodeMsg(p.peerInfo, conn.observedAddr, p.sendSignedPeerRecord) await conn.writeLp(pb.buffer) except CancelledError as exc: raise exc - except CatchableError as exc: + except LPStreamError as exc: trace "exception in identify handler", exc = exc.msg, conn finally: trace "exiting identify handler", conn @@ -207,7 +209,9 @@ proc new*(T: typedesc[IdentifyPush], handler: IdentifyPushHandler = nil): T {.pu identifypush proc init*(p: IdentifyPush) = - proc handle(conn: Connection, proto: string) {.async.} = + proc handle( + conn: Connection, + proto: string) {.async: (raises: [CancelledError]).} = trace "handling identify push", conn try: var message = await conn.readLp(64*1024) diff --git a/libp2p/protocols/ping.nim b/libp2p/protocols/ping.nim index 0921022b9..f7c36960a 100644 --- a/libp2p/protocols/ping.nim +++ b/libp2p/protocols/ping.nim @@ -36,10 +36,8 @@ type PingError* = object of LPError WrongPingAckError* = object of PingError - PingHandler* {.public.} = proc ( - peer: PeerId): - Future[void] - {.gcsafe, raises: [].} + PingHandler* {.public.} = + proc (peer: PeerId): Future[void] {.async: (raises: []).} Ping* = ref object of LPProtocol pingHandler*: PingHandler @@ -51,7 +49,9 @@ proc new*(T: typedesc[Ping], handler: PingHandler = nil, rng: ref HmacDrbgContex ping method init*(p: Ping) = - proc handle(conn: Connection, proto: string) {.async.} = + proc handle( + conn: Connection, + proto: string) {.async: (raises: [CancelledError]).} = try: trace "handling ping", conn var buf: array[PingSize, byte] @@ -62,7 +62,7 @@ method init*(p: Ping) = await p.pingHandler(conn.peerId) except CancelledError as exc: raise exc - except CatchableError as exc: + except LPStreamError as exc: trace "exception in ping handler", exc = exc.msg, conn p.handler = handle diff --git a/libp2p/protocols/protocol.nim b/libp2p/protocols/protocol.nim index cb328849d..b9becb53f 100644 --- a/libp2p/protocols/protocol.nim +++ b/libp2p/protocols/protocol.nim @@ -1,5 +1,5 @@ # Nim-LibP2P -# Copyright (c) 2023 Status Research & Development GmbH +# Copyright (c) 2023-2024 Status Research & Development GmbH # Licensed under either of # * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) # * MIT license ([LICENSE-MIT](LICENSE-MIT)) @@ -19,14 +19,13 @@ const type LPProtoHandler* = proc ( - conn: Connection, - proto: string): - Future[void] - {.gcsafe, raises: [].} + conn: Connection, + proto: string + ): Future[void] {.async: (raises: [CancelledError]).} LPProtocol* = ref object of RootObj codecs*: seq[string] - handler*: LPProtoHandler ## this handler gets invoked by the protocol negotiator + handler*: LPProtoHandler ## gets invoked by the protocol negotiator started*: bool maxIncomingStreams: Opt[int] @@ -50,10 +49,10 @@ func `codec=`*(p: LPProtocol, codec: string) = p.codecs.insert(codec, 0) proc new*( - T: type LPProtocol, - codecs: seq[string], - handler: LPProtoHandler, - maxIncomingStreams: Opt[int] | int = Opt.none(int)): T = + T: type LPProtocol, + codecs: seq[string], + handler: LPProtoHandler, + maxIncomingStreams: Opt[int] | int = Opt.none(int)): T = T( codecs: codecs, handler: handler, diff --git a/libp2p/protocols/rendezvous.nim b/libp2p/protocols/rendezvous.nim index 177116166..afd35b46e 100644 --- a/libp2p/protocols/rendezvous.nim +++ b/libp2p/protocols/rendezvous.nim @@ -641,7 +641,9 @@ proc new*( sema: newAsyncSemaphore(SemaphoreDefaultSize) ) logScope: topics = "libp2p discovery rendezvous" - proc handleStream(conn: Connection, proto: string) {.async.} = + proc handleStream( + conn: Connection, + proto: string) {.async: (raises: [CancelledError]).} = try: let buf = await conn.readLp(4096) diff --git a/libp2p/protocols/secure/secure.nim b/libp2p/protocols/secure/secure.nim index eef56f7d8..e16e2bf5c 100644 --- a/libp2p/protocols/secure/secure.nim +++ b/libp2p/protocols/secure/secure.nim @@ -88,10 +88,12 @@ method handshake*( CancelledError, LPStreamError], raw: true), base.} = raiseAssert("Not implemented!") -proc handleConn(s: Secure, - conn: Connection, - initiator: bool, - peerId: Opt[PeerId]): Future[Connection] {.async.} = +proc handleConn( + s: Secure, + conn: Connection, + initiator: bool, + peerId: Opt[PeerId] +): Future[Connection] {.async: (raises: [CancelledError, LPStreamError]).} = var sconn = await s.handshake(conn, initiator, peerId) # mark connection bottom level transport direction # this is the safest place to do this @@ -115,14 +117,8 @@ proc handleConn(s: Secure, fut1 = sconn.close() fut2 = conn.close() await allFutures(fut1, fut2) - if fut1.failed: - let err = fut1.error() - if not (err of CancelledError): - debug "error cleaning up secure connection", err = err.msg, sconn - if fut2.failed: - let err = fut2.error() - if not (err of CancelledError): - debug "error cleaning up secure connection", err = err.msg, sconn + static: doAssert typeof(fut1).E is void # Cannot fail + static: doAssert typeof(fut2).E is void # Cannot fail except CancelledError: # This is top-level procedure which will work as separate task, so it @@ -133,12 +129,14 @@ proc handleConn(s: Secure, # All the errors are handled inside `cleanup()` procedure. asyncSpawn cleanup() - return sconn + sconn method init*(s: Secure) = procCall LPProtocol(s).init() - proc handle(conn: Connection, proto: string) {.async.} = + proc handle( + conn: Connection, + proto: string) {.async: (raises: [CancelledError]).} = trace "handling connection upgrade", proto, conn try: # We don't need the result but we @@ -149,16 +147,18 @@ method init*(s: Secure) = warn "securing connection canceled", conn await conn.close() raise exc - except CatchableError as exc: + except LPStreamError as exc: warn "securing connection failed", err = exc.msg, conn await conn.close() s.handler = handle -method secure*(s: Secure, - conn: Connection, - peerId: Opt[PeerId]): - Future[Connection] {.base.} = +method secure*( + s: Secure, + conn: Connection, + peerId: Opt[PeerId] +): Future[Connection] {.async: (raises: [ + CancelledError, LPStreamError], raw: true), base.} = s.handleConn(conn, conn.dir == Direction.Out, peerId) method readOnce*(