From 247e453b719e1ce85c992878e16508e580151c14 Mon Sep 17 00:00:00 2001 From: cheatfate Date: Tue, 2 Jul 2019 21:26:21 +0300 Subject: [PATCH] Fix hidden close issue in AsyncStream. --- chronos/streams/asyncstream.nim | 5 ++++- chronos/streams/chunkstream.nim | 4 ---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/chronos/streams/asyncstream.nim b/chronos/streams/asyncstream.nim index eceea210..af99a29c 100644 --- a/chronos/streams/asyncstream.nim +++ b/chronos/streams/asyncstream.nim @@ -707,7 +707,8 @@ proc close*(rw: AsyncStreamRW) = if not isNil(rw.udata): GC_unref(cast[ref int](rw.udata)) rw.state = AsyncStreamState.Closed - rw.future.complete() + if not(rw.future.finished()): + rw.future.complete() when rw is AsyncStreamReader: untrackAsyncStreamReader(rw) elif rw is AsyncStreamWriter: @@ -718,11 +719,13 @@ proc close*(rw: AsyncStreamRW) = callSoon(continuation) else: rw.exevent.fire() + rw.future.addCallback(continuation) elif rw is AsyncStreamWriter: if isNil(rw.wsource) or isNil(rw.writerLoop): callSoon(continuation) else: rw.exevent.fire() + rw.future.addCallback(continuation) proc closeWait*(rw: AsyncStreamRW): Future[void] = ## Close and frees resources of stream ``rw``. diff --git a/chronos/streams/chunkstream.nim b/chronos/streams/chunkstream.nim index d2723f2a..fc814691 100644 --- a/chronos/streams/chunkstream.nim +++ b/chronos/streams/chunkstream.nim @@ -179,8 +179,6 @@ proc chunkedReadLoop(stream: AsyncStreamReader) {.async.} = # incoming data anymore. rstream.buffer.forget() - untrackAsyncStreamReader(rstream) - proc chunkedWriteLoop(stream: AsyncStreamWriter) {.async.} = var wstream = cast[ChunkedStreamWriter](stream) var exitFut = wstream.exevent.wait() @@ -277,8 +275,6 @@ proc chunkedWriteLoop(stream: AsyncStreamWriter) {.async.} = wstream.state = AsyncStreamState.Finished break - untrackAsyncStreamWriter(wstream) - proc init*[T](child: ChunkedStreamReader, rsource: AsyncStreamReader, bufferSize = ChunkBufferSize, udata: ref T) = init(cast[AsyncStreamReader](child), rsource, chunkedReadLoop, bufferSize,