Skip to content
Merged
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions libp2p/transports/wstransport.nim
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ type WsTransport* = ref object of Transport
httpservers: seq[HttpServer]
wsserver: WSServer
connections: array[Direction, seq[WsStream]]
connectionCleanupFuts: seq[Future[void]]
acceptLoop: Future[void]
handshakeFuts: seq[Future[void]]
acceptResults: AsyncQueue[Connection]
Expand Down Expand Up @@ -408,26 +409,29 @@ method stop*(self: WsTransport) {.async: (raises: []).} =
await procCall Transport(self).stop() # call base

var toWait: seq[Future[void]]
if not isNil(self.acceptLoop) and not self.acceptLoop.finished:
if not self.acceptLoop.isNil:
toWait.add(self.acceptLoop.cancelAndWait())

for fut in self.handshakeFuts:
if not fut.finished:
toWait.add(fut.cancelAndWait())
toWait.add(fut.cancelAndWait())

for server in self.httpservers:
server.stop()
toWait.add(server.closeWait())

await allFutures(toWait)

discard await allFinished(
# stop connections and wait for them to be closed
await allFutures(
self.connections[Direction.In].mapIt(it.close()) &
self.connections[Direction.Out].mapIt(it.close())
)
self.connectionCleanupFuts.keepItIf(not it.finished)
await allFutures(self.connectionCleanupFuts)

self.httpservers = @[]
self.handshakeFuts = @[]
self.connectionCleanupFuts = @[]
self.acceptLoop = nil
trace "Transport stopped"
except CatchableError as e:
Expand Down Expand Up @@ -469,7 +473,9 @@ proc connHandler(
self.connections[dir].keepItIf(it != conn)
trace "Cleaned up client"

asyncSpawn onClose()
self.connectionCleanupFuts.keepItIf(not it.finished)
self.connectionCleanupFuts.add(onClose())

return conn

method accept*(
Expand Down
Loading