From 2039e3e883ac42cd9b6012335a92564ca2b56387 Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Fri, 19 Jun 2020 06:08:00 -0700 Subject: [PATCH] fix #13899 defer now works with async (#14723) --- lib/pure/asyncmacro.nim | 3 +++ tests/async/tasync_misc.nim | 25 ++++++++++++++++++------- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/lib/pure/asyncmacro.nim b/lib/pure/asyncmacro.nim index cbed6372c..621a4b00c 100644 --- a/lib/pure/asyncmacro.nim +++ b/lib/pure/asyncmacro.nim @@ -207,6 +207,9 @@ proc asyncSingleProc(prc: NimNode): NimNode {.compileTime.} = futureVarIdents) # don't do anything with forward bodies (empty) if procBody.kind != nnkEmpty: + # fix #13899, defer should not escape its original scope + procBody = newStmtList(newTree(nnkBlockStmt, newEmptyNode(), procBody)) + procBody.add(createFutureVarCompletions(futureVarIdents, nil)) if not subtypeIsVoid: diff --git a/tests/async/tasync_misc.nim b/tests/async/tasync_misc.nim index c5ee22a42..29e6faa72 100644 --- a/tests/async/tasync_misc.nim +++ b/tests/async/tasync_misc.nim @@ -1,8 +1,3 @@ -discard """ - exitcode: 0 - output: "ok" -""" - import json, asyncdispatch block: #6100 let done = newFuture[int]() @@ -61,5 +56,21 @@ block: # 12743 waitFor prc() - -echo "ok" +block: # issue #13899 + proc someConnect() {.async.} = + await sleepAsync(1) + proc someClose() {.async.} = + await sleepAsync(2) + proc testFooFails(): Future[bool] {.async.} = + await someConnect() + defer: + await someClose() + result = true + proc testFooSucceed(): Future[bool] {.async.} = + try: + await someConnect() + finally: + await someClose() + result = true + doAssert waitFor testFooSucceed() + doAssert waitFor testFooFails()