From bedd1ded5edc3bfb6877f7025ca4b21f62492ffe Mon Sep 17 00:00:00 2001 From: Eugene Kabanov Date: Fri, 5 Jun 2020 19:11:51 +0300 Subject: [PATCH] Properly fix cancellation race and not introduce FD leaks. (#102) --- chronos/asyncloop.nim | 5 +++-- chronos/transports/stream.nim | 6 ++---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/chronos/asyncloop.nim b/chronos/asyncloop.nim index 17588b3..2474b9d 100644 --- a/chronos/asyncloop.nim +++ b/chronos/asyncloop.nim @@ -629,8 +629,9 @@ elif unixPlatform: let loop = getGlobalDispatcher() proc continuation(udata: pointer) = - unregister(fd) - close(SocketHandle(fd)) + if SocketHandle(fd) in loop.selector: + unregister(fd) + close(SocketHandle(fd)) if not isNil(aftercb): aftercb(nil) diff --git a/chronos/transports/stream.nim b/chronos/transports/stream.nim index 15cd842..bc13c8d 100644 --- a/chronos/transports/stream.nim +++ b/chronos/transports/stream.nim @@ -722,8 +722,7 @@ when defined(windows): GC_unref(ovl) proc cancel(udata: pointer) {.gcsafe.} = - if not(retFuture.finished()): - sock.closeSocket() + sock.closeSocket() povl = RefCustomOverlapped() GC_ref(povl) @@ -1251,8 +1250,7 @@ else: retFuture.complete(transp) proc cancel(udata: pointer) {.gcsafe.} = - if not(retFuture.finished()): - closeSocket(sock) + closeSocket(sock) while true: var res = posix.connect(SocketHandle(sock),