From e1af5a0c021dd5f3e134bce3c06fc36838beb6e4 Mon Sep 17 00:00:00 2001 From: Jacek Sieka Date: Mon, 22 Apr 2019 19:31:12 -0600 Subject: [PATCH] don't swallow random exceptions (#11) --- eth/p2p/auth.nim | 4 ++-- eth/p2p/blockchain_sync.nim | 8 ++++---- eth/p2p/discovery.nim | 1 + eth/p2p/rlpx.nim | 1 + 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/eth/p2p/auth.nim b/eth/p2p/auth.nim index 4a4a51a..875ffd1 100644 --- a/eth/p2p/auth.nim +++ b/eth/p2p/auth.nim @@ -384,7 +384,7 @@ proc decodeAuthMessageEip8(h: var Handshake, m: openarray[byte]): AuthStatus = h.remoteHPubkey = pubkey h.version = cast[ptr byte](versionBr.baseAddr)[] result = Success - except: + except CatchableError: result = RlpError proc decodeAckMessageEip8*(h: var Handshake, m: openarray[byte]): AuthStatus = @@ -417,7 +417,7 @@ proc decodeAckMessageEip8*(h: var Handshake, m: openarray[byte]): AuthStatus = copyMem(addr h.responderNonce[0], nonceBr.baseAddr, KeyLength) h.version = cast[ptr byte](versionBr.baseAddr)[] result = Success - except: + except CatchableError: result = RlpError proc decodeAckMessageV4(h: var Handshake, m: openarray[byte]): AuthStatus = diff --git a/eth/p2p/blockchain_sync.nim b/eth/p2p/blockchain_sync.nim index be14e21..cbc3aae 100644 --- a/eth/p2p/blockchain_sync.nim +++ b/eth/p2p/blockchain_sync.nim @@ -164,7 +164,7 @@ proc obtainBlocksFromPeer(syncCtx: SyncContext, peer: Peer) {.async.} = if bestBlockNumber > syncCtx.endBlockNumber: trace "New sync end block number", number = bestBlockNumber syncCtx.endBlockNumber = bestBlockNumber - except: + except CatchableError: debug "Exception in getBestBlockNumber()", exc = getCurrentException().name, err = getCurrentExceptionMsg() @@ -210,7 +210,7 @@ proc obtainBlocksFromPeer(syncCtx: SyncContext, peer: Peer) {.async.} = dataReceived = true else: warn "Bodies len != headers.len", bodies = bodies.len, headers = workItem.headers.len - except: + except CatchableError: # the success case sets `dataReceived`, so we can just fall back to the # failure path below. If we signal time-outs with exceptions such # failures will be easier to handle. @@ -231,7 +231,7 @@ proc obtainBlocksFromPeer(syncCtx: SyncContext, peer: Peer) {.async.} = workItem.state = Initial try: await peer.disconnect(SubprotocolReason) - except: + except CatchableError: discard syncCtx.handleLostPeer() break @@ -318,7 +318,7 @@ proc onPeerConnected(ctx: SyncContext, peer: Peer) = f.callback = proc(data: pointer) {.gcsafe.} = if f.failed: error "startSyncWithPeer failed", msg = f.readError.msg, peer - except: + except CatchableError: debug "Exception in startSyncWithPeer()", exc = getCurrentException().name, err = getCurrentExceptionMsg() diff --git a/eth/p2p/discovery.nim b/eth/p2p/discovery.nim index e14209c..b2f83e1 100644 --- a/eth/p2p/discovery.nim +++ b/eth/p2p/discovery.nim @@ -258,6 +258,7 @@ proc processClient(transp: DatagramTransport, proto.receive(a, buf) except: debug "Receive failed", err = getCurrentExceptionMsg() + raise proc open*(d: DiscoveryProtocol) = # TODO allow binding to specific IP / IPv6 / etc diff --git a/eth/p2p/rlpx.nim b/eth/p2p/rlpx.nim index 5b572c6..ec6d1fa 100644 --- a/eth/p2p/rlpx.nim +++ b/eth/p2p/rlpx.nim @@ -198,6 +198,7 @@ proc requestResolver[MsgType](msg: pointer, future: FutureBase) {.gcsafe.} = debug "Exception in requestResolver()", exc = getCurrentException().name, err = getCurrentExceptionMsg() + raise proc registerMsg(protocol: ProtocolInfo, id: int, name: string,