more connection closes to fix leaks

This commit is contained in:
Dmitriy Ryajov 2020-04-04 20:42:08 -06:00
parent e4303110a6
commit 8dda6c289d
3 changed files with 14 additions and 6 deletions

View File

@ -126,8 +126,7 @@ method handle*(m: Mplex) {.async, gcsafe.} =
trace "Exception occurred", exception = exc.msg
finally:
trace "stopping mplex main loop"
if not m.connection.closed():
await m.connection.close()
await m.close()
proc newMplex*(conn: Connection,
maxChanns: uint = MaxChannels): Mplex =
@ -154,5 +153,10 @@ method newStream*(m: Mplex,
method close*(m: Mplex) {.async, gcsafe.} =
trace "closing mplex muxer"
if not m.connection.closed():
await m.connection.close()
await allFutures(@[allFutures(toSeq(m.remote.values).mapIt(it.reset())),
allFutures(toSeq(m.local.values).mapIt(it.reset()))])
m.remote.clear()
m.local.clear()

View File

@ -198,6 +198,7 @@ proc upgradeIncoming(s: Switch, conn: Connection) {.async, gcsafe.} =
# handle subsequent requests
await ms.handle(sconn)
await sconn.close()
if (await ms.select(conn)): # just handshake
# add the secure handlers
@ -285,14 +286,14 @@ proc start*(s: Switch): Future[seq[Future[void]]] {.async, gcsafe.} =
proc handle(conn: Connection): Future[void] {.async, closure, gcsafe.} =
try:
dumpNumberOfInstances()
await s.upgradeIncoming(conn) # perform upgrade on incoming connection
except CatchableError as exc:
trace "Exception occurred in Switch.start", exc = exc.msg
finally:
if not isNil(conn) and not conn.closed:
await conn.close()
await conn.close()
await s.cleanupConn(conn)
dumpNumberOfInstances()
var startFuts: seq[Future[void]]
for t in s.transports: # for each transport

View File

@ -33,7 +33,10 @@ proc connHandler*(t: Transport,
let handlerFut = if isNil(t.handler): nil else: t.handler(conn)
let connHolder: ConnHolder = ConnHolder(connection: conn,
connFuture: handlerFut)
t.connections.add(connHolder)
# TODO: this needs rethinking,
# currently it leaks since there
# is no way to delete the conn on close
# t.connections.add(connHolder)
result = conn
proc connCb(server: StreamServer,