From 130c64f33a493074a64d4e2af2ae57091f5a480e Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Fri, 5 Jun 2020 18:17:05 -0600 Subject: [PATCH] don't return nil in dial (#205) * dont return nil in dial * don't crash on pubsub send --- libp2p.nimble | 2 +- libp2p/protocols/pubsub/pubsubpeer.nim | 14 ++++++++------ libp2p/standard_setup.nim | 2 +- libp2p/switch.nim | 3 +-- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/libp2p.nimble b/libp2p.nimble index e6a685d63..73ebfa361 100644 --- a/libp2p.nimble +++ b/libp2p.nimble @@ -14,7 +14,7 @@ requires "nim >= 1.2.0", "chronos >= 2.3.8", "metrics", "secp256k1", - "stew" + "stew >= 0.1.0" proc runTest(filename: string, verify: bool = true, sign: bool = true) = var excstr: string = "nim c -r --opt:speed -d:debug --verbosity:0 --hints:off" diff --git a/libp2p/protocols/pubsub/pubsubpeer.nim b/libp2p/protocols/pubsub/pubsubpeer.nim index 73324e1e6..95688e03c 100644 --- a/libp2p/protocols/pubsub/pubsubpeer.nim +++ b/libp2p/protocols/pubsub/pubsubpeer.nim @@ -48,8 +48,9 @@ proc isConnected*(p: PubSubPeer): bool = proc `conn=`*(p: PubSubPeer, conn: Connection) = trace "attaching send connection for peer", peer = p.id - p.sendConn = conn - p.onConnect.fire() + if not(isNil(conn)): + p.sendConn = conn + p.onConnect.fire() proc recvObservers(p: PubSubPeer, msg: var RPCMsg) = # trigger hooks @@ -113,10 +114,11 @@ proc send*(p: PubSubPeer, msgs: seq[RPCMsg]) {.async.} = trace "about to send message", peer = p.id, encoded = digest await p.onConnect.wait() - trace "sending encoded msgs to peer", peer = p.id, - encoded = encoded.buffer.shortLog - await p.sendConn.writeLp(encoded.buffer) - p.sentRpcCache.put(digest) + if p.isConnected: # this can happen if the remote disconnected + trace "sending encoded msgs to peer", peer = p.id, + encoded = encoded.buffer.shortLog + await p.sendConn.writeLp(encoded.buffer) + p.sentRpcCache.put(digest) except CatchableError as exc: trace "unable to send to remote", exc = exc.msg p.sendConn = nil diff --git a/libp2p/standard_setup.nim b/libp2p/standard_setup.nim index 240bf33ce..9d167801f 100644 --- a/libp2p/standard_setup.nim +++ b/libp2p/standard_setup.nim @@ -45,7 +45,7 @@ proc newStandardSwitch*(privKey = none(PrivateKey), muxers = {MplexCodec: mplexProvider}.toTable identify = newIdentify(peerInfo) - var + var secureManagerInstances: seq[Secure] for sec in secureManagers: case sec diff --git a/libp2p/switch.nim b/libp2p/switch.nim index 5d0281b94..7f317a479 100644 --- a/libp2p/switch.nim +++ b/libp2p/switch.nim @@ -282,8 +282,7 @@ proc dial*(s: Switch, trace "Attempting to select remote", proto = proto if not await s.ms.select(result, proto): - warn "Unable to select sub-protocol", proto = proto - return nil + raise newException(CatchableError, "Unable to select sub-protocol " & proto) proc mount*[T: LPProtocol](s: Switch, proto: T) {.gcsafe.} = if isNil(proto.handler):