From a8a1138b8b66ac51378897ef126b3783dcf9a2ca Mon Sep 17 00:00:00 2001 From: cheatfate Date: Thu, 6 Jun 2019 21:22:17 +0300 Subject: [PATCH] Fix for race condition in *nix connect(). --- chronos/transports/stream.nim | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/chronos/transports/stream.nim b/chronos/transports/stream.nim index 6717d2b..769a2a0 100644 --- a/chronos/transports/stream.nim +++ b/chronos/transports/stream.nim @@ -1076,22 +1076,23 @@ else: return retFuture proc continuation(udata: pointer) = - var data = cast[ptr CompletionData](udata) - var err = 0 - let fd = data.fd - fd.removeWriter() - if not fd.getSocketError(err): - closeSocket(fd) - retFuture.fail(getTransportOsError(osLastError())) - return - if err != 0: - closeSocket(fd) - retFuture.fail(getTransportOsError(OSErrorCode(err))) - return - let transp = newStreamSocketTransport(fd, bufferSize, child) - # Start tracking transport - trackStream(transp) - retFuture.complete(transp) + if not retFuture.finished: + var data = cast[ptr CompletionData](udata) + var err = 0 + let fd = data.fd + fd.removeWriter() + if not fd.getSocketError(err): + closeSocket(fd) + retFuture.fail(getTransportOsError(osLastError())) + return + if err != 0: + closeSocket(fd) + retFuture.fail(getTransportOsError(OSErrorCode(err))) + return + let transp = newStreamSocketTransport(fd, bufferSize, child) + # Start tracking transport + trackStream(transp) + retFuture.complete(transp) while true: var res = posix.connect(SocketHandle(sock),