diff --git a/giambio/core.py b/giambio/core.py index 0c47ea8..86d1332 100644 --- a/giambio/core.py +++ b/giambio/core.py @@ -50,6 +50,7 @@ class EventLoop: self.running = None # This will always point to the currently running coroutine (Task object) self.joined = defaultdict(list) # Tasks that want to join self.clock = default_timer # Monotonic clock to keep track of elapsed time + self.tree = set() # Keep track of coroutines to know their state def loop(self): """Main event loop for giambio""" @@ -76,6 +77,10 @@ class EventLoop: except StopIteration as e: self.running.ret_value = e.args[0] if e.args else None # Saves the return value self.to_run.extend(self.joined.pop(self.running, ())) # Reschedules the parent task + except CancelledError: + self.tree.discard(self.running) + self.running.cancelled = True # Update the coroutine status + raise except Exception as has_raised: if self.running.joined: self.running.exception = has_raised # Errored? Save the exception @@ -223,6 +228,6 @@ def join(task: Task): @types.coroutine def cancel(task: Task): - task.cancelled = True yield "want_cancel", task + assert task.cancelled diff --git a/test.py b/test.py index 5f13e33..8ab8e03 100644 --- a/test.py +++ b/test.py @@ -43,4 +43,4 @@ async def echo_server(sock: AsyncSocket, addr: tuple): logging.info(f"Connection from {addr} closed") -loop.start(make_srv, ('', 1501)) +loop.start(make_srv, ('', 1500))