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: if err of Defect:
raise err raise err
else: else:
if err of CancelledError:
raise err
if isNil(first): if isNil(first):
first = err first = err
if not isNil(first): if not isNil(first):

View File

@ -71,9 +71,5 @@ proc writeMsg*(conn: Connection,
proc writeMsg*(conn: Connection, proc writeMsg*(conn: Connection,
id: uint64, id: uint64,
msgType: MessageType, msgType: MessageType,
data: string) {.async, gcsafe.} = data: string): Future[void] =
# TODO: changing this to conn.writeMsg(id, msgType, data.toBytes())
#`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())

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -523,11 +523,28 @@ proc pubsubMonitor(s: Switch, peer: PeerInfo) {.async.} =
trace "exiting pubsub monitor", peer = $peer trace "exiting pubsub monitor", peer = $peer
proc subscribePeer*(s: Switch, peerInfo: PeerInfo) {.async, gcsafe.} = proc subscribePeer*(s: Switch, peerInfo: PeerInfo): Future[void] {.gcsafe.} =
if peerInfo.peerId notin s.pubsubMonitors: ## Waits until ``server`` is not closed.
s.pubsubMonitors[peerInfo.peerId] = s.pubsubMonitor(peerInfo) ##
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, proc subscribe*(s: Switch, topic: string,
handler: TopicHandler) {.async.} = handler: TopicHandler) {.async.} =

View File

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

View File

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