better `upgraded` lifetime handling (avoid NPE) (#506)
* avoid npe on connection upgrade * add `onUpgraded` event
This commit is contained in:
parent
64b822e8f0
commit
34e330353f
|
@ -240,9 +240,7 @@ proc cleanupConn(c: ConnManager, conn: Connection) {.async.} =
|
||||||
proc onConnUpgraded(c: ConnManager, conn: Connection) {.async.} =
|
proc onConnUpgraded(c: ConnManager, conn: Connection) {.async.} =
|
||||||
try:
|
try:
|
||||||
trace "Triggering connect events", conn
|
trace "Triggering connect events", conn
|
||||||
doAssert(not isNil(conn.upgraded),
|
conn.upgrade()
|
||||||
"The `upgraded` event hasn't been properly initialized!")
|
|
||||||
conn.upgraded.complete()
|
|
||||||
|
|
||||||
let peerId = conn.peerInfo.peerId
|
let peerId = conn.peerInfo.peerId
|
||||||
await c.triggerPeerEvents(
|
await c.triggerPeerEvents(
|
||||||
|
|
|
@ -36,6 +36,21 @@ type
|
||||||
|
|
||||||
proc timeoutMonitor(s: Connection) {.async, gcsafe.}
|
proc timeoutMonitor(s: Connection) {.async, gcsafe.}
|
||||||
|
|
||||||
|
proc isUpgraded*(s: Connection): bool =
|
||||||
|
if not isNil(s.upgraded):
|
||||||
|
return s.upgraded.finished
|
||||||
|
|
||||||
|
proc upgrade*(s: Connection, failed: ref Exception = nil) =
|
||||||
|
if not isNil(failed):
|
||||||
|
s.upgraded.fail(failed)
|
||||||
|
return
|
||||||
|
|
||||||
|
s.upgraded.complete()
|
||||||
|
|
||||||
|
proc onUpgrade*(s: Connection) {.async.} =
|
||||||
|
if not isNil(s.upgraded):
|
||||||
|
await s.upgraded
|
||||||
|
|
||||||
func shortLog*(conn: Connection): string =
|
func shortLog*(conn: Connection): string =
|
||||||
if conn.isNil: "Connection(nil)"
|
if conn.isNil: "Connection(nil)"
|
||||||
elif conn.peerInfo.isNil: $conn.oid
|
elif conn.peerInfo.isNil: $conn.oid
|
||||||
|
|
|
@ -244,10 +244,8 @@ proc upgradeIncoming(s: Switch, incomingConn: Connection) {.async, gcsafe.} = #
|
||||||
await ms.handle(cconn)
|
await ms.handle(cconn)
|
||||||
except CatchableError as exc:
|
except CatchableError as exc:
|
||||||
debug "Exception in secure handler during incoming upgrade", msg = exc.msg, conn
|
debug "Exception in secure handler during incoming upgrade", msg = exc.msg, conn
|
||||||
if not isNil(cconn) and
|
if not cconn.isUpgraded:
|
||||||
not isNil(cconn.upgraded) and
|
cconn.upgrade(exc)
|
||||||
not(cconn.upgraded.finished):
|
|
||||||
cconn.upgraded.fail(exc)
|
|
||||||
finally:
|
finally:
|
||||||
if not isNil(cconn):
|
if not isNil(cconn):
|
||||||
await cconn.close()
|
await cconn.close()
|
||||||
|
@ -265,10 +263,8 @@ proc upgradeIncoming(s: Switch, incomingConn: Connection) {.async, gcsafe.} = #
|
||||||
await ms.handle(incomingConn, active = true)
|
await ms.handle(incomingConn, active = true)
|
||||||
except CatchableError as exc:
|
except CatchableError as exc:
|
||||||
debug "Exception upgrading incoming", exc = exc.msg
|
debug "Exception upgrading incoming", exc = exc.msg
|
||||||
if not isNil(incomingConn) and
|
if not incomingConn.isUpgraded:
|
||||||
not isNil(incomingConn.upgraded) and
|
incomingConn.upgrade(exc)
|
||||||
not(incomingConn.upgraded.finished):
|
|
||||||
incomingConn.upgraded.fail(exc)
|
|
||||||
finally:
|
finally:
|
||||||
if not isNil(incomingConn):
|
if not isNil(incomingConn):
|
||||||
await incomingConn.close()
|
await incomingConn.close()
|
||||||
|
@ -441,7 +437,7 @@ proc upgradeMonitor(conn: Connection, upgrades: AsyncSemaphore) {.async.} =
|
||||||
# upgrade, this timeout guarantees that a
|
# upgrade, this timeout guarantees that a
|
||||||
# "hanged" remote doesn't hold the upgrade
|
# "hanged" remote doesn't hold the upgrade
|
||||||
# forever
|
# forever
|
||||||
await conn.upgraded.wait(30.seconds) # wait for connection to be upgraded
|
await conn.onUpgrade.wait(30.seconds) # wait for connection to be upgraded
|
||||||
trace "Connection upgrade succeeded"
|
trace "Connection upgrade succeeded"
|
||||||
except CatchableError as exc:
|
except CatchableError as exc:
|
||||||
libp2p_failed_upgrades_incoming.inc()
|
libp2p_failed_upgrades_incoming.inc()
|
||||||
|
|
Loading…
Reference in New Issue