wip: try handling child stream exceptions

This commit is contained in:
Dmitriy Ryajov 2020-05-23 11:09:52 -06:00
parent e3f8f53620
commit 9c2f31262e
1 changed files with 59 additions and 44 deletions

View File

@ -7,7 +7,6 @@
## This file may not be copied, modified, or distributed except according to ## This file may not be copied, modified, or distributed except according to
## those terms. ## those terms.
import oids
import chronos, chronicles, metrics import chronos, chronicles, metrics
import peerinfo, import peerinfo,
errors, errors,
@ -15,6 +14,9 @@ import peerinfo,
stream/lpstream, stream/lpstream,
peerinfo peerinfo
when chronicles.enabledLogLevel == LogLevel.TRACE:
import oids
export lpstream export lpstream
logScope: logScope:
@ -66,51 +68,57 @@ proc `$`*(conn: Connection): string =
if not isNil(conn.peerInfo): if not isNil(conn.peerInfo):
result = $(conn.peerInfo) result = $(conn.peerInfo)
proc bindStreamClose(conn: Connection) {.async.} =
# bind stream's close event to connection's close
# to ensure correct close propagation
if not isNil(conn.stream.closeEvent):
await conn.stream.closeEvent.wait()
trace "wrapped stream closed, about to close conn",
closed = conn.isClosed, conn = $conn
if not conn.isClosed:
trace "wrapped stream closed, closing conn",
closed = conn.isClosed, conn = $conn
await conn.close()
proc init[T: Connection](self: var T, stream: LPStream): T = proc init[T: Connection](self: var T, stream: LPStream): T =
## create a new Connection for the specified async reader/writer ## create a new Connection for the specified async reader/writer
new self new self
self.stream = stream self.stream = stream
self.closeEvent = newAsyncEvent() self.initStream()
when chronicles.enabledLogLevel == LogLevel.TRACE:
self.oid = genOid()
asyncCheck self.bindStreamClose()
inc getConnectionTracker().opened
libp2p_open_connection.inc()
return self return self
proc newConnection*(stream: LPStream): Connection = proc newConnection*(stream: LPStream): Connection =
## create a new Connection for the specified async reader/writer ## create a new Connection for the specified async reader/writer
result.init(stream) result.init(stream)
method initStream*(s: Connection) =
procCall LPStream(s).initStream()
trace "created connection", oid = s.oid
inc getConnectionTracker().opened
libp2p_open_connection.inc()
method readExactly*(s: Connection, method readExactly*(s: Connection,
pbytes: pointer, pbytes: pointer,
nbytes: int): nbytes: int):
Future[void] {.gcsafe.} = Future[void] {.async, gcsafe.} =
s.stream.readExactly(pbytes, nbytes) try:
await s.stream.readExactly(pbytes, nbytes)
except CatchableError as exc:
await s.close()
raise exc
method readOnce*(s: Connection, method readOnce*(s: Connection,
pbytes: pointer, pbytes: pointer,
nbytes: int): nbytes: int):
Future[int] {.gcsafe.} = Future[int] {.async, gcsafe.} =
s.stream.readOnce(pbytes, nbytes) try:
result = await s.stream.readOnce(pbytes, nbytes)
except CatchableError as exc:
await s.close()
raise exc
method write*(s: Connection, method write*(s: Connection,
msg: seq[byte]): msg: seq[byte]):
Future[void] {.gcsafe.} = Future[void] {.async, gcsafe.} =
s.stream.write(msg) try:
await s.stream.write(msg)
except CatchableError as exc:
await s.close()
raise exc
method atEof*(s: Connection): bool {.inline.} =
if isNil(s.stream):
return true
s.stream.atEof
method closed*(s: Connection): bool = method closed*(s: Connection): bool =
if isNil(s.stream): if isNil(s.stream):
@ -119,30 +127,37 @@ method closed*(s: Connection): bool =
result = s.stream.closed result = s.stream.closed
method close*(s: Connection) {.async, gcsafe.} = method close*(s: Connection) {.async, gcsafe.} =
trace "about to close connection", closed = s.closed, conn = $s try:
if not s.isClosed: if not s.isClosed:
s.isClosed = true s.isClosed = true
inc getConnectionTracker().closed
trace "about to close connection", closed = s.closed,
conn = $s,
oid = s.oid
if not isNil(s.stream) and not s.stream.closed: if not isNil(s.stream) and not s.stream.closed:
trace "closing child stream", closed = s.closed, conn = $s trace "closing child stream", closed = s.closed,
try: conn = $s,
oid = s.stream.oid
await s.stream.close() await s.stream.close()
except CancelledError as exc: # s.stream = nil
raise exc
except CatchableError as exc:
debug "Error while closing child stream", err = exc.msg
s.closeEvent.fire() s.closeEvent.fire()
trace "waiting readloops", count=s.readLoops.len,
trace "waiting readloops", count=s.readLoops.len, conn = $s conn = $s,
let loopFuts = await allFinished(s.readLoops) oid = s.oid
checkFutures(loopFuts) await all(s.readLoops)
s.readLoops = @[] s.readLoops = @[]
trace "connection closed", closed = s.closed, conn = $s trace "connection closed", closed = s.closed,
conn = $s,
oid = s.oid
inc getConnectionTracker().closed
libp2p_open_connection.dec() libp2p_open_connection.dec()
except CatchableError as exc:
trace "exception closing connections", exc = exc.msg
method getObservedAddrs*(c: Connection): Future[MultiAddress] {.base, async, gcsafe.} = method getObservedAddrs*(c: Connection): Future[MultiAddress] {.base, async, gcsafe.} =
## get resolved multiaddresses for the connection ## get resolved multiaddresses for the connection