Browse Source

event: Make cancel() for regular tasks work like remove()

Fixes #1741
rdb 4 months ago
parent
commit
5bc9b70e86
2 changed files with 18 additions and 2 deletions
  1. 1 1
      panda/src/event/pythonTask.cxx
  2. 17 1
      tests/event/test_futures.py

+ 1 - 1
panda/src/event/pythonTask.cxx

@@ -503,7 +503,7 @@ cancel() {
       --_chain->_num_awaiting_tasks;
       --_chain->_num_awaiting_tasks;
       return true;
       return true;
     }
     }
-    else if (must_cancel || _fut_waiter != nullptr) {
+    else if (_generator != nullptr && (must_cancel || _fut_waiter != nullptr)) {
       // We may be polling an external future, so we still need to throw a
       // We may be polling an external future, so we still need to throw a
       // CancelledException and allow it to be caught.
       // CancelledException and allow it to be caught.
       if (must_cancel) {
       if (must_cancel) {

+ 17 - 1
tests/event/test_futures.py

@@ -133,13 +133,29 @@ def test_future_wait_cancel():
         fut.result()
         fut.result()
 
 
 
 
-def test_task_cancel():
+def test_task_remove():
     task_mgr = core.AsyncTaskManager.get_global_ptr()
     task_mgr = core.AsyncTaskManager.get_global_ptr()
     task = core.PythonTask(lambda task: task.done)
     task = core.PythonTask(lambda task: task.done)
     task_mgr.add(task)
     task_mgr.add(task)
 
 
     assert not task.done()
     assert not task.done()
     task_mgr.remove(task)
     task_mgr.remove(task)
+    assert not task.is_alive()
+    assert task.done()
+    assert task.cancelled()
+
+    with pytest.raises(CancelledError):
+        task.result()
+
+
+def test_task_cancel():
+    task_mgr = core.AsyncTaskManager.get_global_ptr()
+    task = core.PythonTask(lambda task: task.done)
+    task_mgr.add(task)
+
+    assert not task.done()
+    task.cancel()
+    assert not task.is_alive()
     assert task.done()
     assert task.done()
     assert task.cancelled()
     assert task.cancelled()