bpo-36613: call remove_done_callback if exception (GH-12800)


Call remove_done_callback() in finally block.

https://bugs.python.org/issue36613
(cherry picked from commit c1964e9e2177eabe821f3e4243be8b99e0d2d542)

Co-authored-by: gescheit <gescheit12@gmail.com>
diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py
index 416c346..2af4f32 100644
--- a/Lib/asyncio/tasks.py
+++ b/Lib/asyncio/tasks.py
@@ -457,10 +457,11 @@
     finally:
         if timeout_handle is not None:
             timeout_handle.cancel()
+        for f in fs:
+            f.remove_done_callback(_on_completion)
 
     done, pending = set(), set()
     for f in fs:
-        f.remove_done_callback(_on_completion)
         if f.done():
             done.add(f)
         else: