Fix build, add some raises (#315)

* Fix build, add some raises

* wip

* wip more raises

* missing exc object in mplex

* proper lifetime for subscribePeer

Co-authored-by: Dmitriy Ryajov <dryajov@gmail.com>
This commit is contained in:
Giovanni Petrantoni 2020-08-06 10:30:57 +09:00 committed by GitHub
parent bd5d43874a
commit 5c986cf657
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 41 additions and 14 deletions

View File

@ -48,6 +48,8 @@ proc allFuturesThrowing*[T](args: varargs[Future[T]]): Future[void] =
if err of Defect:
raise err
else:
if err of CancelledError:
raise err
if isNil(first):
first = err
if not isNil(first):

View File

@ -71,9 +71,5 @@ proc writeMsg*(conn: Connection,
proc writeMsg*(conn: Connection,
id: uint64,
msgType: MessageType,
data: string) {.async, gcsafe.} =
# TODO: changing this to
#`await conn.writeMsg(id, msgType, data.toBytes())`
# causes all sorts of race conditions and hangs.
# DON'T DO IT!
result = conn.writeMsg(id, msgType, data.toBytes())
data: string): Future[void] =
conn.writeMsg(id, msgType, data.toBytes())

View File

@ -69,6 +69,8 @@ template withWriteLock(lock: AsyncLock, body: untyped): untyped =
template withEOFExceptions(body: untyped): untyped =
try:
body
except CancelledError as exc:
raise exc
except LPStreamEOFError as exc:
trace "muxed connection EOF", exc = exc.msg
except LPStreamClosedError as exc:

View File

@ -115,9 +115,9 @@ proc handleStream(m: Mplex, chann: LPChannel) {.async.} =
trace "finished handling stream"
doAssert(chann.closed, "connection not closed by handler!")
except CancelledError as exc:
trace "cancling stream handler", exc = exc.msg
trace "cancelling stream handler", exc = exc.msg
await chann.reset()
raise
raise exc
except CatchableError as exc:
trace "exception in stream handler", exc = exc.msg
await chann.reset()

View File

@ -93,6 +93,8 @@ method rpcHandler*(f: FloodSub,
try:
await h(t, msg.data) # trigger user provided handler
except CancelledError as exc:
raise exc
except CatchableError as exc:
trace "exception in message handler", exc = exc.msg

View File

@ -427,6 +427,8 @@ method rpcHandler*(g: GossipSub,
fromPeer = msg.fromPeer.pretty
try:
await h(t, msg.data) # trigger user provided handler
except CancelledError as exc:
raise exc
except CatchableError as exc:
trace "exception in message handler", exc = exc.msg

View File

@ -41,6 +41,8 @@ proc init*(C: type ChronosStream,
template withExceptions(body: untyped) =
try:
body
except CancelledError as exc:
raise exc
except TransportIncompleteError:
# for all intents and purposes this is an EOF
raise newLPStreamEOFError()

View File

@ -523,11 +523,28 @@ proc pubsubMonitor(s: Switch, peer: PeerInfo) {.async.} =
trace "exiting pubsub monitor", peer = $peer
proc subscribePeer*(s: Switch, peerInfo: PeerInfo) {.async, gcsafe.} =
if peerInfo.peerId notin s.pubsubMonitors:
s.pubsubMonitors[peerInfo.peerId] = s.pubsubMonitor(peerInfo)
proc subscribePeer*(s: Switch, peerInfo: PeerInfo): Future[void] {.gcsafe.} =
## Waits until ``server`` is not closed.
##
result = s.pubsubMonitors.getOrDefault(peerInfo.peerId)
var retFuture = newFuture[void]("stream.transport.server.join")
let pubsubFut = s.pubsubMonitors.mgetOrPut(
peerInfo.peerId,
s.pubsubMonitor(peerInfo))
proc continuation(udata: pointer) {.gcsafe.} =
retFuture.complete()
proc cancel(udata: pointer) {.gcsafe.} =
pubsubFut.removeCallback(continuation, cast[pointer](retFuture))
if not(pubsubFut.finished()):
pubsubFut.addCallback(continuation, cast[pointer](retFuture))
retFuture.cancelCallback = cancel
else:
retFuture.complete()
return retFuture
proc subscribe*(s: Switch, topic: string,
handler: TopicHandler) {.async.} =

View File

@ -76,6 +76,8 @@ proc connHandler*(t: TcpTransport,
if not(isNil(conn)):
await conn.close()
t.clients.keepItIf(it != client)
except CancelledError as exc:
raise exc
except CatchableError as exc:
trace "error cleaning up client", exc = exc.msg
@ -139,6 +141,8 @@ method close*(t: TcpTransport) {.async, gcsafe.} =
trace "transport stopped"
inc getTcpTransportTracker().closed
except CancelledError as exc:
raise exc
except CatchableError as exc:
trace "error shutting down tcp transport", exc = exc.msg

View File

@ -135,7 +135,7 @@ suite "Mplex":
let
conn = newBufferStream(
proc (data: seq[byte]) {.gcsafe, async.} =
result = nil
discard
)
chann = LPChannel.init(1, conn, true)