Fix unhandled exceptions #3

This commit is contained in:
kdeme 2019-04-01 16:56:52 +02:00 committed by zah
parent 2d75751960
commit 5550179dd1
1 changed files with 27 additions and 12 deletions

View File

@ -505,7 +505,16 @@ proc nextMsg*(peer: Peer, MsgType: type): Future[MsgType] =
proc dispatchMessages*(peer: Peer) {.async.} = proc dispatchMessages*(peer: Peer) {.async.} =
while true: while true:
var (msgId, msgData) = await peer.recvMsg() var msgId: int
var msgData: Rlp
try:
(msgId, msgData) = await peer.recvMsg()
except TransportIncompleteError:
trace "Connection dropped in dispatchMessages"
# This can happen during the rlpx connection setup or at any point after.
# Because this code does not know, a disconnect needs to be done.
asyncDiscard peer.disconnect(ClientQuitting)
return
if msgId == 1: # p2p.disconnect if msgId == 1: # p2p.disconnect
await peer.transport.closeWait() await peer.transport.closeWait()
@ -1337,7 +1346,7 @@ proc rlpxConnect*(node: EthereumNode, remote: Node): Future[Peer] {.async.} =
except TransportIncompleteError: except TransportIncompleteError:
trace "Connection dropped in rlpxConnect", remote trace "Connection dropped in rlpxConnect", remote
except UselessPeerError: except UselessPeerError:
trace "Useless peer ", peer = remote trace "Disconnecting useless peer", peer = remote
except RlpTypeMismatch: except RlpTypeMismatch:
# Some peers report capabilities with names longer than 3 chars. We ignore # Some peers report capabilities with names longer than 3 chars. We ignore
# those for now. Maybe we should allow this though. # those for now. Maybe we should allow this though.
@ -1345,9 +1354,11 @@ proc rlpxConnect*(node: EthereumNode, remote: Node): Future[Peer] {.async.} =
except TransportOsError: except TransportOsError:
trace "TransportOsError", err = getCurrentExceptionMsg() trace "TransportOsError", err = getCurrentExceptionMsg()
except: except:
debug "Exception in rlpxConnect", remote, error "Exception in rlpxConnect", remote,
exc = getCurrentException().name, exc = getCurrentException().name,
err = getCurrentExceptionMsg() err = getCurrentExceptionMsg()
result = nil
raise
if not ok: if not ok:
if not isNil(result.transport): if not isNil(result.transport):
@ -1365,6 +1376,7 @@ proc rlpxAccept*(node: EthereumNode,
var handshake = newHandshake({Responder}) var handshake = newHandshake({Responder})
handshake.host = node.keys handshake.host = node.keys
var ok = false
try: try:
let initialSize = handshake.expectedLength let initialSize = handshake.expectedLength
var authMsg = newSeqOfCap[byte](1024) var authMsg = newSeqOfCap[byte](1024)
@ -1414,22 +1426,26 @@ proc rlpxAccept*(node: EthereumNode,
result.remote = newNode(initEnode(handshake.remoteHPubkey, address)) result.remote = newNode(initEnode(handshake.remoteHPubkey, address))
await postHelloSteps(result, response) await postHelloSteps(result, response)
ok = true
except PeerDisconnected as e: except PeerDisconnected as e:
if e.reason == AlreadyConnected: if e.reason == AlreadyConnected:
debug "Disconnect during rlpxAccept", reason = e.reason trace "Disconnect during rlpxAccept", reason = e.reason
else: else:
debug "Unexpected disconnect during rlpxAccept", reason = e.reason debug "Unexpected disconnect during rlpxAccept", reason = e.reason
transport.close() except TransportIncompleteError:
result = nil trace "Connection dropped in rlpxAccept", remote = result.remote
raise e except UselessPeerError:
trace "Disconnecting useless peer", peer = result.remote
except: except:
let e = getCurrentException() error "Exception in rlpxAccept",
debug "Exception in rlpxAccept",
err = getCurrentExceptionMsg(), err = getCurrentExceptionMsg(),
stackTrace = getCurrentException().getStackTrace() stackTrace = getCurrentException().getStackTrace()
transport.close()
if not ok:
if not isNil(result.transport):
result.transport.close()
result = nil result = nil
raise e raise
when isMainModule: when isMainModule:
@ -1450,4 +1466,3 @@ when isMainModule:
dispatchMsgPtr = invokeThunk dispatchMsgPtr = invokeThunk
recvMsgPtr: GcSafeRecvMsg = recvMsg recvMsgPtr: GcSafeRecvMsg = recvMsg
acceptPtr: GcSafeAccept = rlpxAccept acceptPtr: GcSafeAccept = rlpxAccept